# object translation

This topic is 3780 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I might be in the wrong secton of the forum for this problem but... Now that I've managed to get my object rotating and translating correctly I'm now needing to get it moving realistically. In that I'm trying to get my object to move exactly in the direction the object is facing. Ideally like a car. Any idea on how I do this? What math do I use? I am aware trig will be involved but not sure on how to put it together. Is there a good tutorial on this sort of thing? Thank you in advance :) Andrew

##### Share on other sites
>> In that I'm trying to get my object to move exactly
>> in the direction the object is facing

If n is the (normalized) direction in which your object is facing and p is its current position and a the distance to move, the final position is:

p + n * a

Alexander

##### Share on other sites
Quote:
 Original post by Andrew1979Any idea on how I do this? What math do I use? I am aware trig will be involved but not sure on how to put it together.

Actually, trig will not be involved or at least doesn't have to. Since you are using OpenGL, all you need to understand is the difference between world and object space and that transformations are always done in object space. If your model is created with z being "forward", then glTranslatef(0,0,x) will ALWAYS move it in the direction it is facing. Just don't let people tell you that GL is doing everything backwards. That's true if you insist on a world space point of view, but it's also pointlessly confusing for complex transformation stacks. In the same way, glRotate will always rotate around the objects center and along the objects x,y,z axes.

##### Share on other sites
Hi

Trienco I think your not quite understanding what I'm getting at, or it could be me.

I can move my object forward and backward and rotate it no problem. Question is, is how do I get my object to move in that rotated direction or on that rotated angle both forward and backward?

I can move the object directly forward and back, no problem, how do I get it to move forward and backward on the angle I rotate it to?

The method you suggets just sends the object forward and backward which ideally I'm wanting the object to move forward and backward on the angle I've rotated it on.

##### Share on other sites
Well in my opinion what Porthos said would work. Basically you store a matrix that represents you entity's current orientation and from it you can get the direction it is facing. Otherwise you could store an angle and in 2D you get :
newPosX=posX+speed*cos(ang);newPosY=posY+speed*sin(ang);

Hope this helps.

PS That is a plus between posX/Y and speed. How do I make GDNet display my plus signs?

##### Share on other sites
glTranslatef(x, y, z);
glRotatef(angle, 0, 0, 1);

assuming z is up, rotate object around it and then move.
If y is forward, make y a non-zero value.

##### Share on other sites
Quote:
 Original post by Andrew1979Trienco I think your not quite understanding what I'm getting at, or it could be me.

It's probably just that you haven't dropped your world space perspective yet and still assume OpenGL doing stuff in reverse order.

Take your object and do this (assuming you modelled it with z=forward):

glRotatef(45, 0,1,0);
glTranslatef(0,0,1);

surprise, it will still move forward, because it's local z-axis will ALWAYS be forward, no matter how much you rotated it before that.

Then go forth and store the transformation matrix for your objects and do NOT think you can get away by storing a position and a few angles and have things working (or even rotating) the way you expect it.

Of course you can go and start storing a forward and up and position vector, but that would be pretty silly, because left/up/forward/position is nothing but the four columns making up your matrix anyway, so you'd just overcomplicate things by replacing one matrix multiplication with lots of manually fiddling around.

If you want to be lazy for now, let GL do the work.
class car{  float matrix[16];  void move(float x, float y, float z);  void rotate(float angle, float x, float y, float z);};move(...){  glPushMatrix();  glLoadMatrix(matrix);  glTranslatef(x,y,z);  glGetFloatv(GL_MODELVIEW_MATRIX, matrix);  glPopMatrix();}rotate(...){  glPushMatrix();  glLoadMatrix(matrix);  glRotate(angle,x,y,z);  glGetFloatv(GL_MODELVIEW_MATRIX, matrix);  glPopMatrix();}render(){  glPushMatrix();  glMultMatrix(matrix);  //rendercalls  glPopMatrix();}

Reading back the matrix all the time isn't great for performance, but will do fine until you get or write a matrix math library. And definitely read up on things like object space, world space, transformations and the difference between multiplying a matrix from the left and from the right (which ultimately decides if you're using the world or local space coordinate system).

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 10
• 27
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633403
• Total Posts
3011675
• ### Who's Online (See full list)

There are no registered users currently online

×