# Simple 3D Math help

I dont think this is the correct board but theres no maths board so I'll post to the most related forum. Apologies.

I'm trying to move an object through 3D space relative to its Rotation, Pitch, and Yaw. I dont have a compiler at hand but I'm writting code and need to know if this is right. x and z being the plane, and y being vertical. I'm not exactly and expert and 3D math so I'm kind of half guessing here.

x += (Amount * sinf( Rot ));
z += (Amount * cosf( Rot ));

z += (Amount * sinf( Pitch ));
y += (Amount * cosf( Pitch ));

y += (Amount * sinf( Yaw ));
x += (Amount * cosf( Yaw ));

I'm not sure I even used sin and cos on the correct dimensions. I know Rot is correct, but thats only for rot... If thats not all right, could someone tell me what is?

Thanks a million.

• In your code I see that if (for example) Rot=0, Pitch=PI/2,
Z will be increased by 2 Amounts (If you want this my comments are useless).

As I understood from your source,
you want to move the object along one direction
(which vector is defined by Rot, Pitch & Yaw).
So in this case it would be better to calculate
direction vector and then add it to the object's position.

You can perform 3 times of rotation on initial vector, say (0;0;1)
(X=0; Y=0; Z=1). So then you'll have direction (the length of vector
must be equal to 1) and that vector multiply by Amount.

Rotation of point is:
X'=X*cos(a)-Y*sin(a)
Y'=X*sin(a)+Y*cos(a)
where is 'a' - angle of rotation.
Usually for the above task first performing rotation of point
around Z axis, then X and finally Y (cinema camera rotation

• : As I understood from your source,
: you want to move the object along one direction
: (which vector is defined by Rot, Pitch & Yaw).
: So in this case it would be better to calculate
: direction vector and then add it to the object's position.

: You can perform 3 times of rotation on initial vector, say (0;0;1)
: (X=0; Y=0; Z=1). So then you'll have direction (the length of vector
: must be equal to 1) and that vector multiply by Amount.
:
: Rotation of point is:
: X'=X*cos(a)-Y*sin(a)
: Y'=X*sin(a)+Y*cos(a)
: where is 'a' - angle of rotation.
: Usually for the above task first performing rotation of point
: around Z axis, then X and finally Y (cinema camera rotation
: or human's head).

Thank you for your reply. I hate to bother you further, but I dont have a compiler available and my grasp of the math is poor. I think I understand what you explained, but I'm not 100% sure, and have no way to calculate it. I feel stupid asking someone to get this specific, but I'm in a tight spot.

My function is in a format as follows:

void Move(float x, float y, float z, float Rot, float Pitch, float Yaw, float Amount)
{
}

Would it be rude of me to ask you if you could show me the exact code that would be used for that function? I've never asked for complete code before and I feel a little bad doing so.

Thank you for helping.
• Hello,
Well, first of all I'd like to say, that for the above task you need only 2 axises
(if you only need move object): X and Y.
Here you are...

void Rotate2D(float *x,float *y,float a) //<- this is from my source, 100% reliability.
{ // it will rotate the point CCW by 'a' radians.
float xx,yy;
xx=*x*cosf(a)-*y*sinf(a);
yy=*x*sinf(a)+*y*cosf(a);

*x=xx;*y=yy;
};

// But this function is better for objects moving, not view's camera (else you are to
// change the order)
// As you need only 1 forward vector to be directed (0;0;1), there will be only 2 rotations
// around X & Y axis.
void Move(float *x, float *y, float *z, float AxisX, float AxisY, float Amount)
{
float nx=0,ny=0,nz=1; // <- normal of init direction
Rotate2D(nz,ny, AxisX); // around X
Rotate2D(nx,nz, AxisY); // around Y

*x+=nx*Amount;
*y+=ny*Amount;
*z+=nz*Amount;
}

==============
suppose if:
x=0;y=0;z=0;

Move(&x,&y,&z, PI/2,0, 10);
will move the point up to 10 (0;10;0)

and then...

Move(&x,&y,&z, 0,0, 10);
will move the point front to 10 (0;10;10)

and more...

Move(&x,&y,&z, 0,PI/2, 10);
will move the point left to -10 (-10;10;10)
==============

But if you need to direct some object while moving along some direction, you
better use matrices (It will work faster and it is better for complex transformations)

That is all.
If you have some queries, do not hesitate to ask questions, anytime.

• Why not Dload a demo copy of Blitzbasic at www.blitzbasic.com ???

The only thing it cannot do is compile executable programs, but you can run your programs in its environment...it offers pixel drawing and line drawing routines which could be used for you to learn what you want...?