Archived

This topic is now archived and is closed to further replies.

dede

Rotation and Translation of Objects.

Recommended Posts

dede    132
I just finished my 72 hour game, but I have a slight problem with some theory I tried to put into it, but didn't work... In OpenGL, I was trying to do:
glPushMatrix();
    glTranslatef(locX, locY, 0.0f);
    glRotatef(angle, 0.0f, 0.0f, 1.0f);
       glBegin(GL_QUADS);
            glVertex2f(-32.0f, -32.0f);
            glVertex2f(32.0f,  -32.0f);
            glVertex2f(32.0f,   32.0f);
            glVertex2f(-32.0f,  32.0f);
       glEnd();
glPopMatrix();
   
It worked, but I would like to use a Vector Array, which means I need to rotate and translate my object manually. Here is what I came up with...
//determain rotation on a unit vector.
x = cos(angle) + sin(angle)
y = -sin(angle) + cos(angle)

//translate it to the correct distances...
x *=32
y *=32
//translate and draw
  glBegin(GL_QUADS);
       glVertex2f(locX-x, locY+y);
       glVertex2f(locX+x, locY+y);
       glVertex2f(locX+x, locY-y);
       glVertex2f(locX-x, locY-y);
  glEnd();
   
It is a rotation around the Z, then a translation to the correct point. In my head, it should be a square at the correct point, but for some reason, it turned into a rectangle, and the texture always faces up. Can someone see where I went wrong with this, and why? ~~~~~ "One Reality is worth one thousand dreams" Download and play Slime King I. [edited by - dede on January 6, 2004 2:55:04 AM] [edited by - dede on January 6, 2004 6:21:13 AM]

Share this post


Link to post
Share on other sites
FReY    424
Hi dede

Your problem is that you''re only calculating one axis vector. For placing a point in 2d you need 2 axes, right?

So use the following as your axes
axis1 = (x,y)
axis2 = (-y,x)

Now all your points are linear combinations of axis1 and axis2 (which you can now think of as x and y axes respectively). A linear combination means that you just add a multiple of each to obtain your new point.

so, your new points would now become
point1 = 1.axis1 + 0.axis2
point2 = 0.axis1 + 1.axis2
point3 = -1.axis1 + 0.axis2
point4 = 0.axis1 - 1.axis2

so applying this, try:


glBegin(GL_QUADS);
glVertex2f(locX+x, locY+y);
glVertex2f(locX-y, locY+x);
glVertex2f(locX-x, locY-y);
glVertex2f(locX+y, locY-x);
glEnd();


The problem with the above example is that it will probably end up being exactly 45 degrees offset from what you want. If you are keen, try the following which is probably what you wanted to start off with.

point1 = 1.axis1 - 1.axis2
point2 = 1.axis1 + 1.axis2
point3 = -1.axis1 + 1.axis2
point4 = -1.axis1 - 1.axis2

Hope that helped.
FReY

Share this post


Link to post
Share on other sites