tags:

views:

62

answers:

2

i have an object that is doing a circular motion in a 3d space, the center or the circle is at x:0,y:0,z:0 the radius is a variable. i know where the object is on the circle (by its angle [lets call that ar] or by the distance it has moved). the circle can be tilted in all 3 directions, so i got three variables for angles, lets call them ax,ay and az. now i need to calculate where exactly the object is in space. i need its x,y and z coordinates.

float radius = someValue;
float ax = someValue;
float ay = someValue;
float az = someValue;
float ar = someValue; //this is representing the angle of the object on circle

//what i need to know
object.x = ?;
object.y = ?;
object.z = ?;
+3  A: 

Use a rotation matrix. Make sure you use a unit vector.

Marcelo Cantos
+4  A: 

You need to provide more information to get the exact formula. The answer depends on which order you apply your rotations, which direction you are rotating in, and what the starting orientation of your circle is. Also, it will be much easier to calculate the position of the object considering one rotation at a time.

So, where is your object if all rotations are 0?

Let's assume it's at (r,0,0).

The pseudo-code will be something like:

pos0 = (r,0,0)
pos1 = pos0, rotated around Z-axis by ar  (may not be Z-axis!)
pos2 = pos1, rotated around Z-axis by az
pos3 = pos2, rotated around Y-axis by ay
pos4 = pos3, rotated around X-axis by ax

pos4 will be the position of your object, if everything is set up right. If you have trouble setting it up, try keeping ax=ay=az=0 and worry about just ar, until your get that right. Then, start setting the other angles one at a time and updating your formula.

Each rotation can be performed with

x' = x * cos(angle) - y * sin(angle)
y' = y * cos(angle) + x * sin(angle)

This is rotation on the Z-axis. To rotate on the Y-axis, use z and x instead of x and y, etc. Also, note that angle is in radians here. You may need to make angle negative for some of the rotations (depending which direction ar, ax, ay, az are).

You can also accomplish this rotation with matrix multiplication, like Marcelo said, but that may be overkill for your project.

Tom Sirgedas
i didnt get what u wrote to work, thats why i answer so latebut its correct, i dont know what i did wrong. also what u wrote IS the matrix manipuilation, so its not overkill ;)i simplified it for my needsx = 0; y = 0; z = 0;// polar coordinates to cartesian coordinate (that already gives me 2 angles)x1 = r * sin(ax) * cos(ay)y1 = r * sin(ax) * cos(ay)z1 = r * cos(ax)// Rotation matrix around the last angley2 = y1 * cos(az) - z1 * sin(az)z2 = z1 * cos(az) + y1 * cos(az) so my values are now x1,y2 and z2last rotation is unnecessary (no more characters left to explain why:D)