OpenGL 2d turning

I am trying to make a quick program where my square in the center of the screen will go towards the position of my cursor. I can get it to, but if the cursor is far away, the guy moves faster and if its closer it moves slower.

I simply got the distance on x axis from the guy to the cursor and divided that by about 500. This is clearly an inefficient way to do this. I just am condused on how exactly to move him at a constant speed.

I also am trying to get him to point in the direction of the cursor, but i cannot grasp the concept of getting the equation of the line and using that. If i could figure out how to get a point on the line that is always a constant distance away i could solve most of these problems.

Any help is greatly appreciated as i am in the process of learning.
speed x = speed constant * distance x / total distance
speed y = speed constant * distance y / total distance
where
total distance = square root ( distance x^2 + distance y^2 )
and constant speed is a number of your choice.

The idea is that speed vector is the distance vector forced into a constant size (via division by its length).
hamsterman's solution will do the trick for the speed issue. As for facing the right way, you can use the atan2 function:

angle = atan2( distance_y, distance_x );

Just note that atan2 gives an angle in radians, whereas OpenGL takes angles in degrees, so you'll have to multiply by 180/pi to convert.
Thank you guys so much!! I am loving programming so much!!
Just one question for Disch, am i supposed to use the rotatef function of should i manually get the perpendicular line and whatever....?
If you are using the old-style OpenGL matrix transformations, you would use glRotatef, yes:

1
2
3
4
5
6
7
8
9
10
// this is all from distant memory, so double-check!

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();  // start with an identity matrix
glRotatef( angle_of_rotation );  // rotate the object
glTransformf( object_x_pos, object_y_pos, 0 );  // then move it to where it's supposed to be

glBegin( /*...*/ );
// draw vertexes etc here
glEnd();
Last edited on
well im using glut, and the rotatef function takes 4 arguments, the angle, posX, posY, posZ, and the translatef is how it is moved.
ah, yeah I forgot you need to give it an axis to rotate on.

So yeah it would be glRotatef( angle, 0, 0, 1 ); Assuming you want "normal" 2D rotation.
Yeah i did this and i now figure that my square guy isn't at the point (0,0), so i kept trying to make the x and y coords my guys, but that doesn't work. I guess i will just make my guy coords at 0, 0 always. Thanks
huh?
Okay what if i don't want my guy to always be at the point (0,0)?
1
2
3
4
5
6
7
8
9
10
11
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();  // start with an identity matrix
glRotatef( angle_of_rotation, 0, 0, 1 );  // rotate the object

//  V   V
glTransformf( object_x_pos, object_y_pos, 0 );  // then move it to where it's supposed to be
//  ^   ^

glBegin( /*...*/ );
// draw vertexes etc here
glEnd();


EDIT: if it's not Transform it might be Translate. Sorry, it's been a while. Double check.
Last edited on
For some reason, it isn't working. I have a line going from the guys position to my mouse and the square is not appearing at where it says the guys position is.... its a little off and gets further or closer depending on my mouse distance away.not sure what to do
Hard to debug without seeing pics/code.
Topic archived. No new replies allowed.