Rotation to xyz coordinates

Hello,

note: I know my issue is more of a mathematical problem than a threejs problem, but I would appreciate the help.

I have this sphere, the blue pins were placed by rotation only (the center is at 0,0,0 and I just rotate along the Y-axis and the Z-axis), the pin (p1) starts at rotation (y=0, z=0), the angles between them are 45 degree. The red spheres are placed using location (XYZ ) the issue is here.

As you can see the sphere of p4 is misplaced, I’m using this method for converting rotation to coordinates:

const rad = Math.PI / 180;
const lat = 45 * rad; // here I change the angle from 45 to what ever I want
const lon = 45 * rad; // same here

// 5 is the radius
const x_ = 5 * Math.cos(lat) * Math.cos(lon);
const y_ = 5 * Math.cos(lat) * Math.sin(lon);
const z_ = 5 * Math.sin(lat);

p4.position.set(x_,y_,z_);
//is this method correct?

p1 → (0, 0) … correct
p2 → (0,45) … correct
p1 → (45,0) … correct
p1 → (45,45) … wrong

My problem boils to is how do I get the XYZ position of an object with YZ rotation only?
Thanks

Is the using of Spherical an option?

@prisoner849
just for the quick replay, I have no idea what this is, and I’m looking it up right now!

just for clarification, the blue pin model’s pivot located at 0,0,0 and the geometry lay flat at the X-axis.
to move the pin I just use:

pin.rotation.set(0, yR, zR); // where yR and Zr are the angles in radians 

@prisoner849 ok, so Spherical uses the Spherical coordinate system. to be honest, I have no idea how this can help me … I went through the documentation and I didn’t see anything useful! Am I missing something? can it returns XYZ coordinate?

You can set Vector3 from that object: three.js docs
Or use these coords: three.js docs

Edit:

I managed to resolve the manual calculation of coordinates (with sin and cos). However, the exact formula set for conversion from spherical to Cartesian coordinates depends on how axes are chosen. So, @Asfan, could draw how your X, Y and Z axes are, as well as what are your lat and lon angles (where is their 0 and what is their positive direction).

1 Like

@prisoner849 what do you mean by set vector3? I am looking for vector3 aka XYZ, I have (r,θ,φ) … sorry if I didn’t understand you nor the docs!

@PavelBoytchev I’ve seen your first replay and it might have something to do with it as I’m trying to align the wrong orientation order with the formal or set that I’ve used!

Vector3 is a set of coords for x, y and z.
.setFromSphericalCoords(radius, phi, theta); does all the job for you for casting the spherical coordinates into a vector of x, y, z :thinking:

2 Likes

I tried to use your orientation of axes. In order to fix the positions, I had to replace Y and Z coordinates of the points. See lines 91-92. This is because in Three.js (by default) the Y axis points up.

Here is with swapped YZ coordinates:

With your original order, I get the same result as you:

3 Likes

ooo oo I get it now, I thought the Spherical has a method that calculates xyz and I was missing it somehow! thank you. I’ll try it instead of using manual calculation and see if that can fix my issue.

Though as @PavelBoytchev said, I might have a problem with the rotation order!

Tried it with spherical coords: https://codepen.io/prisoner849/pen/gOdxMWM?editors=0010

4 Likes

@PavelBoytchev @prisoner849 thank you all for the help <3

1 Like