• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
phil67rpg

rotating vertices

21 posts in this topic

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

 

?

0

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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();
}
0

Share this post


Link to post
Share on other sites

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.

Edited by irlanrobson
0

Share this post


Link to post
Share on other sites


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.

0

Share this post


Link to post
Share on other sites

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

 

 

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

 

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

0

Share this post


Link to post
Share on other sites


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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Show your code.

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

0

Share this post


Link to post
Share on other sites

 

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
1

Share this post


Link to post
Share on other sites

stop using trigonometry manually

 

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

Edited by 3TATUK2
0

Share this post


Link to post
Share on other sites
That's nonsense. You can rotate around any point you please with matrices (and obviously by glRotatef). You just have to apply T^-1 * R * T instead of just R, where T is a translation matrix which moves the desired center point to the origin.
1

Share this post


Link to post
Share on other sites

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

According to this:

but it still rotates around the center of the screen, but does not rotate around the center of the shape.

He wants to rotate around the center of the shape.
But either way raw trigonometry is unnecessary, as it was already pointed out that applying first the inverse translate of the center point prior to rotating, lastly followed by the object’s translation, can all be done with matrix math.




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

When you want to rotate around the center of the object, as he said he wants to do.
Otherwise you translate by the opposite of the center of rotation, rotate, and then apply object translation. In other words, when working with only the object’s translate and rotate, rotate always comes last.
SCALE * ROTATE * TRANSLATE.


L. Spiro
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0