• Create Account

# rotating vertices

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

21 replies to this topic

### #1phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 23 September 2013 - 03:26 PM

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();
}


### #23TATUK2  Members   -  Reputation: 714

Like
0Likes
Like

Posted 23 September 2013 - 11:03 PM

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 );

### #3Sponji  Members   -  Reputation: 2221

Like
0Likes
Like

Posted 23 September 2013 - 11:07 PM

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

### #4Irlan Robson  Crossbones+   -  Reputation: 3920

Like
0Likes
Like

Posted 24 September 2013 - 08:57 AM

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

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

Edited by irlanrobson, 24 September 2013 - 08:59 AM.

### #5phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 24 September 2013 - 11:37 AM

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

### #6phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 24 September 2013 - 10:32 PM

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.

### #7phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 24 September 2013 - 10:33 PM

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();
}


### #8Migi0027 (肉コーダ)  Crossbones+   -  Reputation: 4344

Like
0Likes
Like

Posted 24 September 2013 - 11:04 PM

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.

Edited by Migi0027, 24 September 2013 - 11:04 PM.

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/

### #9RobTheBloke  Crossbones+   -  Reputation: 2536

Like
0Likes
Like

Posted 25 September 2013 - 04:05 AM

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!

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).

### #10phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 25 September 2013 - 12:55 PM

well I am only doing what tatuk was suggesting

### #11phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 25 September 2013 - 06:40 PM

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.

well I tried rotating then translating but it does not move up or down.

### #12phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 27 September 2013 - 07:25 PM

I am reading the opengl superbible 4th ed. it is a really good read.

### #133TATUK2  Members   -  Reputation: 714

Like
0Likes
Like

Posted 27 September 2013 - 08:18 PM

well I am only doing what tatuk was suggesting

Not exactly. I meant something like this:

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

float radr = 0.017453292519943295 * rotatation; // convert rotation from degrees to radians

float temp = x;
x = x * cosf( radr ) - y * sinf( radr );
y = temp * sinf( radr ) + y * cosf( radr );

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

// 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();


Edited by 3TATUK2, 27 September 2013 - 08:19 PM.

### #14phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 28 September 2013 - 07:20 PM

well I tried the above code but it does not rotate, here is my code,

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

float radr = 0.017453292519943295 * rotation; // convert rotation from degrees to radians

float temp = x;
x = x * cosf( radr ) - y * sinf( radr );
y = temp * sinf( radr ) + y * cosf( radr );

// First translate to the wanted position
glTranslatef(x, y, z);
glColor3f(color,0.0f,0.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();
}


### #153TATUK2  Members   -  Reputation: 714

Like
0Likes
Like

Posted 28 September 2013 - 11:07 PM

well what *does* happen? something's *got* to happen because you're changing the x,y,z position (by a rotation matrix) ... are you even supplying new rotation values? it has to gradually increase from 0->360

### #16L. Spiro  Crossbones+   -  Reputation: 24751

Like
1Likes
Like

Posted 29 September 2013 - 05:51 PM

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

That is because he did it in the wrong order.

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

// First rotate.
glRotatef(rotation, 0.0f, 0.0f, 1.0f);

// THEN translate.
glTranslatef(x, y, z);

// 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();
}
Case close problem solved stop using trigonometry manually.

L. Spiro

### #17phil67rpg  Members   -  Reputation: 187

Like
0Likes
Like

Posted 29 September 2013 - 05:59 PM

well spiro I did what  you said and it moves up and down, but it still rotates around the center of the screen, but does not rotate around the center of the shape. sorry spiro but I am trying. I commend you, you are a very bright person.

### #18L. Spiro  Crossbones+   -  Reputation: 24751

Like
0Likes
Like

Posted 29 September 2013 - 11:18 PM

I get the feeling you mixed what I posted with the math you have been trying to use.

The solution is my post, the whole post, and nothing but the post, so help me Todd.

L. Spiro

### #19Sponji  Members   -  Reputation: 2221

Like
1Likes
Like

Posted 29 September 2013 - 11:24 PM

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

That is because he did it in the wrong order.

What, in wrong order? I don't understand, when would he even want to use the order you suggested? First translate, then rotate.

But still, I bet the problem is somewhere else. I think he isn't moving the object correctly, and that should be done outside the render function.

Edited by Sponji, 29 September 2013 - 11:37 PM.

Derp

### #203TATUK2  Members   -  Reputation: 714

Like
0Likes
Like

Posted 30 September 2013 - 07:42 AM

stop using trigonometry manually

He needs to. He wants to rotate AROUND a point. Not at the centerpoint which glRotatef() would do.

Edited by 3TATUK2, 30 September 2013 - 07:43 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS