Archived

This topic is now archived and is closed to further replies.

Koos

Vectors and lateral movement

Recommended Posts

Ok, I''m having trouble coming up with an equation to try and calculate lateral movement with vectors. Here is the general information... cpos = camera position vector cview = camera view vector And here is my code: GLvoid CAMERA::StrafeCamera(GLfloat speed) { CAMVECT vect1, tempvect1; vect1.x = cview.x - cpos.x; vect1.y = cview.y - cpos.y; vect1.z = cview.z - cpos.z; tempvect1.x = (float)(sin(90)*vect1.x+cos(90)*vect1.z); tempvect1.z = (float)(cos(90)*vect1.x-sin(90)*vect1.z); cpos.x += tempvect1.x * speed; cpos.z += tempvect1.z * speed; cview.x += tempvect1.x * speed; cview.z += tempvect1.z * speed; } I have the idea that the problem lies in the use of 90, but I dont know how to fix it. Can anyone help me?

Share this post


Link to post
Share on other sites
The C/C++ trig functions take radian angles as their arguments. The conversion from degrees to radian is angle/180 * PI. So you can define a conversion macro in C:
// expects M_PI to be defined with the value of PI
#ifndef M_PI
// this is too much precision; you should probably use the first 10 digits or so
#define M_PI 3.1415926535897932384626433832795
#endif
#define deg2rad(x) ((x / 180) * M_PI)

or a template inline function in C++:
const int _PI = 3.1415926535897932384626433832795;
inline template <class T> deg2rad(T x) { return ((x / 180) * _PI); }

You can now use either version in your program (but not both!) like so:
float rad = deg2rad(deg); 

Enjoy!



I wanna work for Microsoft!

Share this post


Link to post
Share on other sites