0
\$\begingroup\$

I am making a game using LWJGL. I have encountered a problem with rotation, looked it up, found a solution, and added it, but it doesn't entirely work.

In the code below, the bottom section is rotating well, but when the top one is rotating the other direction, the same direction, or anything else relating to rotation, the bottom sprite seems to move with it, or cancel out the rotation altogether.

            GL11.glPushMatrix();
            GL11.glTranslatef(0.0f, 0.0f, -300f);
            GL11.glRotatef(i+=0.5f, 0.0f, 0.0f, 1.0f );
            drawStar2(0, 0, 0);
            GL11.glLoadIdentity();
            GL11.glPopMatrix();
            
            GL11.glLoadIdentity();
            
            GL11.glPushMatrix();
            GL11.glTranslatef(0.0f, 50.0f, -300f);
            GL11.glRotatef(i-=0.5f, 0.0f, 0.0f, 1.0f);
            drawStar0(0, 0, 0);
            GL11.glLoadIdentity();
            GL11.glPopMatrix();

How can I make the rotation of the object in the code on the bottom insensitive to the rotation of the object in the code at the top?

I am also using Slick-Utils, if this is also causing a problem I haven't heard of.

\$\endgroup\$
0
\$\begingroup\$

Your matrix operations look okay. (You don't need to call glLoadIdentity at all, but it won't do any harm.) The problem looks to be here:

GL11.glRotatef(i+=0.5f, 0.0f, 0.0f, 1.0f );
...
GL11.glRotatef(i-=0.5f, 0.0f, 0.0f, 1.0f);

If you want independently spinning objects, you must give them separate fields/variables to store each one's current rotation.

// in your class or whatever
private float star2Rot;
private float star0Rot;

...

GL11.glRotatef(star2Rot += 0.5f, 0.0f, 0.0f, 1.0f );
drawStar2(0, 0, 0);

...

GL11.glRotatef(star0Rot -= 0.5f, 0.0f, 0.0f, 1.0f);
drawStar0(0, 0, 0);

Some further advice: When animating rotations, it's a good idea to remove multiples of a full rotation to avoid losing floating-point precision with unnecessarily large values.

That is, put this sort of thing somewhere (it doesn't matter where as long as it gets run while the animation is running):

if (star2Rot >= 360.0) {
  star2Rot -= 360.0;
}

This may help avoid glitches when your app is running for a long time.

\$\endgroup\$
1
  • \$\begingroup\$ omg thank you! I feel so stupid after this... but big thanks :) \$\endgroup\$ – Pale_Gray Mar 3 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.