Understanding the difference between the local and global locations of a three.js Object3D

Initially, I envisioned my small 'ship' with turrets that could track a target. This is the jfiddle I created to showcase it. You can view it here. However, when the turrets track, they behave strangely. I noticed that the turret seems to believe it is slightly off from its intended location (at the origin), as indicated by this piece of code:

turret_a.position.y = .25;
turret_a.position.z = 2;

The reason I set these values was to establish a relative position when adding the turret to the 'base ship', as shown in the following code snippet:

ship = new THREE.Object3D();
ship.add( ship_base );
ship.add( turret_a ) ;

Surprisingly, when I adjusted the position of turret_a after it was already added to the ship and the ship was included in the scene, the turret started to track in a way that aligned more closely with my vision.

My question pertains to why the lookAt() function appears to be using the previous location of the turret rather than its current position relative to its parent object when determining the necessary rotation angles. Why does this happen?

Answer №1

Upon examining the code for Object3D.lookA(), it becomes apparent that:

// This function does not accommodate objects with rotated and/or translated parent(s)

The code will function properly only if the parent ship is positioned at the origin and remains unrotated.

Check out the updated version here: http://jsfiddle.net/czGZF/4/

Using three.js r.59

Answer №2

According to the documentation on the website, when it comes to Camera objects, the lookAt() method utilizes world positions, just as you have observed. This approach seems to be a common and standard practice in handling such situations.

Even though I am not overly familiar with the three.js API, it seems that in order to obtain the global position of the ball, you can utilize the following code snippet:

var targetPos = ball.position.clone();
ball.localToWorld(targetPos);

By implementing the above code, you should be able to make progress towards achieving your desired outcome. However, it is worth noting that the fiddle you are working with appears to be quite unpredictable, which may complicate finding a complete solution in a timely manner.

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

The computed properties in Vue are not receiving any values when an array is passed using v-bind

Embarking on my Vue journey with a simple example, I am attempting to calculate the sum of items based on the provided data. If you wish to explore the complete example, it can be accessed in this jsffile. Here is the component structure: Vue.component(&a ...

Modify the hover color of <TextField /> within the createMuiTheme() function

Is there a way to change the borderColor on hover for the outlined <TextField /> Component within the createMuiTheme()? I have managed to do it easily for the underlined <Input /> export default createMuiTheme({ MuiInput: { &apo ...

Guide on resetting the scrollHeight values of DOM elements obtained using ClientFunction

I am currently using TestCafe to run tests in two separate fixtures and classes for different app pages. I have noticed that when I use the "ClientFunction" to access the "window.document" object in these tests, the values can vary depending on the executi ...

Filtering Sails.js query model based on a collection attribute

Imagine I have these models set up in Sails.js v0.10: Person.js module.exports = { attributes: { name: 'string', books: { collection: 'book', via: 'person' } } }; Book.js module.exports = { ...

Is there a way to modify AJAX response HTML and seamlessly proceed with replacement using jQuery?

I am working on an AJAX function that retrieves new HTML content and updates the form data in response.html. However, there is a specific attribute that needs to be modified before the replacement can occur. Despite my best efforts, I have been struggling ...

What is the best way to showcase information from multiple JSON files simultaneously?

Creating dynamic JSON URLs based on browser cookie data. var cookie = $.cookie('faved_posts'); The cookie contains IDs like 123456, 111111, 000001, etc. Now we need to request the JSON file for each ID in the cookie. We have: $.each(cookie, ...

Utilizing solely ThreeJS for crafting immersive user interfaces without the need for HTML DOM overlays is the optimal method to achieve

Is it possible to overlay a 2D layer for UI elements such as text and buttons over a 3D scene in ThreeJS? I envision something like incorporating the rendering engine from PixiJS into ThreeJS for added functionality. PixiJS has some 3D features, so combi ...

What are the steps to implement server side routing using react-router 2 and expressjs?

If you're looking to delve into server side rendering with react-router, the documentation linked here might fall short in providing a comprehensive understanding. In particular, it may not offer enough insights on how to leverage react-router 2 for r ...

Getting the width of children components in Reactjs can be achieved by using the

Check out this sample from the web: https://codesandbox.io/s/jovial-resonance-5mjq0 const Parent = styled.div` display: flex; width: 100%; background: yellow; justify-content: space-between; `; const Children = styled.div` display: flex; back ...

Would it be considered improper to implement an endless loop within a Vue.js instance for the purpose of continuously generating predictions with Tensorflow.js?

In my project, I am leveraging different technologies such as Tensorflow.js for training and predicting foods, Web API to access the webcam in my notebook, and Vue.js to create a simple web page. Within the addExample() method, there is an infinite loop r ...

Tips for resolving the problem when encountering the "undefined data" issue in the success callback of an AJAX request

I am facing an issue with my JSP page where I have a button that calls an Ajax method named "sendmail()" upon click. The send mail API is in the Controller, and I am trying to display an alert message in the Ajax success function using data.message, but it ...

Incorporating list items in a React component is not functioning as expected

When I console.log(this.props), here are my props: list:Array(1): {user: "Jack Nicholson", userid: "5b684ed8d3eb1972b6e04d32", socket: "1c0-Jb-kxe6kzPbPAAAD"} However, despite mapping through my list and using the component <UserItem user={user.user} ...

Using React JS to iterate over an array and generate a new div for every 3 items

It's a bit challenging for me to articulate my goal effectively. I have a JSON payload that looks like this: { "user": { "id": 4, "username": "3nematix2", "profile_pic": &qu ...

Is there a way to ensure that any updates made to the backend database are immediately visible in the browser?

Currently, I am facing an issue with my hardware scanner that is connected to a Windows computer. Whenever I scan an item using the hardware scanner, the Windows computer retrieves the price/information about that specific item. Then, I manually input this ...

Enhancing the appearance of individual cells in jQuery DataTables

While working with jQuery DataTables, I have successfully managed to access row data and apply styling to the entire row. Below is the code snippet used to initialize the datatable: var $dataTable = $('#example1').DataTable({ "data": data, ...

Learning how to track mouse wheel scrolling using jQuery

Is there a way to track mouse scrolling using jquery or javascript? I want the initial value to be 0 and increment by 1 when scrolling down and decrement by 1 when scrolling up. The value should always be positive. For example, if I scroll down twice, the ...

leveraging a callback function alongside the useState hook

I'm facing an issue with the change() function. My goal is to call the filteredData() function after the setState operation is completed. Typically, I would use a callback function for this task, but useState doesn't support callbacks. Using useE ...

Manipulating a React table: Customizing a row in the table

Below is the code snippet: const [data, setData] = useState([ {id: 1, name: 'paper', qty: 10}, {id: 2, name: 'bottle', qty: 10}, ]); const [isEditable, setEditable] = useState([]); useEffect(()=>{ data.map(each=>{ ...

How can I incorporate popups in React using mapbox-gl?

Currently utilizing mapbox-gl within React, everything seems to be functioning properly except for the integration of mapbox-gl's Popups. I have the function let Popup, but I am uncertain about how to incorporate it. renderMap() { if (this.props. ...

What is the best way to halt a jQuery function when hovering over it?

Currently, I have a jQuery function set up to run on setInterval(). However, I am looking for a way to pause the interval when hovering over the displayed div and then resume once no longer hovering (i.e., continue cycling through the divs). Does anyone ...