Public Group

# Moving in a 3D world

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

## 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 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_speedon(rightKeyDown) angle_in_radians += turning_speedx += sin(angle_in_radians) * distancey += 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 on other sites
I'm using OpenGL API...

##### 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 on other sites
Quote:
 Original post by RavuyaThen 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 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?

cheers guys !!!!
it works !!!

##### Share on other sites
Quote:
 on(leftKeyDown) angle_in_radians -= turning_speedon(rightKeyDown) angle_in_radians += turning_speedx += sin(angle_in_radians) * distancey += 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 on other sites
Quote:
Original post by gan
Quote:
 on(leftKeyDown) angle_in_radians -= turning_speedon(rightKeyDown) angle_in_radians += turning_speedx += sin(angle_in_radians) * distancey += 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

1. 1
2. 2
Rutin
19
3. 3
4. 4
frob
13
5. 5

• 9
• 18
• 11
• 9
• 17
• ### Forum Statistics

• Total Topics
632605
• Total Posts
3007371

×