Spinning a sphere in such a way that the selected point is aligned as a Vector facing the camera

I have been searching for a solution to this particular issue and have come across two responses that are either outdated or do not provide the necessary THREE related code.

question and question

My desired outcome is to have the clicked point facing the camera!

if (intersects.length > 0) {
    var point = intersects[0].point;
    //console.log(point);

    var a = new THREE.Vector3(0,0,1);
    var b = point.normalize();

    var rotationAxis = new THREE.Vector3(0, 0, 0);
    rotationAxis.crossVectors(b, a);
    var dot = a.dot(b);
    var angle = Math.acos(dot);

    var quaternion = new THREE.Quaternion();
    var dest = quaternion.setFromAxisAngle(rotationAxis, angle);
    sphere.setRotationFromQuaternion(dest);
}

UPDATE:

I have been attempting to translate the calculations from the first response into threejs, and while it's close, there seems to be a slight discrepancy in the rotation degrees. Could you help me pinpoint where the error lies?

UPDATED JSFIDDLE HERE

Initial position - 0,0,1 https://i.sstatic.net/yAbR4.png

After clicking on a point https://i.sstatic.net/51zOt.png

Answer №1

To solve your problem, you must determine a rotation that will transition you from the center-to-clicked-point direction to the center-to-camera direction. You can find an example implementation here: Quaternion rotation calculation for changing vectors

    var a = new THREE.Vector3(0, 0, 1);             
    var b = continent.position.clone().normalize();
    earthContainer.localToWorld(b.clone()));

    var qtn = new THREE.Quaternion();
    qtn.setFromUnitVectors(b, a);
    earthContainer.setRotationFromQuaternion(qtn);

Interestingly, THREE.js includes a built-in method for this with qtn.setFromUnitVectors(u, v);. I made some adjustments to your code snippet in the following jsfiddle http://jsfiddle.net/abcd1234/, which closely resembles the initial answer you referenced. However, there seems to be a slight discrepancy in the functionality (clicking right causes sphere to rotate left, etc.) without a clear explanation.

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 functionality of the custom file upload button is experiencing issues on Microsoft Edge

I've been working on creating a unique custom image upload button that functions perfectly in Chrome, Firefox, and Opera based on my testing. However, I'm facing an issue where it doesn't work properly in Microsoft Edge. Feel free to check ...

Is the user currently accessing the website in multiple tabs?

I am currently working on detecting the online status of users and need to update it when the tab is closed. The challenge I am facing is determining if the user has multiple tabs open so that the status remains the same, only updating when there are no ...

JavaScript: Converting an Array to a String

Having trouble making the string method work with an array - it keeps showing up as empty. let url = "https://api.myjson.com/bins/be7fc" let data =[]; fetch(url) .then(response => response.json()) .then(result => data.push(r ...

utilizing a spacemouse in conjunction with autodesk forge

Recently, I acquired a 3dconnexion spacemouse and have been attempting to configure it to work with the forge viewer. Fortunately, I came across some JS samples in the SDK for three.js that worked flawlessly. It seems both forge and three.js utilize webgl ...

the process of altering properties in vue js

After running my Vue file, I encountered the following console error. As someone new to Vue programming, I'm attempting to utilize a Syncfusion UI component to display a grid. Prop being mutated: "hierarchyPrintMode" I am unsure where to add the comp ...

Click on ng-click to sort through the blog entries

Currently, I am in the process of developing a blog utilizing AngularJS and JSON. Nearly everything is functioning as expected except for one particular filter item. As I am relatively new to Angular, it presents a bit of a challenge for me. The issue ari ...

Dynamic Express Route Handler

I have an array: var arr = ["/index.html", "/alternative_index.html", "/index"] and I am trying to make the Express server return the same output for these different routes: localhost:8080/index.html localhost:8080/alternative_index.html localhost:8080/ ...

What are the reasons behind the failure of this regex matching in Angular specifically on Safari browser?

In my angular project, I have the following code. It works perfectly fine in Chrome and Firefox, but in Safari, it throws an exception. var shour = "9:00:00 PM CDT"; var ehour = "12:00:00 AM CDT"; var conver_shour = shour.match(/^(\d+):(\d+)/)[ ...

Is there a way to incorporate CSS or tables when converting HTML to PDF using JavaScript?

While working on a project, I successfully converted an HTML file into a PDF. However, the output did not display the CSS design. Can anyone provide suggestions on how to include CSS design in the PDF file? Below is the JavaScript function code: $(funct ...

Searching for dates within a range on DataTables using CodeIgniter

Can someone assist me in displaying data within a selected date range? Here is the code snippet I have so far: Below is the code for displaying the data tables: View <div class="box-body"> <div class="table-responsive"> ...

How can I display a timer icon in front of text on a Material-UI CardHeader subtitle?

I have a question regarding displaying time in my posts. Currently, I am showing the time as 'a few seconds ago', '2mins ago', 'an hour ago', etc. However, I would like to include a clock icon before this string. Although I a ...

The onload function on the iframe is triggering twice in Internet Explorer 11

I am encountering a strange issue with an iframe in HTML that has an onload function. When using IE11, the onload function is being triggered twice, whereas it works fine in Chrome. Here is the HTML code: <iframe src="someurl" onload="someFunction( ...

There was an unforeseen conclusion to the JSON input while attempting to parse it

I am having an issue with the code in my get method. Can someone please help me troubleshoot and provide some guidance on how to solve it? app.get("/",function(req,res) { const url = "https://jsonplaceholder.typicode.com/users" ...

Adjust the size of the event bar on the FullCalendar resourceTimeline view

In my implementation of FullCalendar v6, I am displaying 6 months worth of data in the calendar, with each slot representing a one-month period. The issue I am facing is that when creating an event spanning 10 days, it currently takes up the entire width o ...

Navigate to a specific URL path and send properties as arguments in the function for handling events

I am working on a vuetify autocomplete search feature. When a user selects an item, I need to navigate to a specific route and pass some props along. However, my attempts to change the current route without passing props have resulted in errors. Here is w ...

How can I display all categories in a Radar chart using amcharts 5

I'm currently using React with amcharts to generate a Radar chart. The data I have is structured as an array of objects like this: { category: 'Something', value: 5 }. There are a total of 12 items in the data set. However, when plotting t ...

How can I modify my for loop with if else statements so that it doesn't just return the result of the last iteration in

Today is my first day learning JavaScript, and I have been researching closures online. However, I am struggling to understand how to use closures in the code I have written: function writeit() { var tbox = document.getElementById('a_tbox'). ...

When attempting to access front-end SPA links through the browser, the back-end triggers an error due to not being

My front-end single-page-application is built using JS (ReactJS), and the back-end is running on Phoenix (Elixir). Everything functions smoothly within the application, but I encounter a route error from Phoenix when attempting to access a page in the SPA ...

When trying to use express, the error message "d3.scale is

Having trouble integrating a c3-chart into my web application using node.js and express. The c3.js file is throwing the error message: TypeError: d3.scale is undefined Here is an excerpt from my layout.yade file: doctype html html head title= titl ...

The function findOne from Mongoose seems to be non-existent, all while utilizing the Passport library

Whenever I try to implement my local strategy using the passport lib, I keep encountering this error. TypeError: UserModel.findOne is not a function I've spent hours searching for a solution that addresses this issue but haven't been successful ...