Question

I'm currently using LWJGL to create a simple 2D game. I wanted to create some objects to make things a bit easier. Currently simply drawing a Quad object works fine, but then I decided to implement a way to draw a quad rotated.

This is the code so far for drawing a rotated Quad:

@Override
public void draw(float angle) {
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // translate the quad to the origin before rotation
    glTranslatef(-(this.x + (this.w / 2)), -(this.y + (this.h / 2)), 0);

    glRotatef(angle, (this.x + (this.w / 2)), (this.y + (this.h / 2)), 0);

    // translate the quad back to its original position
    glTranslatef((this.x + (this.w / 2)), (this.y + (this.h / 2)), 0);

    glColor4f(this.color.getRed(), this.color.getAlpha(), this.color.getBlue(), this.color.getAlpha());
    glBegin(GL_QUADS);
        glVertex2f(this.x, this.y);
        glVertex2f(this.x + this.w, this.y);
        glVertex2f(this.x + this.w, this.y + this.h);
        glVertex2f(this.x, this.y + this.h);
    glEnd();
}

I do this to draw a Quad Quad q = new Quad(10, 10, 100, 100); // new Quad(x, y, w, h) q.draw(Math.PI); // it is 180 degrees but it doesn't draw the quad like it is

And this is how it draws

Quad

I'm not sure what is going on here.

As a side question

Would it be the same effect if I simply use a 2D rotation matrix to calculate the new x and y and then simply calculate the other x's and y's?

Was it helpful?

Solution

The parameters of glRotatef define the axis. And you want to rotate about the z-axis. Therefore:

glRotatef(angle, 0, 0, 1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top