rotating vertices

Started by
20 comments, last by L. Spiro 10 years, 6 months ago

well l am able to move some vertices up and down on the screen. I am trying to get my shape to rotate around a central z-axis. here is my code. Let me know if you need more information.

void
 
 
DrawShip(GLfloat position_X,GLfloat position_Y, GLfloat position_Z, GLfloat rotation,GLfloat color)
{
glPushMatrix();
glColor3f(color,0.0f,0.0f);
glRotatef(rotation,0.0f,0.0f,1.0f);
glBegin(GL_LINE_LOOP);
glVertex3f(0.0f,-0.25f+position_Y,0.0f);
glVertex3f(-0.25f,-0.5f+position_Y,0.0f);
glVertex3f(-0.5f,-0.5f+position_Y,0.0f);
glVertex3f(-0.0f,0.5f+position_Y,0.0f);
glVertex3f(0.5f,-0.5f+position_Y,0.0f);
glVertex3f(0.25f,-0.5f+position_Y,0.0f);
glVertex3f(0.0f,-0.25f+position_Y,0.0f);
glEnd();
glPopMatrix();
}

?

Advertisement

you need to look into basic trig rotation matrix math... it goes something like this:

float temp = x_position;
x_position = x_position * cosf( z_rotation ) - y_position * sinf( z_rotation );
y_position = temp * sinf( z_rotation ) + y_position * cosf( z_rotation );
(z_rotation in radians, - *NOT* - degrees)
then you should be able to glTranslatef( x_position, y_position, z_position );

void DrawShip(float x, float y, float z, float rotation) {
    glPushMatrix();

    // First translate to the wanted position
    glTranslatef(x, y, z);

    // Then rotate it
    glRotatef(rotation, 0.0f, 0.0f, 1.0f);

    // Then render the shape of the object, you have already translated and rotated it to the right place
    glBegin(GL_LINE_LOOP);
    glVertex3f(0.0f,-0.25f,0.0f);
    glVertex3f(-0.25f,-0.5f,0.0f);
    glVertex3f(-0.5f,-0.5f,0.0f);
    glVertex3f(-0.0f,0.5f,0.0f);
    glVertex3f(0.5f,-0.5f,0.0f);
    glVertex3f(0.25f,-0.5f,0.0f);
    glVertex3f(0.0f,-0.25f,0.0f);
    glEnd();
    glPopMatrix();
}

Derp

Instead, use Rotation Matrices or Quaternions to rotate ANYTHING. Take a look in a good mathematics book. cool.png

Once you get that, use glMultMatrixf( MyMatrix ) and it's done.

well I tried sponji code but it still does not rotate around a its central axis after I move it up or down.


you need to look into basic trig rotation matrix math... it goes something like this:
float temp = x_position;
x_position = x_position * cosf( z_rotation ) - y_position * sinf( z_rotation );
y_position = temp * sinf( z_rotation ) + y_position * cosf( z_rotation );
(z_rotation in radians, - *NOT* - degrees)
then you should be able to glTranslatef( x_position, y_position, z_position );

well I tried the above code, it does not work, it rotates but it does not follow its nose, it is a space ship of vertices.

here is my code

void
 
 
DrawShip(GLfloat position_X,GLfloat position_Y, GLfloat position_Z, GLfloat rotation,GLfloat color)
{
glPushMatrix();
glColor3f(color,0.0f,0.0f);
glTranslatef(position_X,position_Y,position_Z);
glBegin(GL_LINE_LOOP);
glVertex3f((0.0f*cosf(rotation)+0.25f*sinf(rotation)),(0.0f*sinf(rotation)-0.25f*cosf(rotation)),0.0f);
glVertex3f((-0.25f*cosf(rotation)+0.5f*sinf(rotation)),(-0.25f*sinf(rotation)-0.5f*cosf(rotation)),0.0f);
glVertex3f((-0.5f*cosf(rotation)+0.5f*sinf(rotation)),(-0.5f*sinf(rotation)-0.5f*cosf(rotation)),0.0f);
glVertex3f((0.0f*cosf(rotation)-0.5f*sinf(rotation)),(0.0f*sinf(rotation)+0.5f*cosf(rotation)),0.0f);
glVertex3f((0.5f*cosf(rotation)+0.5f*sinf(rotation)),(0.5f*sinf(rotation)-0.5f*cosf(rotation)),0.0f);
glVertex3f((0.25f*cosf(rotation)+0.5f*sinf(rotation)),(0.25f*sinf(rotation)-0.5f*cosf(rotation)),0.0f);
glVertex3f((0.0f*cosf(rotation)+0.25f*sinf(rotation)),(0.0f*sinf(rotation)-0.25f*cosf(rotation)),0.0f);
glEnd();
glPopMatrix();
}

Are you translating after you rotated?

To archive your spaceships rotation, you'd need the following order of transformations (I don't know if this order has a name):

(Rotate -> Scale) -> Translate

The first two can be switched.

So rotate it around 0,0,0

Then scale it if needed

Then translate it to xyz, then it looks like it rotated around itself. Though the center of the vertices/the center of the spaceships model must be around 0,0,0, in terms of vertices.

Hope this helps.

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

here is my code


void
 
 
DrawShip(GLfloat position_X,GLfloat position_Y, GLfloat position_Z, GLfloat rotation,GLfloat color)
{
glPushMatrix();
glColor3f(color,0.0f,0.0f);
glTranslatef(position_X,position_Y,position_Z);
glBegin(GL_LINE_LOOP);
glVertex3f((0.0f*cosf(rotation)+0.25f*sinf(rotation)),(0.0f*sinf(rotation)-0.25f*cosf(rotation)),0.0f);
glVertex3f((-0.25f*cosf(rotation)+0.5f*sinf(rotation)),(-0.25f*sinf(rotation)-0.5f*cosf(rotation)),0.0f);
glVertex3f((-0.5f*cosf(rotation)+0.5f*sinf(rotation)),(-0.5f*sinf(rotation)-0.5f*cosf(rotation)),0.0f);
glVertex3f((0.0f*cosf(rotation)-0.5f*sinf(rotation)),(0.0f*sinf(rotation)+0.5f*cosf(rotation)),0.0f);
glVertex3f((0.5f*cosf(rotation)+0.5f*sinf(rotation)),(0.5f*sinf(rotation)-0.5f*cosf(rotation)),0.0f);
glVertex3f((0.25f*cosf(rotation)+0.5f*sinf(rotation)),(0.25f*sinf(rotation)-0.5f*cosf(rotation)),0.0f);
glVertex3f((0.0f*cosf(rotation)+0.25f*sinf(rotation)),(0.0f*sinf(rotation)-0.25f*cosf(rotation)),0.0f);
glEnd();
glPopMatrix();
}

Oh god! My eyes! They burn! :p

Don't call sin/cos so many times like that! sin/cos are non trivial functions, and by transforming the vertices like that by using the CPU, instead of the GPU, you are failing to harness any of the specially optimised GPU hardware to transform the vertices.

I think it's probably time for you to start writing your own 4x4 matrix class (or use one from a 3rd party library, e.g. Eigen), so that you can construct a matrix that performs the transformations, before passing that matrix to openGL via glMultMatrixf (incidentally, this will also help to port your code over to OpenGL 4.4 at a later date, since your transforms will already be in matrix form ready to pass into a shader).

well I am only doing what tatuk was suggesting

This topic is closed to new replies.

Advertisement