elaverick's picture

3D coordinates given angles and distance

This isn't strictly an OpenTK question so I apologise for posting here but my lack of maths knowledge has tripped me up again.

I'm trying to extend my turtle drawing system into 3 dimensions, however I don't know how to calculate the resulting X,Y & Z co-ordinates given the appropriate rotations. I can manage it fine in 2D space but 3 dimensions is proving to be a bit beyond me.

2D version is as follows:

float cos = (float)Math.Cos(util.DegreeToRadian(turtleAngles.Z));
float sin = (float)Math.Sin(util.DegreeToRadian(turtleAngles.Z));
 
turtlePosition.X += cos * distance;
turtlePosition.Y += sin * distance;

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
puklaus's picture

I use this kind of method:

Vector3 Position;
Vector3 Rotation=new Vector(0,0,1);
 
public void Move(float spd)
{
            Vector3 rotation = Rotation * MathExt.DegToRad;
            Position.X -= (float)Math.Sin(rotation.Y) * (float)Math.Cos(-rotation.X) * spd;
            Position.Y -= (float)Math.Sin(-rotation.X) * spd;
            Position.Z -= (float)Math.Cos(rotation.Y) * (float)Math.Cos(-rotation.X) * spd;
}
ERP's picture

It's basically the same, but the sin and cos affect the different coordinate pairs.

HOWEVER - it won't work the way you expect, in 3D the order of the rotations matters, i.e. if you rotate by X then Y then Z you end up with a completely different position than if you rotate by Z then Y then X. There are also issues with what are termed gimble locks using XYZ rotations in 3D, the existence of gimble locks is why a lot of 3D software uses quaternions, but they son't translate well to your application.

Before you do anything you need to pick a convention, what does the rotation actually mean, i.e. which axis is it rotating around. At that point you need to do the math to generate the axis and I'd personally just use GL.Rotate.