Sometimes the function setFromObject(mesh) returns inaccurate values

THREE.Box3.setFromObject(*object*)
is returning inaccurate values. I will demonstrate my process in understanding this issue:

I am creating 2 meshes using vertices. The first one using the triangle() function, and the other using trapezoidForm().

var triangle = function (base, height) {

    return [
        new THREE.Vector2(0,  -height / 2),
        new THREE.Vector2(-base / 2,  height / 2),
        new THREE.Vector2(base / 2,  height / 2)
    ];
}
var trapezoidForm = function (base, upperBase, height) {

    return [

        new THREE.Vector2(-base / 2,  height / 2),
        new THREE.Vector2(-upperBase / 2,  -height / 2),
        new THREE.Vector2(upperBase / 2,  -height / 2),
        new THREE.Vector2(base / 2,  height / 2),
    ];

}

The returned value from the functions are used to create the mesh:

var material    = new THREE.MeshPhongMaterial({ color: 0x666666, /*specular: 0x101010*//*, shininess: 200*/ });
var shape       = new THREE.Shape(vertices);
var mesh        = new THREE.Mesh(new THREE.ShapeGeometry(shape), material);

The mesh is then positioned in the scene, and a bounding box is created:

mesh.position.set(posX, 0, posZ);
mesh.rotation.set(-Math.PI / 2, 0, 0);
boundingBox.setFromObject(mesh);

Next, I aim to find the center of both shapes by calculating it using the bounding box:

var centerX = (boundingBox.max.x + boundingBox.min.x) * 0.5;
var centerZ = (boundingBox.max.z + boundingBox.min.z) * 0.5;

The calculation seems accurate for the triangle but incorrect for the trapezoid.

A console screenshot displays the vertices of both shapes along with their respective bounding boxes. X-coordinate is represented by the first number, and Z-coordinate by the second.

https://i.sstatic.net/g3vqs.png

Expected outcome: The bounding box for the trapezoid should ideally display:

max: X: 200
     Z: 200

min: X: -200
     Z: -100

An image depicting the current state (triangle shows a minus sign at its center, while the trapezoid does not):

https://i.sstatic.net/oEhvT.png

Answer №1

After some trial and error, I figured out the solution on my own:

The key was to establish the boundingBox before adjusting its position or rotation:

//Creating the bounding box
boundingBox.setFromObject(mesh);

mesh.position.set(posX, 0, posZ);
mesh.rotation.set(-Math.PI / 2, 0, 0);

Rather than:

mesh.position.set(posX, 0, posZ);
mesh.rotation.set(-Math.PI / 2, 0, 0);
//Creating the bounding box
boundingBox.setFromObject(mesh);

The reason why the triangle didn't present issues was because it was rendered at coordinates 0,0.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to fetch additional Product Cards using JSON and a nextPage parameter

I'm facing difficulties in nesting a fetch inside another fetch. I'm not sure if this is the correct approach, but my goal is to utilize Vanilla JavaScript to fetch another JSON from the nextPage URL within the JSON list when the "load more" butt ...

Encountered an issue with the Mongoose Schema method: The model method is not recognized as a

Here are two Mongoose model schemas that I am working with. The LabReport model includes an array that references the SoilLab model. Within the SoilLab model, there is a static method that was initially used to choose which fields to display when retrievin ...

Concealing an element from a separate module

In the angularjs single page application project that I'm working on, there are two modules: module-'A' and module-'B'. Each module has its own view templates. The view template of module-'B' contains a div with id="sit ...

Error loading content for webpack://SwaggerUIBundle/src/core/system.js in Swagger UI

When trying to utilize Swagger UI for documenting a Laravel project using the URL http://localhost:8014/api/documentation, I came across an error displayed in the browser console: Error: Unable to destructure property 'type' of 'u' as i ...

Obtain three-dimensional curves along the edges of a cuboid using Three.js

I am a beginner in three.js and I recently used box geometry to create a 3D concrete slab. Now, I am interested in adding wave-like shapes to the edges of this rectangular slab. My goal is to achieve something similar to the image linked here: https://i.ss ...

Looking for a way to locate a string for a Boolean field in mongoDB?

I am dealing with a situation where I have a field name and criteria to search for in a collection. However, the types of fields I am working with vary, ranging from String to Number to Boolean. I attempted the following approach: const fieldName1 = &ap ...

Unable to Pause Video with Javascript

I have a project with a video that plays in a continuous loop. Below is the HTML code for the video tag: <video playsinline autoplay muted loop id="myVid"> <source src="River.mp4" type="video/mp4"> </video> My goal is to make the vi ...

Building a dynamic form in React: Fetching select data from an API, posting to another API, and automatically clearing fields upon submission

I am currently working on a form that utilizes a GET request to retrieve data from an API endpoint and then proceeds to make a POST request to another endpoint. Although I have been successful in achieving this function, I am facing challenges with reset ...

Encountering an error: Module missing after implementing state syntax

My browser console is showing the error message: Uncaught Error: Cannot find module "./components/search_bar" As I dive into learning ReactJS and attempt to create a basic component, this error pops up. It appears after using the state syntax within my ...

Encountered a problem in Node.js when attempting to generate a CSV file using Bot Framework

I am currently working on a chatbot project using the Microsoft Bot Framework with Node Js. My goal is to provide the user with a CSV file upon request. However, I have encountered an issue with the format of the downloaded file 'prova.csv' not ...

After making edits, the JQGrid will automatically refresh the data with the updated information retrieved from the

My Jqgrid is in need of some editing. I have successfully set it up to save data after editing, but there is an issue with the grid not refreshing with the database data once it's saved. For instance, the version field gets updated automatically by th ...

Passing specific props to child components based on their type in a React application using TypeScript

Sorry if this question has already been addressed somewhere else, but I couldn't seem to find a solution. I'm looking for a way to pass props conditionally to children components based on their type (i.e. component type). For example, consider ...

Integrating Flask with React for a cohesive frontend and backend connection

Recently, I decided to try my hand at setting up a webapp using Flask and React by following a YouTube tutorial. Despite encountering a few issues with virtual environments (which I ultimately resolved by not using one), I managed to closely follow the tut ...

$routeProvider - providing controller dependencies based on the URL path

Take a look at the following code snippet: var app = angular.module("app", [], function($routeProvider) { $routeProvider .when("/page1", { controller: "MyController" }) .when("/page2", { controller: "MyController" }) .when("/page3", { contro ...

Troubleshooting problem with printing iframes on Firefox

When attempting to print an iframe with a large amount of data using the code below, I'm encountering an issue in Firefox where only the first page is printed. The rest of the pages are not being included in the printout. window.frames[frameID]. ...

Tips for optimizing node_module file size

Currently, I'm developing 2-3 react applications and noticed that every time I run npm install, it ends up downloading numerous dependencies into the cumbersome "node-modules" folder, totaling around 250-300mb in size! This large size makes it challen ...

Learn the proper way to write onClick in tsx with Vue 2.7.13

current version of vue is 2.7.13 Although it supports jsx, I encounter a type error when using onClick event handling. The type '(event: MouseEvent) => Promise<void>' cannot be assigned to type 'MouseEvent' Is there a correct ...

Struggling with retrieving information from a basic JSON file using React

I am facing an issue where I need to retrieve data from a local JSON file. The following is the code snippet I am using: const myReq = new Request('./data.json') fetch(myReq) .then(rawD=> rawD.json()) .then(inf ...

An object that appears to be empty at first glance, but contains values that are undefined

I am facing an issue with my object that I am populating with information. The logs show the object as empty, but when I inspect it in Chrome, it appears to have the correct details filled in. Here is a snapshot of what the logs display: closed: closed o ...

Challenges with sending JSON encoded Arrays from WordPress to PHP results in empty or NULL responses

My Plugins site has multiple Inputs that I need to gather values from and push them into an array. Then, I utilize the jQuery.post method to send this data to my PHP script. Javascript: var json = JSON.stringify(output); // output is an array with multip ...