Rotating an object in Three.js along the radius of a sphere

I want to position an object at the end of a sphere's radius based on its coordinates (x, y, z).

In traditional mathematical formulas for the coordinates of a point on a sphere, we use:

var x = radius * Math.cos(phi) * Math.cos(theta);
var y = radius * Math.cos(phi) * Math.sin(theta);
var z = radius * Math.cos(phi);

Instead, I am using:

var x = radius * Math.cos(angleZ) * Math.cos(angleX);
var y = radius * Math.cos(angleZ) * Math.sin(angleX);
var z = radius * Math.cos(angleZ);

I understand that these are not the correct angles. How can I relate these angles (angleX, angleY, angleZ) to phi and theta in order to transform them correctly?

In Three.js, I have:

cylinderX.rotation.x = degToRad(angleX); 
cylinderX.rotation.y = degToRad(angleY);                    
cylinderX.rotation.z = degToRad(angleZ);
  • degToRad - function that converts degrees to radians

Where:

angleX is the angle between the y plane and the z plane
angleY is the angle between the z plane and the x plane
angleZ is the angle between the x plane and the y plane

Phi and theta are different angles because theta is the angle between the radius and the plane.

When I run the animation, the position of the object (x, y, z) and its location on the sphere's plane are incorrect.

How can I solve this issue?

To be more specific, the code and example can be found here:

I want to place the green cube at the end of the red cuboid as the car rotates on all axes (x, y, z).

In the arrays: x[...], y[...], z[...], there are values of angles representing how the car (object) rotates on all axes (X, Y, Z).

I am unsure how to connect angleX, angleZ, angleY to "phi" and "theta" in order to correctly position the "green cube" based on a radius of 100, for example.

The full code and example can be found in the animate.js file below:

    var container, stats, camera, scene, renderer, object;          
    var mouseX = 0, mouseY = 0;

    var windowHalfX = window.innerWidth / 2;
    var windowHalfY = window.innerHeight / 2;

    var angleX=0, angleY=0, angleZ=0, G=0;
    var x;  var y; var z; 
    var times = [];

    x = ["0", "10", "20","30", "40", "50" ];                                
    y = ["0", "20", "40","60", "70", "80" ];    
    z = ["0", "30", "60","90", "120", "150"  ];     
    gX = ["1", "1.4", "0.7", "0.4", "1", "1.2", "0.5", "1.2", "1.4", "1.3", "1", "0.7" ];
    gY = ["1", "2", "1", "2", "1", "2", "3", "1.2", "1.4", "1.3", "1", "2" ];
    gZ = ["1", "2", "1", "2", "1", "2", "3", "1.2", "1.4", "1.3", "1", "2" ];   

    // more code here...

Answer №1

To easily position an object on the surface of a sphere, you can incorporate the object into a parent Object3D. Move the object a set distance (equal to the radius of the sphere) along one of the x, y, or z axes. Place the parent object in the center of the sphere you wish to match, ensuring the child object rests on the sphere where it intersects the chosen axis. By arranging your setup in this manner, you can manipulate the parent object to place your desired object accurately on the sphere's surface.

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

Tips for documenting curried functions using Js docs

I'm encountering issues while trying to use Js docs for generating static documentation of my project. When attempting to run the js doc command, I am faced with numerous parse errors specifically in areas where I have curried functions. Strangely, my ...

tips for generating a random number for direct display

Can anyone help me figure out how to automatically display the total of numbers from this script on my blog post without having to click anything? Additionally, I need it to update if the browser is refreshed. ` http://jsfiddle.net/BenedictLewis/xmPgR/ ...

I am encountering an issue where I am unable to send a HashMap String to a PHP server, and I am not receiving a JSONArray in the

How can I send a hashmap string to a PHP server without receiving back a JsonArray using Volley in Android? CM =$_POST['code_send']; $db =Db::getInstance(); $records = $db->query ("SELECT * FROM p_users WHERE c_meli='$CM'"); $js ...

I am having trouble with Fullcalendar not loading my JSON data to display events

I've been experimenting with the fullcalendar JavaScript library, but I'm struggling to load data from my MySQL database onto the calendar. When I test my db-connect.php file, it does return the first entry in the table, yet the calendar remains ...

The WHATWG URL API allows creation of a new URL using the new URL

I've been experimenting with node and attempting to create an instance of the URL class to utilize its useful properties. Here's what I tried: const { URL } = require('url'); (...) http.createServer((request,response) => { let u ...

Unable to make getJSON function properly with CodeIgniter

I'm experimenting with using getJSON to retrieve the most recent data from my database. So far, I've stored it in an array and used json_encode(the array). This method successfully displays the information on the view, but the problem lies in the ...

Enhancing the user experience of the dropdown component through improved

I have developed an accessibility feature for a dropdown component that dynamically populates values in the options menu only when the dropdown is opened. This means that it compiles the template on the fly and attaches it to the dropdown box. Dropdown HT ...

Interacting with a C# Web Service Using JQuery

I've set up a JSON web service in C# and I'm working on creating a custom HTML page to interact with it. http://localhost:25524/DBService.svc/json/db=TestDB/query=none When I input this URL into my browser, I expect to receive JSON formatted da ...

ng-class expressions are constantly evolving and adapting

Within a div, I am utilizing ng-class="{minifyClass: minifyCheck}". In the controller, I monitor changes in two parameters - $window.outerHeight and $('#eventModal > .modal-dialog').height(). If there are any changes, I trigger the minifyChan ...

Using Angular select asynchronously within a custom directive

Despite my efforts, I am struggling to get an angular select with async to work properly. It seems to be mostly working, but not entirely. Consider the controller below: $scope.stuff = {}; $scope.stuff.blah = "SOME_KEY"; External.list().then( function ...

Is it possible to incorporate Vue and Vuetify into an existing project that requires IE compatibility?

Currently in the process of enhancing a legacy project with new functionality. The front end is currently relying solely on jQuery for all the webpages. I have been tasked with adding another webpage and would like to incorporate Vuetify + Vue due to the i ...

Retrieving the Selector Value during a Change Event

Is there a way to retrieve the selector value in a change event? I attempted this approach: $("#frek_menonton_tv :input").change(function(){ $(this).selector; }); However, it only returns an empty string. Desired outcome: frek_menonton ...

Easily load events into a responsive calendar widget with w3widgets. No

When attempting to load events dynamically, I encountered an issue where the output was not displaying correctly. I used AJAX to retrieve data in the following format: var datalist = "2015-09-22":{} $(".responsive-calendar").responsiveCalendar({ eve ...

"Vue is throwing an error because it cannot set the property '$offlineStorage' on an undefined object. How can this issue be resolved

While working on my vue ionic app, I integrated the plugin available at https://github.com/filrak/vue-offline. However, upon installing the plugin, an error was encountered: vue-offline.js?bf4e:193 Uncaught TypeError: Cannot set property '$offlineStor ...

Discovering ways to examine a React tree, view the props and state of components on Internet Explorer 11

After installing the React Developer Tools extension for Firefox and Chrome, I have been able to successfully utilize them. However, my struggle lies in not being able to locate a similar extension for Internet Explorer 11. Unfortunately, my application i ...

Looking for a unique search object specifically designed for mongodb?

I am currently developing my first application using node.js and angular, and I have encountered a challenge that I am struggling to solve. Let's say I have a User Schema like this: User = { firstname: "Bryan", lastname: "Allan", email: "<a ...

Update class name in React component based on state change

My current setup involves the setting of active and class flags as shown below: constructor(props) { super(props); this.state = {'active': false, 'class': 'album'}; } handleClick(id) { if(this.state.active){ this.s ...

The browser message states: Variable $? Not Found

As a newcomer to javascript (jquery/json), I've created code to display a chart using CanvasJS with a php/json data fetching script. However, I'm facing an issue where the chart is not rendering when I integrate my code. Upon inspecting the brows ...

Unable to trigger dispatchEvent on an input element for the Tab key in Angular 5

In my pursuit of a solution to move from one input to another on the press of the Enter key, I came across various posts suggesting custom directives. However, I prefer a solution that works without having to implement a directive on every component. My a ...

Developing a quiz using jQuery to load and save quiz options

code: http://jsfiddle.net/HB8h9/7/ <div id="tab-2" class="tab-content"> <label for="tfq" title="Enter a true or false question"> Add a Multiple Choice Question </label> <br /> <textarea name ...