Sign in to follow this  

Moving in a 3D world

This topic is 4221 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know, I have ever posted that question. I poste it again, because the last answers did not really help me... I've got a 3D environement. Instead of moving my character along the x-axis or the z-axis, like it's done in a 2D pac-man, I would like to move my character in a direction. For example if I press the left keyboard button, I would like my character to turn and if I press the forward keyboard button the character has to go in the direction related to the current angle of the character. cheers !!

Share this post


Link to post
Share on other sites
Use sine and cosine.

I use something akin to the following algorithm, though I haven't actually peeked in my base code for three years so I'm doing this from memory:


on(leftKeyDown) angle_in_radians -= turning_speed
on(rightKeyDown) angle_in_radians += turning_speed
x += sin(angle_in_radians) * distance
y += cos(angle_in_radians) * distance


Trig is very handy. I'm not exactly sure this is the right algo, now that I think about it -- you may have some problems with the angle being off by a couple degrees depending on how your API rotates things.

Share this post


Link to post
Share on other sites
Then it shouldn't matter too much (although note that glRotate and other routines in OpenGL use degrees, not radians, to store rotational values. You may have to write a degrees-to-radians function so you can use it with sin and cos properly).

Share this post


Link to post
Share on other sites
Quote:
Original post by Ravuya
Then it shouldn't matter too much (although note that glRotate and other routines in OpenGL use degrees, not radians, to store rotational values. You may have to write a degrees-to-radians function so you can use it with sin and cos properly).


or you can just use PI/180 to convert from degrees to radians, dont really need a function ;)

this main problem comes up with things like asteroids clones where you need to rotate the little ship and you fire from the front of the ship regardless where it is pointing.. same with flying the ship.

Share this post


Link to post
Share on other sites
when the character turns, do I have to modify angle, x and y at the same time?

besides, the y value of my character is fixed. i'm only using x and z. what does it change in your formula?

Share this post


Link to post
Share on other sites
Quote:


on(leftKeyDown) angle_in_radians -= turning_speed
on(rightKeyDown) angle_in_radians += turning_speed
x += sin(angle_in_radians) * distance
y += cos(angle_in_radians) * distance




i think multiply it with frame per second (normally is fixed to 60 fps)will be a good enhancement for some of the fast machine.

Share this post


Link to post
Share on other sites
Quote:
Original post by gan
Quote:


on(leftKeyDown) angle_in_radians -= turning_speed
on(rightKeyDown) angle_in_radians += turning_speed
x += sin(angle_in_radians) * distance
y += cos(angle_in_radians) * distance




i think multiply it with frame per second (normally is fixed to 60 fps)will be a good enhancement for some of the fast machine.


Definitely a good idea. You don't want to be zipping around on your home computer and crawling at a snail's pace on some crappy old machine. :P

Share this post


Link to post
Share on other sites

This topic is 4221 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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