View Full Version : Moving a triangle regardless of direction

02-04-2001, 01:47 PM
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.

Any hints?



02-05-2001, 05:14 AM
What order are your rotation and translation in? It sounds like you may need to swap them...


02-06-2001, 08:07 AM
I translate then rotate.

My real problem (seems to be atleast) is how to determine how far to move the triangle when it's at a 45 degree angle.


02-07-2001, 08:49 AM
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 http://www.opengl.org/discussion_boards/ubb/wink.gif

Damn typos in here...

[This message has been edited by Hermann (edited 02-07-2001).]

02-07-2001, 01:18 PM
Those car examples are working wonders! =)

Thanks for the hint. I know exactly what's wrong now!