Dávid Szabó
Dávid Szabó

Reputation: 2247

3D Coordinates after rotating

I have 2 points: C and P

I want to rotate P around C in 3 dimensions.

How can i calculate the new position of P?

I have two angle: 'yaw' and 'pitch'.

Yaw moves P around C on the 'x' axis, so it moves it left+ and right- around the point.

Pitch moves P around C on the 'y' axis, so it moves it up+ and down- around the point.

Z, depth moves toward you in +, moves away from you in -

Of course i want to make it work with 'intermediate' positions too, like 45deg yaw and 45deg pitch.

Yaw and Pitch is given in DEG, then converted to RAD (before this code, so the code uses RAD)

p.x = c.x + Math.sin(yaw) * Math.sin(pitch);
p.y = c.y + Math.cos(yaw);
p.z = c.z + Math.cos(yaw) * Math.cos(pitch);

Imagine a Sphere and a Point, i want to move the Point on the Sphere's surface.

This sometimes work, sometimes not. What am i missing?

Sorry if this is a duplicate, i have no idea what should i search for.

Here is a fiddle about my problem: http://jsfiddle.net/Jm6Lt/2/

Blue sphere's position is calculated, white sphere is rotated around the red sphere's center.

Upvotes: 1

Views: 1475

Answers (1)

Lutz Lehmann
Lutz Lehmann

Reputation: 25972

What you wrote down are the spherical coordinates on a sphere of radius 1 around C.

Look up "rotation matrix" and "Givens rotation", these are the kind of operations that will rotate a given point. The type of operation that you are looking for will have the form

P'=C+R*(P-C), 

where R is a rotation matrix.


You got the spherical coordinates slightly wrong, they should be:

p.x = c.x + Math.sin(yaw) * Math.sin(pitch);
p.y = c.y + Math.cos(yaw);
p.z = c.z + Math.sin(yaw) * Math.cos(pitch);

Then the vector for (yaw, pitch)=(0,0) is (0,1,0) (before translation by C) and the coordinates describe its rotation, first in the y-z-plane around the x axis with angle yaw and after that around the y axis with angle pitch.

Upvotes: 1

Related Questions