Moving objects in opengl

Started by
6 comments, last by Skeleton_V@T 18 years, 3 months ago
Hi, can anyone explain how to move an object (for the sake of conversation lets say verticie) in opnegl? i think theres something involving a transformation but i'm not sure how to go about this. also how would you move a sphere (they are created at the origin and dont take any coordinates as any sort of parameters).
Advertisement
OpenGL uses transformation matrices that the hardware (or software, depending on where and what OpenGL you use) will apply to your vertices. Plenty of information on transformations in OpenGL is available in the online version of Red Book. For the actual matrix/vector math involved, you may (or may not) need to brush up your linear and vector algebra.

Tom
ok found stuff on translate...but not sure how to use it. do you call it after a verticie or before or?
Before.

As soon as you call "glVertex" it draws something (not always, but consider it that and you'll be fine), so you have to put everything for that vertex before it.

glBegin(GL_POINTS);	glRotatef(0.0f, 1.0f, 0.0f, 65.0f);	glTranslatef(-5.0f, 1.2f, 0.4f);	glNormalf(1.0f, 0.0f, 0.0f);	glColorf(0.0f, 0.0f, 1.0f);	glTexCoord2f(0.5f, 0.23f);	glVertex3f(0.0f, 0.0f, 0.0f);	glEnd();


So, the first 2 calls multiplies the modelview/world matrix by a rotation matrix for blah, and a translation matrix for blah, blah. I know that's not as helpful as it could be, but, are you really interested in the matrix math at this point in time?

Anyway, the translation and rotation calls basically ensure that whatever is drawn after those 2 calls are rotated by what you specified in the glRotation call and translated by what you specified in the glTranslate call.

The next 3 calls sets up the normal, color and texture coords for that vertex.

Of course, with only none of those things are particularly useful, unless you are drawing a series of points.

So, pretty much, all those gl* calls before glVertex acts on what glVertex draws.

Oh, and you should always be drawing about the point 0.0f,0.0f,0.0f. If you rotate by whatever and then translate by whatever and draw at 3.4f,6.1f,0.5f, you'll get something that is absolutely not what you wanted.

You'll understand that more as you get more experience.

Edit:: Also, if you want to rotate something and also translate it, you must rotate first and then translate. Otherwise instead of rotate about the object's center point, it will be rotating around another point and you'll go crazy attempting to figure it out. So, just remember rotate then translate.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
I'd prefer to do it the simple way, I'm sure you have a main loop. add a draw() function to your object methods'.

in the main loop, call [object].draw();

in the draw(){

Gl.glBegin([whatever]);

Gl.glVertex2d(x,y);

Gl.glEnd();

}

this way, it will always redraw the object and it's as if it's moving. As to moving it by default

private double x,y;

in the draw(){

Gl.glBegin([whatever]);

Gl.glVertex2d(x,y);

this.move();

Gl.glEnd();

}

private void move(){

y++; // moves the object vertically
}
Quote:Original post by EndarEdit:: Also, if you want to rotate something and also translate it, you must rotate first and then translate. Otherwise instead of rotate about the object's center point, it will be rotating around another point and you'll go crazy attempting to figure it out. So, just remember rotate then translate.


I'm a rather new to OpenGL, but I don't beleive that this is correct. Isn't the reverse true?

According to Beginning OpenGL Game Programming, "Let's say that you have an arrow located at the origin that lies flat on the x-y plane, and the first transformation you apply is a rotation of 30 degrees around the z axis. You then apply a translation transformation of +5 unites along the x axis. THe final position of the triangle would be (5, 4.33) with the arrow pointing at a 30-degree angle from the positive x axis. Now, let's swap the order and say you translate the arrow by +5 unites along the x axis first. Then you rotate the arrow 30 degrees about the z axis. After the translation, the arrow would be located at (5,0). WHen you apply the rotation transformation, the arrow would be at (5,0), but it would be pointing at a 30-degree angle from the x axis."

This is also what I have seen from my minimal experience with OpenGL.
Quote:Original post by Ezbez
Quote:Original post by EndarEdit:: Also, if you want to rotate something and also translate it, you must rotate first and then translate. Otherwise instead of rotate about the object's center point, it will be rotating around another point and you'll go crazy attempting to figure it out. So, just remember rotate then translate.


I'm a rather new to OpenGL, but I don't beleive that this is correct. Isn't the reverse true?

Yes, he got the order mixed up. One way to remember this stuff, is that the last transformation before drawing an object always applies 'first'. Therefore, to rotate an object around it's axis, this rotation should be the last transform before drawing it.

Tom
Quote:Original post by dimebolt
//...

Yes, he got the order mixed up. One way to remember this stuff, is that the last transformation before drawing an object always applies 'first'. Therefore, to rotate an object around it's axis, this rotation should be the last transform before drawing it.

Tom


That's not really true, it depends on your current matrix mode:
  1. In Model View matrix: If you apply a clock-wise rotation, the objects will be rotated clock-wise from the user's view point.

  2. In World matrix: If you apply a clock-wise rotation, the objects will be rotated counter clock-wise from the user's view point, since moving the World's origin to the left produces the same result as moving all the objects Model View's origin to the right.

This is actually rather simple but is also the most confusing beginner knowledge as the current matrix affects the order we transform the matrices isn't always pointed out by online tutorials (most serious books cover it quite sufficiently though).
--> The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones <--

This topic is closed to new replies.

Advertisement