What is the best way to implement TrackballControls with a dynamic target?

Is there a way to implement the three.js script TrackballControls on a moving object while still allowing the camera to zoom and rotate? For example, I want to track a moving planet with the camera while giving the user the freedom to zoom in and rotate around the planet. A basic jsfiddle example can be found here:

http://jsfiddle.net/mareid/8egUM/3/

(Please note that mouse control may not work correctly on jsfiddle).

I want to attach the camera to a red sphere so it moves with it, but still maintain the ability to zoom and rotate. I attempted to make the camera follow the sphere by adding certain lines to the render() function:

mouseControls.target = new THREE.Vector3(object.position);
camera.position.set(object.position.x,object.position.y,object.position.z+20);

However, this method interferes with the zoom and rotation functionalities of TrackballControls. I believe it should be possible to adjust the TrackballControls calculations to revolve around the center of the red sphere mathematically, but I'm struggling to achieve this. I have experimented with various vector additions to the position of the red sphere in the TrackballControls.js file with no success.

Any advice or assistance on this matter would be greatly appreciated. Thank you!

Answer №1

If I were to suggest a control option, I would lean towards using OrbitControls over TrackballControls. While my recommendation is primarily focused on OrbitControls, the same concept may also be applicable to TrackballControls (though I have not reviewed its code recently).

When examining how OrbitControls functions, you will notice that it utilizes this.object for the camera and this.target for the camera's focal point. The calculations involve the vector (the difference between the two) for some operations. During panning, movement is applied to both positions, whereas only the camera is relocated during rotation. While this.pan is adjusted to shift the camera, it solely handles panning perpendicular to the vector from this.object to this.target due to the manner in which the panning vector is defined.

Nevertheless, if you subtract the vector from object.position to controls.target and assign it to controls.pan, it should transition to that object:

Revised example code:

Incorporate a function into OrbitControls.js that has access to its local pan variable:

function navigateTo( vector ) {
    // Create a copy of the provided vector to prevent modifications
    pan.add( vector.clone().sub( this.target ) );
}

Your customized code:

function updateAnimation() {

    requestAnimationFrame(updateAnimation);
    mouseControls.navigateTo( object.position ); // Utilize the new function
    mouseControls.update();
    render();

}

You may also wish to toggle mouseControls.noPan to true.

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

Issue with refreshing a material

When updating a transaction, I am encountering the issue of inadvertently deleting other transactions. My goal is to update only one transaction. Can someone review my backend logic to identify the root cause? Schema Details: const mongoose = require(&apo ...

Navigating through various locators with Protractor to fill an array

Currently utilizing protractor, my aim is to iterate through a collection of locators (each with unique identifiers) in order to retrieve the displayed text and then compare it against an array of expected values. I have encountered examples similar to th ...

Is there a way to determine the negative horizontal shift of text within an HTML input element when it exceeds the horizontal boundaries?

On my website, I have a standard HTML input field for text input. To track the horizontal position of a specific word continuously, I have implemented a method using an invisible <span> element to display the content of the input field and then utili ...

A-Frame: Struggling to accurately update the world position of a child object within the scene

Within my current virtual reality scene setup, I am working with an entity that has laser controls and a raycaster structured as follows: <a-scene> <a-entity laser-controls raycaster ...> <!--<a-entity id="inventory" ...> Coul ...

The request to http://localhost:5000/error resulted in a 404 (Not Found) error message. I encountered difficulties connecting

When attempting to retrieve information about a product from Amazon using their productId with Express.js and ScraperAPI, an error is encountered: Error message: "{name: "RequestError", message: "Error: Invalid URI "${baseUrl}&url=https://www.amazon.com/d ...

problem when trying to establish the minimum height for a div element prior to the website being fully loaded, considering the

Having trouble with a CSS issue that seems simple but I can't find a solution for. I have a main div with a min-height set to a certain value, specified in %. Since there is no outer div, the min-height won't display if it's given in px. I ...

Directives for conditions if Internet Explorer is greater than or equal to 9, or if it is not

Embarking on a new project, I find myself in the unfortunate position of having to support IE7 & IE9. However, my goal is to eventually phase out IE7 support smoothly. Ideally, I aim to utilize the latest versions of libraries wherever possible. < ...

Converting (Geo)JSON data into an array of objects

I am new to JavaScript and facing some difficulties in converting GeoJSON to a JavaScript Object Array. Using JSON.parse, I successfully parse the JSON generated on the server into a JSON Object. The addGeoJson method from Google returns [object (Array)] ...

Elevate the value within a function and refresh the said function

I'm currently facing a challenge with this particular piece of code, let spin = new TimelineMax(); spin.to($('.particle'), 150, { rotation: 360, repeat: -1, transformOrigin: '50% 50%', ease: Linear.easeNone }); Th ...

Leveraging the Wikia API

I've been attempting to utilize the X-men API on Wikia in order to gather the name and image of each character for use in a single page application using JavaScript. You can find the link to the wiki page here: Despite my efforts, I am struggling to ...

Transitioning to Firebase Authentication

I made the decision to transition away from firebase authentication. To accomplish this, I exported all firebase users along with their email addresses, hashed passwords, salt keys, and other necessary information. Next, I transferred them to a database a ...

Tips for concealing the URL in the address bar while using `<a href>` links

I have a variety of documents saved in a folder within an ASP MVC 5 project. Instead of directly linking to the document with an HTML tag, I am utilizing the following ng-href: <a ng-href="~/download/document/{{vm.document}}"></a> By using th ...

JavaScript function issue with automatic tabbing

Encountered an issue while utilizing this in our asp.net application. The main goal is as follows: When in a textbox -> tab once MaxLength is reached When in a checkbox -> tab once the control is toggled with the keyboard (spacebar) Other than bu ...

Using the Facebook marketing API requires a valid Instagram account ID to be provided as a parameter

I've been exploring the capabilities of the Facebook Marketing API once again. After successfully creating Facebook ads using my Node.js app, I now have my sights set on Instagram. My current call to create an AdCreative looks like this: fb.api(&ap ...

Creating a Form with a Custom Format in AngularJS

Just starting out with AngularJS and currently working with ACTIVITI. I'm looking to create a form in a specific structure where each response follows this format: { "taskId" : "5", "properties" : [ { "id" : "room", ...

Running concurrent codes is not possible in Node.js serialport

I am attempting to stream data from the MSP432 xds110 in order to display it on my website in real-time. To achieve this, I have opted to utilize the node.js serialport library for data retrieval. You can find more information about serialport here: In m ...

What is the best way to show the user profile on a Forum?

I am struggling to figure out how to display the username of a user on my forum page. I currently only have access to the user's ID and need help in extracting their name instead. It seems that I lack knowledge about mongoose and could really benefit ...

Order of Rendering GLTF in THREE.js

Consider these two code snippets - both versions of three, 0.113.2 and the latest 0.147.0. Make sure to adjust the camera for a clear view of the issue. Both codes are importing a GLTF file, but the rendering looks incorrect in 0.147.0. It seems that the ...

Issue with text input field causing the Enter key to not create a new line

In the example above, the text is placed in the middle of the text area. Here is the CSS code : .form-control { height: 300px; display: block; width: 100%; padding: 0.375rem 0.75rem; font-size: 1rem; font-weight: 400; line-heig ...

Utilizing Javascript and CSS for horizontal alignment from left to right

I have a JavaScript function that generates a list of store locations and creates a DIV beneath the map. Currently, the items are displayed top to bottom on the page. However, I would like to change the layout so that the items are shown as 3 in a row fro ...