Sign in to follow this  

Using glRotate and glTranslate

Recommended Posts

Hi, I've been modifying my books application so that I now can move along the x and z axis with the a, d, s and w keys. I want to be aible to rotate around the y axis as well with left and right arrow. This is what I've done: glRotatef(yAngle, 0.0f, 1.0f, 0.0f); glTranslatef(xPos, 0.0f, zPos); and it works in a way, except for one important thing. When I use glRotate, the axises does not follow the rotation (that's what I want). So that if I rotate 30 degrees to the left (forexample) the x-axis will still be to the left/right side of the camera. And of course the negative z-axis in front and positive in the back. I want this so that when I move using the glTranslate it will always move to the right/left or ahead/back of the camera. I hope you understand my question ;)

Share this post

Link to post
Share on other sites
Easy enough to do.

//general things that help:
#define RADIANS 57.295779513082320876798154814105f
//convert to radians
#define toRads(degs) ((degs) / RADIANS)
//degree functions:
#define dsin(degs) sin(toRads(degs))
#define dcos(degs) cos(toRads(degs))
#define dtan(degs) tan(toRads(degs))

//when you're updating your position:
float secondsPassed;//you'll need to set this yourself (should be positive)
float speed = 1.0f;//change this if you need to
float rotationAngle = (yAngle - 90.0f);//this may be wrong (depends on your needs)
xPos += (secondsPassed * speed * (dcos(rotationAngle)));
zPos += (secondsPassed * speed * (dsin(rotationAngle)));

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this