Obtaining the global coordinates of an object in Three.js

I need to adjust the position of an object that is already placed in a new location, but currently it is moving from the local position instead of global.

this._scene.updateMatrixWorld();
this._scene.add(mesh);
var v1 = new THREE.Vector3();    
v1.setFromMatrixPosition( mesh.matrixWorld );  
mesh.position.set(v1.x +2, v1.y + 2, v1.z + 2);

Answer №1

My solution to the issue at hand:

Each mesh in my project has a geometry attribute, allowing me to access it using mesh.geometry. I took advantage of this by creating a BoundingSphere around the mesh.

mesh.geometry.computeBoundingSphere();

With this setup, I can now obtain the world position of the boundingSphere, which is also the world position of the mesh itself.

var vector = mesh.geometry.boundingSphere.center;

Hooray! 'vector' now holds the center world position in (x,y,z) coordinates for your mesh.

Just to be clear, 'mesh' refers to a THREE.Mesh object in this context.

Answer №2

Is it necessary to involve the matrix when you can simply move the object itself by the increment? I don't see why the matrix is needed in this scenario.

mesh.position.set(mesh.position.x +2, mesh.position.y + 2, mesh.position.z + 2);

On second thought, if using the matrix is essential, consider setting the matrixWorld. You may refer to , although utilizing the position setter should suffice for most cases.

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

I'm trying to figure out the best way to successfully pass a prop to another component in TypeScript without running into the frustrating issue of not being able to

I have been facing an issue while trying to pass a prop from a custom object I have defined. The structure of the object is as follows: export type CustomObjectType = { data?: DataObject }; export type DataObject = { id: number; name: stri ...

"Error: The chai testing method appears to be improperly defined and is not

I am trying to set up a Mocha and Chai test. Here is my class, myClass.js: export default class Myclass { constructor() {} sayhello() { return 'hello'; }; } Along with a test file, test.myclass.js: I am attempting to a ...

Node.js module retriever showing as blank

My current challenge involves the creation of a settings class with one getter. Here is how it currently looks: class Configuration { get endpoint() { return 'https://api.example.com'; } } module.exports.Configuration = Configuration; In a ...

It appears that THREE.js is experiencing issues when running on a local server

Recently I've started working with THREE and I've encountered some strange issues that I suspect are related to the localhost/same-origin-policy. Basically, I created a custom animation using armatures in Blender and exported it using the three. ...

When does the ng-disable function become activated?

Here's an example: <button ng-disabled="!isSomethingValid() || loading || disabled" ... class="btn btn-primary"> What determines the condition for the ng-disable attribute to evaluate its expression? ...

Use a spy to mock a component method using karma and jasmine

Encountering this error message during testing: ERROR: 'Error during cleanup of component' The issue stems from the following code snippet : ngOnDestroy(){ methodCallToMock() } To address this, I need to mock the methodCallToMock() functi ...

Solving issues with Angular4 Router changes

I'm attempting to chain the router resolver for my application. Below are my Router options: { path: '', component: AdminComponent, resolve: [ SessionResolve, LocaleResolve ] } The desired flow is to first call S ...

display and conceal elements and refresh action

Can anyone help me with a function to hide and show a div? function toggledDivVisibility(divName) { if (divName.is(':hidden')) { var hiddenDiv = document.getElementById("filter"); hiddenDiv.style.display = 'block&a ...

What is the alternative method of invoking a function within another function in React Native without utilizing a constructor?

Within my RegisterTaster function, I need to execute another function called endRegisterAlert. Since I'm not using a constructor and instead treating the class as a const in React Native, how can I achieve this? What is the best way to call the endRe ...

Changing the li tag by clicking on it is a simple task that can be easily

Whenever I click on a tag, I want the li class to change to "active" and open a new page with the corresponding tag as active. For example, if I click on the Overview tag, the new page should open with the li tag as active. I have attempted to write some c ...

What is the method for binding context on a click event in Vue?

Can't access context function on click in Vue Example HTML with click function: <li v-on:click="itemClick($event, this)"></li> My Vue.js code snippet: var app = new Vue({ el: '#mainApp', methods: { ite ...

The Threejs model may be anchored at coordinates (0, 0, 0), however, the scene rendering does not align with the specified coordinates

Today while working with Threejs, I faced an issue with the model position setting. Here is my code for loading a Gltf Model ...

Is there a way to identify when a Tween.js animation has completed?

Is there a completed or finished event available when using this code for animating the camera in a scene with tween.js? tween : function (target){ var position = camera.position; var tween = new TWEEN.Tween(p ...

Passing a variable to a different PHP script

I'm facing a dilemma and need some assistance. I'm encountering an issue where a PHP variable (or JavaScript/jQuery) is not being successfully transmitted to another PHP file through an AJAX request that I set up. To provide context, I am in the ...

Having troubles with the xml2json jQuery plugin's functionality?

I've implemented the jQuery XML to JSON Plugin by Fyneworks.com. Any idea why my code isn't functioning as expected? index.html <!DOCTYPE html> <html> <head> <script src="build/jquery.xml2json.js"></script> <sc ...

Tips for avoiding a form reload on onSubmit during unit testing with jasmine

I'm currently working on a unit test to ensure that a user can't submit a form until all fields have been filled out. The test itself is functioning correctly and passes, but the problem arises when the default behavior of form submission causes ...

What is the best way to repeatedly execute a function upon button click?

for (let index = 0; index < shoppingCenters.length; index++) { const mall = shoppingCenters[index]; locateAddress(mall); } $scope.locateAddress = function(mall) {} XHTML <ion-nav-buttons side="primary"> <button class="button" ng-click= ...

Importance of value attribute in <input ng-model ..>

Maybe a silly inquiry, but I'm curious about the purpose of having value="" in this particular situation: <input ng-model="something.name" value="" class="input-xlarge" /> Are there any alternatives to keeping the value attribute empty? I init ...

What could be causing SVG to not render in a NEXTJS project upon deployment on Vercel?

Recently, I created a standout single-page nextJS application that was beautifully styled with Tailwind CSS. One interesting feature I added was incorporating the background of a component called SectionWrapper as an svg file. To achieve this, I crafted a ...

Don't forget to retain the checkboxes that were chosen in the

I am looking for a solution to a specific scenario. When I open a modal box and check some checkboxes, I want those same checkboxes to be selected the next time I open it. Below is an example of my code. Main Controller modal function function openModa ...