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).
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();
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
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.
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