matrix translation

Started by
5 comments, last by Xyan 22 years, 3 months ago
I''d like to know if I translate to a certain position(say 0.0,0.5,0.5) and after that I draw my object, it''s get the same coordinates, right? So if I translate down the x-axis to a knew position and I draw a second object(say 0.5,0.5,0.5), I''ve got a second object with it''s own new coordinates at''s its origin, right, third object same thing...? So if I want to rotate an object in a circle I translate the object and then rotate it, right? Now here''s the problem, do I have to do this after I''ve drawn my object because if I do it before I will just translate 2 times and it will just have a different origin, right? Is calculating the camera position the same thing as for objects? Suppose I want to be able to rotate the camera and transform it(like in a fps), the problem is that the camera''s original coordinates will stay the same, right? So if my camera moves on the x-axis and rotates it will not rotate on its axis but on the first axis I made, so it will rotate in a circel instead? If this makes any sence could you please help me a bit with my problem? Thanks for any replies!
Advertisement
quote:Original post by Xyan
I'd like to know if I translate to a certain position(say 0.0,0.5,0.5) and after that I draw my object, it's get the same coordinates, right? So if I translate down the x-axis to a knew position and I draw a second object(say 0.5,0.5,0.5), I've got a second object with it's own new coordinates at's its origin, right, third object same thing...?


Yes; Of course. If you call glTranslatef(...) then draw an object, the object's (0,0) position will be where the call to Translatef took it.

What is a (0,0) position? When you start out, your drawing point is at the center of a x-y axis intersection. The midpoint of the two are the (0,0) position. When you call something like glVertex3f(0.0, 0.0, 0.0), that vertex is @ (0,0). If you then call glTranslatef(5.0, 0.0, 0.0), then call glVertex3f(8.0, 0.0, 0.0), your object will be drawn 13 units along the x axis.

quote:Original post by Xyan
So if I want to rotate an object in a circle I translate the object and then rotate it, right?
Now here's the problem, do I have to do this after I've drawn my object because if I do it before I will just translate 2 times and it will just have a different origin, right?


If you intend to move your object you usually call the translation and rotation functions before you draw your object. For example, if you want a spinning triangle inside a sphere, you could do something like this:

      float rot; int DrawTriangle(){glBegin(GL_TRIANGLE);glVertex3f(-1.0f, 0.0f, 0.0f);glVertex3f(1.0f, 0.0f, 0.0f);glVertex3f(0.0f, 1.0f, 0.0f);glEnd();glFlush();} int RenderScene(){glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix();//draw your sphereglPopMatrix(); glPushMatrix();glRotatef(rot, 1.0f, 0.0f, 0.0f);DrawTriangle();glPopMatrix(); //swap buffers}  



quote:Original post by Xyan
Is calculating the camera position the same thing as for objects? Suppose I want to be able to rotate the camera and transform it(like in a fps), the problem is that the camera's original coordinates will stay the same, right? So if my camera moves on the x-axis and rotates it will not rotate on its axis but on the first axis I made, so it will rotate in a circel instead?


yes you are correct. In order to calculate the appropriate rotations and transformations, try something like this:

  #define SPEED 0.06 int moveFB(int direction);int moveLR(float angle); ... bool forward, backward, turnleft, turnright;  float x, y, z;float lx, ly, lz; float LRpos;  ... int RenderScene(){...if(forward)   moveFB(1);if(backward)   moveFB(-1);if(turnleft){   LRpos-=SPEED;   moveLR(LRpos);}if(turnright){   LRpos+=SPEED;   moveLR(LRpos);} ... //For moving left or right. Requires an angle (float angle)//that is incremented to turn right and decremented to turn left.MoveLR(float angle){	        lx = sin(angle);        lz = -cos(angle);	glLoadIdentity();	gluLookAt(x, y, z,		  x + lx, y + ly, z + lz,		  0,1,0);return 0;} //For moving flatly along the z axis. Requires //one number (int direction), which is either //'1' for forward or '-1' for backward.MoveFB(int direction){x += direction*(lx)*SPEED;z += direction*(lz)*SPEED;glLoadIdentity();	gluLookAt(x, y, z,		  x + lx, y + ly, z + lz,	          0,1,0);}  


quote:
If this makes any sence could you please help me a bit with my problem?
Thanks for any replies!


Remember not to get discouraged. I had similar problems and no one would explain anything to me. I hope this helps.

~Dwarf

If you need any further assistance, contact me: icer@dog.com



Edited by - Dwarf with Axe on January 3, 2002 7:45:33 PM

Edited by - Dwarf with Axe on January 3, 2002 7:47:29 PM
----------[Development Journal]
I have a question. Why do you take sin, cos and the angle and multiply them? It''s really a forgotten trigonometry lesson, but this is just one thing I don''t understand. I''ve seen the sin and cos curves, but why camera''s, and other moving objects are multiplied by them, I don''t know.

subnet_rx


"There are two types of langages, those that everyone bitches about, and those that no one uses."

Standardized C++ Libraries

C++ Programming help

The only news site on the web

"There are two types of languages, those that everyone complains about, and those that no one uses."Standardized C++ LibrariesGamecron PC GamingA Christian Blog
Can you think of another way to do it? It''s short, simple, and to the point. I wrote it so it''s easy to understand.
----------[Development Journal]
wait, no, I''m not asking why you did it. I''m asking why it''s needed. What does it do visually?

subnet_rx


"There are two types of langages, those that everyone bitches about, and those that no one uses."

Standardized C++ Libraries

C++ Programming help

The only news site on the web

"There are two types of languages, those that everyone complains about, and those that no one uses."Standardized C++ LibrariesGamecron PC GamingA Christian Blog
Oh! I''m sorry! I thought you were putting down my function. =)

The reason you need the sin and cos in there is because the ''camera'' in OpenGL never moves. It is always at one point. In terms of a FPS, it''s the world that you move, not the player. The reason you need geometric calls is because when you move along the axis, you need a way to move freely, and not be pathminded onto an axis.

For example, if I had a 3D game and moved forward, I would be moving along the z axis (no math). If I turn left, I just rotate (no math), but if I start walking back to where I came from at a different angle from the original, then I need a way of moving the world so that it looks like I am really moving. Otherwise, the world will just rotate and translate along the axis, and that''s not good.

In Short: They''re for camera movement reguarding world translation/rotation.

~Dwarf


Why am I the only one on this away team wearing a red shirt?
----------[Development Journal]
Thanks so much!

This topic is closed to new replies.

Advertisement