I am trying to move a 2D triangle around the screen. The problem that I have is that the tip of the triangle (vertex facing Top of the monitor) is to be forward facing, (like the grill of a car goes where the car is pointed) and the triangle when the up arrow is pressed will move in that direction.
How do I get it to move in that direction, currently it moves along the X axis no matter what the rotation is.
To move anything n units at a x° angle, move it cos(x)*n in x-direction and sin(x)*n in y-direction. (you might want to include math.h).
Second, translating before rotating seems to make not much sense. I’s suggest something like the following, called from your display function:
GLfloat angle = 0.0;
void moveArrow ( GLfloat stepwidth, GLfloat angle )
{
glPushMatrix ( ); // to move it independantly from the rest of the scene
glRotatef ( oldangle + angle, 0.0, 0.0, 1.0 ); // if angle = 0 it will just move forward
glTranslatef ( 0.0, stepwidth, 0.0 ); // if stepwidth = 0 it will just spin, if not it will move in y-direction (upwards, relatively)
glBegin ( GL_TRIANGLE );
// colors, vertices...
glEnd ( );
glPopMatrix ( ); // go back to the rest of the scene
}
This way you don’t need to do any math. It’s just like the car, first use the steering wheel and then the accelerator, not lift it around a bend and pose it in the correct direction afterwards
Damn typos in here…
[This message has been edited by Hermann (edited 02-07-2001).]