`Shifting a spherical object from point A to point B along its axis`

I am currently working on a project that involves rotating a sphere from point A to point B on itself. After finding Unity3d code for this, I came across the following solution:

Quaternion rot = Quaternion.FromToRotation (pointA, pointB);
sphere.transform.rotation *= rot; //Multiplying rotations gives the final rotation

I found this information on , but now I am struggling to implement it in Three.js.

This is what I have tried so far:

var s = sphere mesh
var va = 1st start vector
var vb = 2nd end vector;

var qa = new THREE.Quaternion(va.x, va.y, va.z, 1);
var qb = new THREE.Quaternion(vb.x, vb.y, vb.z, 1);
var qc = new THREE.Quaternion();

THREE.Quaternion.slerp(qa, qb, qc, 1);

s.useQuaternion = true;
s.quaternion = qc;

Any help or guidance on this would be greatly appreciated. Thank you!

Answer №1

Imagine the sphere being perfectly centered at its origin with vector A</> and <code>B both normalized to unit length. Calculate the cross product of C = A×B. This resultant vector will serve as your rotation axis. The angle for your rotation can be found using

θ = cos<sup>-1</sup>(A∙B)
, where A∙B is the dot product of the unit vectors A and B. Remember that the angle measurement here is typically in radians, not degrees.

If the sphere isn't exactly at the origin or if its radius differs from unity, you'll need to normalize and adjust vectors A and B before determining the rotation. Below illustrates this:

A' ← (A-P)/|A-P|     // Normalized version of A
B' ← (B-P)/|B-P|     // Normalized version of B
V ← A'×B'            // Axis around which system rotates
θ ← cos<sup>-1</sup>(A'∙B')      // Angle of rotation

Now, utilize V and θ as your inputs for constructing the quaternion needed to define your rotation. Note that this rotation will be centered at the origin. Prior to applying it, make sure to first translate by -P, then execute the rotation, and finally shift back by P.

A quick note: There may be a potential sign error present. If things aren't working out, it could be due to the cross product's sign not aligning correctly with the dot product's sign. To resolve this, simply reverse the order of arguments in the cross product.

Answer №2

let angleX = group.rotation.y;
let angleY = -longitude * (Math.PI / 180) % (2 * Math.PI);
let angleZ = Math.PI / 2 * -1;
group.rotation.y = angleX % (2 * Math.PI);
group.rotation.x = latitude * (Math.PI / 180) % Math.PI;
group.rotation.y = angleY + angleZ;

where latitude=a, longitude=b, group=Object3D(or sphere)

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

Having trouble getting Angular 2 animations to fade in

I've been trying to figure out how to make the html fadeIn for hours. Every time ngFor displays it, the opacity stays at 1 immediately, without fading in. FadingOut works fine, but the fadeIn effect is not working as expected. @Component({ selector:& ...

How to eliminate a particular item within a string only in rows that include a different specific item using JavaScript

Is my question clear? Here's an example to illustrate: let string = "Test text ab/c test Test t/est ### Test/ Test t/test Test test" I want to remove / only from the line that includes ###, like so: Test text ab/c test Test t/est ### Test T ...

Send the output of an asynchronous ajax request to the designated function within the jQuery Validation plugin

Utilizing the jQuery Validation plugin (JVP) for server-side validations via ajax has been seamless, except for one hitch: JVP struggles with performing remote validation on blank fields. While it's easy to use its default required: true rule for sim ...

The requested Javascript function could not be found

I have the following JavaScript function that creates a button element with a click event attached to it. function Button(id, url, blockMsg){ var id = id; var url = url; var blockMsg = blockMsg; var message; this.getId = function(){ return id; }; th ...

The jQuery functions properly offline, but fails to load when connected to the

I am encountering an issue with the following script: <script type="text/javascript"> $.getJSON("http://api.twitter.com/1/statuses/user_timeline/koawatea.json?count=1&include_rts=1&callback=?", function(data) { $("#headertweet") ...

Are you looking to load pictures using jQuery?

I am currently using a ul li menu setup as follows: <ul> <li><a href="#1">Image #1</a></li> <li><a href="#2">Image #2</a></li> <li><a href="#3">Image #3</a></li> ...

What is the best way to implement the populate method in Node.js when working with MongoDB?

Here is the code snippet I am working with: const Order = require('../../models/order'); const Product = require('../../models/product'); Order.find({}, '_id product quantity', function(err, result) { if (result) { con ...

Vue function displays 'undefined' message

Although many posts on this topic exist already, I am struggling to understand what is going wrong (even after searching extensively here and on Google). I have created my interceptor like this, but I keep receiving an error message stating "This is undef ...

What techniques can be used to avoid the MUI `DataGrid` from constantly re-rendering when a row is committed?

Check it out here to see how the MUI documentation implemented it: <b>const</b> rows = [/* Row Data */] <DataGrid rows={rows} {/* Other Props */} /> <sup>/*[1]*/</sup> The approach taken by MUI is quite impressive. It streaml ...

Tips for identifying MIME type errors in an Angular 9 application and receiving alerts

While working on my Angular app, I encountered the MIME type error Failed to load module script: The server responded with a non-javascript mime type of text/html. Fortunately, I was able to resolve it. Now, I'm stuck trying to figure out how to rece ...

I am looking for a tool that can extract XML data from a remote URL and convert it into JSON format for easy retrieval using JavaScript

Grabbing XML directly from your own domain's local URL is simple, but doing so cross-domain can be more challenging. How can you retrieve the XML data located at using javascript? ...

What could be causing Vuejs to not update elements promptly?

Currently, I am encountering a scenario where I am adding options to a select element using Vue.js when the @change event of that specific element is triggered. An issue arises where the new option is not 'registered' until I exit the function. ...

Using JavaScript to Transmit URL

Imagine I have a URL similar to this: http://localhost:8000/intranet/users/view?user_id=8823 All I aim to achieve is to extract the value from the URL using JavaScript, specifically the user_id (which is 8823 in this instance), and transmit it through an ...

React: Despite my efforts to return a value from render, nothing was actually returned

My current project involves creating nested components to display the dataset I have. export const MyComponent = (props) => { const groupMilestoneByYear = (data) => { // Take Milestone Data array as input and group it by Year let yearGroup ...

verification tool for a less file's syntax

Searching for a tool to validate a specific less file at the syntax level. The issue lies in the validator not recognizing dependencies or detecting declared mixins. Many existing less processors do not work due to missing dependencies that cannot be prov ...

Reduce the amount of ajax calls

Currently, I have implemented checkboxes that serve as search filters on a website. Every time a user checks a filter box, an ajax request is triggered to fetch data from the server and display it. The issue arises when users select multiple checkboxes in ...

How to Use Javascript to Listen for Events on a Different Web Page

Is it possible to open a popup using window.open and then subscribe to page events (such as onload) of the popup from the opener? I am looking for a method in my parent page (opener) that will execute when the popup's onload or ready event fires. Can ...

Error updating model: The `teams` model cannot be modified once it has been compiled

After spending hours researching on StackOverflow, I came across numerous articles related to my issue. However, I am still unable to identify what mistake I have made here. Here is my code: models/Team.js const mongoose = require('mongoose'); ...

Exploring the Integration of jQuery AJAX in a Contact Form

I would like to incorporate AJAX functionality into a contact form. Here is the current code I have... $("#contact_form").validate({ meta: "validate", submitHandler: function (form) { $('#contact_form').hide(); ...

Error: Authorization requires both data and salt arguments

As a novice in NodeJS, I attempted to create an authentication form using NodeJS + express. The issue I am facing is regarding password validation - specifically, when "confirmpassword" does not match "password", it should return nothing. Despite my effo ...