Attempting to figure out the implementation of quaternions for rotating a camera that is traveling along a trajectory to face a different directional vector

I am facing a challenge in smoothly rotating the camera without changing the y-vector of the camera direction. When using look at, the camera direction changes abruptly which is not ideal for me. I am looking for a smooth transition as the camera's direction changes. It appears that quaternions might be the solution to this problem based on my research.

The camera (this.object) is moving along a defined path (this.spline.points). The current location of the camera is represented by (thisx,thisy, thisz).

I have cc[i] as the direction vector pointing to where I want the camera to face. Previously, I was using lookat(cc[i]) but it resulted in a quick, instantaneous change of direction.

After reading some information, I attempted the code below which caused the screen to go black when the camera was supposed to move.

If anyone could guide me on whether I am on the right path and how to fix my code, it would be greatly appreciated.

Thank you

var thisx = this.object.matrixWorld.getPosition().x.toPrecision(3);
var thisy = this.object.matrixWorld.getPosition().y.toPrecision(3);
var thisz = this.object.matrixWorld.getPosition().z.toPrecision(3);
var i = 0;
do {
    var pathx = this.spline.points[i].x.toPrecision(3);
    var pathz = this.spline.points[i].z.toPrecision(3);

    if (thisx == pathx && thisz == pathz){

    this.object.useQuaternion = true;
    this.object.quaternion = new THREE.Quaternion(thisx, thisy, thisz, 1);
    var newvect;
    newvect.useQuaternion = true;
    newvect.quaternion = new THREE.Quaternion(thisx+cc[i].x, thisy+cc[i].y, thisz+cc[i].z, 1);

    var newQuaternion = new THREE.Quaternion();

    THREE.Quaternion.slerp(this.object.quaternion, newvect.quaternion, newQuaternion, 0.5);
    this.object.quaternion = newQuaternion;


     //this.object.lookAt( cc[i]);
            i = cc.length;
    } else i++;

} while(i < cc.length);

Answer №1

Calling this.object.useQuaternion = true is unnecessary as it is the default behavior. The current rotation can be found in this.object.quaternion, eliminating the need to generate it.

A different approach could be taken by constructing the rotation matrix from spline position, lookAt, and up vectors to create a path of quaternions as a preprocessing step:

var initial = this.spline.points[i].clone().normalize(); 
var target = cc[i].normalize();
var up = this.object.up.normalize();
var rotationMatrix = new THREE.Matrix4().lookAt(initial, target, up);

The quaternions can then be created from the rotation matrix:

var quaternionPath = [];
quaternionPath.push(new THREE.Quaternion().setFromRotationMatrix(rotationMatrix));

Once the path is established, the quaternions can be applied to the camera in the animation loop - assuming there are enough samples. Otherwise, slerp could be used to generate intermediate points.

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

Incorporate a pair of additional columns and showcase the outcome within a third column within an HTML

function = () => { var info1 = parseInt(document.getElementById("info1").value); var info2 = parseInt(document.getElementById("info2").value); var res = Number(info1.value + info2.value); var info3 = document.getElementById("info3"); ...

Tips on concealing a table upon opening an HTML page

Hello, I've implemented some JavaScript code that displays a table when hovering over a link and hides it when moving the mouse away. However, I'm facing an issue where I want the table to be hidden when the page first loads. Can anyone help me a ...

What is the reason for having to add my IP to the white-list daily?

As a beginner, I am delving into the world of back-end development with Node.js, Express.js, and Mongoose for educational purposes. My database is hosted on Atlas-Mongo DB. Initially, everything seemed to be running smoothly as I configured it with the fre ...

Uncovering the User's Browser Specifically for UC-Mini and Opera-Mini

I'm in need of a script that can identify if the user's browser is uc-mini or opera-mini. These particular browsers do not support the "transition" feature. Therefore, when this specific browser is detected, I would like to deactivate the "trans ...

determine the proportion of the item

My function is supposed to map an object to create new keys. The new key "percent" is meant to calculate the percentage of each value of the key "data". It should be calculated as the value divided by the sum of all values. However, for some reason, it&apo ...

The code for implementing the "Read More" feature is not functioning as intended

I have been experiencing an issue with the implementation of the "read more" feature on my website. Although the code seems to be functioning properly, it only works after pressing the read more button twice. This particular code is designed to detect the ...

Switch between using a dropdownlist and textbox depending on the selection in another dropdownlist

In the process of developing an application, I have implemented a country dropdown list and a state dropdown list. Here's the scenario: when a user selects a country, the states for that country will populate in the state dropdown list. However, the ...

Creating a linear video playback system

Having an issue with my code in Chrome where auto play is enabled, but the video won't play. I have a loop set up to play each video one after the other, but first things first - how do I get this video to start playing? If there's a pre-made so ...

There was an error that said: "Uncaught TypeError: Function.prototype.apply: Arguments list has wrong type

I keep encountering the error Uncaught TypeError: Function.prototype.apply: Arguments list has wrong type whenever I utilize the .apply() method but I'm unable to pinpoint the cause. You can review my code here. Upon loading jsfiddle, try clicking ne ...

When multiple instances are present, the functionality of dynamically generated jQuery functions ceases to operate effectively

I've developed a chat application similar to hangouts where clicking on a user generates the chat div. One feature I have is allowing users to press enter in a textarea to send text, but when multiple dynamically generated jQuery functions are present ...

Generate a smaller set of information by choosing specific columns within a two-dimensional JavaScript array

Apologies for the simplicity of my question and my limited knowledge in javascript. I am completely new to this language and need some guidance. I have an array of data and I want to extract a subset based on selected columns. Here is a snippet of the init ...

What is the best way to utilize a reduce function to eliminate the need for looping through an object in JavaScript?

Currently, my code looks like this: const weekdays = eachDay.map((day) => format(day, 'EEE')); let ret = { Su: '', Mo: '', Tu: '', We: '', Th: '', Fr: '', Sa: '' }; w ...

Launch a PowerPoint presentation in a separate tab on your web browser

If you have a link that leads to a .pdf file, it will open in a new tab like this: <a target="_blank" href="http://somePDF.pdf">Download</a> But what about a Powerpoint file, such as a .ppt? Is there a way to make it open in a new browser tab ...

The Angular JSONP feature is malfunctioning

I'm attempting to utilize a jsonp call with the following code, but it doesn't appear to be functioning as expected. Code var url = 'http://z:15957/Category/Categories?callback=JSON_CALLBACK'; $http.jsonp(url).success(function (data) ...

Refresh webpage based on conditions - JavaScript

Is there a way to automatically refresh a web page every 3 seconds, but also have the ability to stop the refreshing and trigger an alert message if certain form elements reach specific values? As shown in the image below: The batsmen's score will b ...

Which is better for 3D web development: three.js or libgdx?

While Libgdx has a primary focus on Android and is written in Java, it is also capable of running apps on the browser through webGL. Three.js, on the other hand, is an impressive 3D library built in JavaScript that utilizes WebGL (or 2D canvas) for rende ...

Tips on securely saving passwords using Greasemonkey

Created a custom userscript that prompts users to save their login credentials in order to avoid entering them repeatedly. Familiar with using localStorage.setItem() to store key-value pairs, but concerned about storing passwords in clear text. Seeking ad ...

Is there an issue with the way I've implemented my depth-first search algorithm?

After implementing a dfs search for an 8 puzzle game, I have encountered an issue where my stack keeps adding possible movements without decreasing to find an answer. It seems like the code is not working correctly as a dfs should be, and I'm seeking ...

Using FlatShading alongside a texture in three.js to achieve a minimalist flat color effect

Currently, I am working on creating a terrain using three.js and texturing it with a grass texture. I have applied FlatShading to give it a low poly look, but the texture is still visible on each face. I want to achieve a flat color on each face instead of ...

Challenges arise when building a package while importing all modules on external servers

While exploring Three.js within a React application, I have created an example that functions properly when tested locally; however, it fails to load when accessed remotely from static hosts like GitHub or Amazon S3. When I run my GitHub project locally, ...