Moving in a 3D world

Started by
7 comments, last by SippyCup 17 years, 11 months ago
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 !!
Advertisement
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.
I'm using OpenGL API...
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).
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.
heh
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 !!!
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.
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

This topic is closed to new replies.

Advertisement