How can we smoothly transition between old and new target positions in THREE.js using lookAt?

JSfiddle Demo

Using the THREE.js "lookAt" function, I am able to align my cone with each target sphere one by one (red, green, yellow, blue).

// Setup

c_geometry = new THREE.CylinderGeometry(3, 40, 120, 40, 10, false);
c_geometry.applyMatrix(new THREE.Matrix4().makeRotationX(Math.PI / 2));
c_material = new THREE.MeshNormalMaterial();
myCone = new THREE.Mesh(c_geometry, c_material);
scene.add(myCone);

// Execution (within the Animation loop)

myCone.lookAt(target.position); 

Now, my goal is to smoothly and gradually move the cone from the previous target to the new target. To achieve this, I believe I need to calculate intermediate points along a circular arc centered at the cone's position Cxyz, extending from the old target position Pxyz to the new target position Nxyz.

I am seeking guidance on: (a) tools, (b) trigonometry formulas, or (c) code samples that can help me calculate the xyz coordinates of these intermediate points on the arc. I will provide the angle increment between points based on the desired motion speed and frame rate.

Answer №1

To achieve a seamless transition between different orientations, it is recommended to pre-calculate the target quaternions in your scenario:

myCone.lookAt( s1.position ); 
q1 = new THREE.Quaternion().copy( myCone.quaternion );

myCone.lookAt( s2.position );
q2 = new THREE.Quaternion().copy( myCone.quaternion );

Subsequently, within your render loop:

myCone.quaternion.slerpQuaternions( q1, q2, time ); // 0 < time < 1

This code sample pertains to three.js version r.141

Answer №2

If you're seeking to smoothly move an object while keeping it looking at a target, here's a way to achieve that effect:

function MoveAndMaintainFocus(object: Object3D, targetPosition: Vector3, focusTarget: Vector3){
  const startPosition = object.position.clone();
  const initialFocus = new Vector3(
    0,
    .1, // Adjust as needed to prevent sudden camera rotations
    -object.position.distanceTo(focusTarget) // Note the orientation of THREE.Camera
  );
  object.localToWorld(initialFocus);
  const temporaryFocus = initialFocus.clone();
  function FocusLerp(progress: number){
    // Include this in your update loop
    object.position.lerpVectors(startPosition, targetPosition, progress);
    temporaryFocus.lerpVectors(initialFocus, focusTarget, progress);
    object.lookAt(temporaryFocus);
  }
}

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

How can we stop elements from suddenly moving around in a canvas?

Below is the pseudocode I have come up with: when the move button gets pressed: currentCirclePos = circleX; moveTimer = setInterval(move, 10); The move function implementation is as follows: var move = function() { circleX += a slight moveme ...

Deduct a digit from a variable

My goal is to decrease the value of revlength each time the loop runs. For example, if there are 2 posts by 'A Google user', then subtract 2 from revlength. This is my attempted solution: var revlength = place.reviews.length; if (place.reviews[ ...

Create waypoints on multiple elements

I am working on implementing a unique feature using a div tag with the class "dipper." <div class = "dipper"> <p>Peekaboo</p> </div> As part of this implementation, I have included a script that triggers the display of the "dipper ...

Activate UpdatePanel upon hovering the mouse cursor over it (displayed as a tooltip)

In order to provide users with additional information, such as a tooltip, on items within a RadioButtonList, I am looking to display about 500 - 600 characters of info. Currently, I am updating a PanelUpdate when a user selects an item in the RadioButton ...

Personalize headers in v-data-table while preserving default sorting capabilities

I'm looking to enhance my v-data-table by making the table headers "tab-able". To achieve this, I decided to create a slot and include tabindex on the columns. However, I encountered an issue where the sorting functionality stopped working. Does an ...

How come the parameters in my function are being displayed as boolean values in JSDocs when that is not the intended behavior?

I am documenting my journey of following the React tutorial for tic-tac-toe, and I'm puzzled as to why the parameters of my function are showing up as boolean values in JSDocs when they are not supposed to. When I hover over the function with my curs ...

Is there a way for me to automatically update a webpage every 5 minutes, beginning at 8:01 a.m., and subsequently at 8:06 a.m., and so forth?

<html> <head>Harshal</head> <script> var limit="5:0" var doctitle = document.title var parselimit=limit.split(":") parselimit=parselimit[0]*60+parselimit[1]*1 function beginrefresh(){ if (parselimit==1) window.location.reload ...

Tips for concealing an image hidden in the second TD using the data from the table stored in the first TD using JQuery

In my code, I have a 2-column table setup where the first column contains another table with 3 rows of content and the second column includes an image. Here is how it's structured: <table id="myTable" cellspacing="0" cellpadding="0"> < ...

Navigating to another page after submitting an AngularJS form

In order to enhance the user experience on my website, I have created an index page containing a form with a text field and a search button. The goal is for users to input their search query into the text field and upon clicking the search button, the form ...

What is the process for setting the version in a serverless project?

Recently I downgraded the serverless to version 1.38.0 using the command npm install -g <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="047761767261766861777744352a373c2a34">[email protected]</a>. This triggered in ...

Can TwitterBootstrap modal interrupt script execution?

I'm currently exploring how to implement a bootstrap modal on my webpage. My goal is to utilize ajax to submit a form, but before doing so, I'd like to prompt the user for additional input within the modal. After gathering this new input, I aim ...

What makes componentDidMount the ideal location to initiate AJAX requests?

It is quite common to see AJAX requests being fired at the componentDidMount hook in many React projects. However, I find it hard to understand this suggested practice. For example, consider a component where the AJAX request depends on a prop from the pa ...

Detecting the type of request in a Node Express server when handling JSON and HTML data

I am interested in determining whether an incoming request is a standard page load or if it is coming from an ajax request. My aim is to use the same controller for both scenarios, whether it be an ajax request or a normal page load. At present, I am uti ...

How is it possible that the fourth element in the array displays a length of 24 when all it contains is the word "Spain"?

One challenge I'm facing is comparing the 5th element of my array with the value of the object that has been clicked. Below is the code snippet I am currently using: var options = new Array(); $(".option").click(function compareReaction(name) { ...

Steps to update model in AngularJS when a key is pressed:

I'm having trouble implementing a keyboard layout in my AngularJS app. The goal is to have the displayed letters change to match the corresponding keys on the keyboard when the shift key is pressed. I've created a function in my controller to han ...

Injecting a component in Angular 2 using an HTML selector

When I tried to access a component created using a selector within some HTML, I misunderstood the hierarchical provider creation process. I thought providers would look for an existing instance and provide that when injected into another component. In my ...

JavaScript and Ajax are functioning properly in Mozilla Firefox, however there seem to be some compatibility issues with Google Chrome

I have a form that serves the dual purpose of registration and login, and I am using JavaScript Ajax to submit it. While it works smoothly in Mozilla Firefox, it fails in Chrome and IE. The goal is to execute an AJAX and PHP script that checks the databa ...

The callback function appears to malfunction when utilizing the getJSON function within jQuery

I have encountered an issue while using jQuery's getJSON function to import data and trigger a callback function. Surprisingly, the callback function does not run when using getJSON, but works fine when using the get function. Even more puzzling is th ...

Authenticating with passportjs using a Google Apps email address for verification

I am currently experimenting with using Passport.js along with a Google Apps email ID. I have successfully been able to authenticate using a gmail.com email ID, however, I am facing challenges when attempting to authenticate if the email ID is associated w ...

JavaScript libraries are not required when using the .append function to add HTML elements

Currently, I am attempting to utilize $.ajax in order to retrieve an html string from a php file and append it to the current html div. Oddly enough, when I use php echo, everything functions properly. However, when I attempt to load dynamically using $.lo ...