Question

I'm writing an OpenGL es (2.0) game on Android.

I have a textured quad like so:

enter image description here

Before I enter each level, I set a value which is the number of seconds that this timer should take to rotate. I then simply work out how many degrees to rotate each second. Like so:

AngleToRotate = 360/numberOfSeconds;

So when I want to rotate I do something like this: (pseudo code)

if (countOneSecond()){

    rotateTimer(AngleToRotate)

}

So.... if I want it to take 60 seconds, I say

AngleToRotate = 360/60 (AngleToRotate = 6)

Every seconds, it rotates by 6 degrees, therefore, it completes a full rotation in 1 minute.

Problem

The problem is that if I want to rotate by an amount where the angle doesn't result in a whole number, I run into problems, so lets say 80 seconds:

 AngleToRotate = 360/80 (AngleToRotate = 6) = 4.5

Therefore it only rotates by 4.

Summary

So, is it possible to rotate a quad by a fraction of an angle? Or is there another trick to make the timer complete a full revolution in any specified amount of seconds?

Code

Here is my rotation method:

public void rotate(float x, float y, float angle){

//Convert OpenGL co-ordinates into screen coordinates

    //xPlotRight and xPlotLeft are defined, I have removed them from here as they
    //aren't relevant to this question

    //Center of quad (Along the x)
    centreX = xPlotLeft + ((xPlotRight-xPlotLeft)/2);
    //Center of quad (Along the y)
    centreY = yPlotBottom + ((yPlotTop-yPlotBottom)/2);

    //Rotate the quad

    Matrix.setIdentityM(mRotationMatrix, 0);
    Matrix.translateM(mRotationMatrix, 0, centreX, centreY, 0f);
    Matrix.rotateM(mRotationMatrix, 0, -angle, 0, 0, 0.1f);
    Matrix.translateM(mRotationMatrix, 0, -centreX, -centreY, 0f);
}
Était-ce utile?

La solution

OpenGL ES 2.0 does not really have a concept of angles. We don't see this in the code you posted, but somebody builds a rotation matrix from the angle, which most likely ends up in the mRotationMatrix variable based on the code you show. Almost any transformation matrix you use will have float elements, and can therefore represent rotations for arbitrary angles within floating point precision.

If AngleToRotate is currently of type int, you'll need to make it a float. Similarly, if rotateTimer currently has an int argument, change the type of the argument to float. Then check the code of rotateTimer, and make sure that it passes along the angle as float if it calls additional methods. At some point you should arrive at the code that builds the rotation matrix, which will most likely convert the angle from degrees to radians, calculate the cos and sin of the angle, and populate the rotation matrix with those values.

I would recommend to keep all your angles in radians, but that's more a style suggestion.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top