I wrote the following code to rotate a cube on x axis an entire revolution, then the y axis, and back and forth. The code generates the desired result.
- (void)update
{
if (_x_axis) {
if (i >= 6.28) {
i = 0;
_x_axis = false;
} else {
float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f);
self.effect.transform.projectionMatrix = projectionMatrix;
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -3.0f);
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, i, 0.0f, 1.0f, 0.0f);
self.effect.transform.modelviewMatrix = modelViewMatrix;
i += .01;
}
} else {
if (i >= 6.28) {
i = 0;
_x_axis = true;
} else {
float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f);
self.effect.transform.projectionMatrix = projectionMatrix;
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -3.0f);
modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, i, 1.0f, 0.0f, 0.0f);
self.effect.transform.modelviewMatrix = modelViewMatrix;
i += .01;
}
}
My question is, when I change the increment to i to something such as .02 or .2 or .3, why does the cube still rotate exactly one revolution?
If i is incremented by .2, the last value of i used in the transformation would be 6.2 before it would be reset and the rotation axis was changed. Basically, what is keeping the cube level? I would think with the larger increment of i, the cube wouldn't make an entire revolution before switching axis.
6.28 = 2*M_PI
, with the code given a full rotation. The parameter i
is straight given to GLKMatrix4Rotate
. If you want different lengths, you can vary this constants in the conditions, or map the parameter i
before calling GLKMatrix4Rotate
.
For the the second approach, modify the code:
i
to [0;1]
, and make the step width a bit smaller.GLKMatrix4Rotate
with i*2*M_PI
.Now the animtion generally runs as previous, however, you can twist this with easing functions.
update
call. When the rotation axis is changed, you could draw the model one time again in the initial position. BTW What is the meaning of _rotation
? Seems to be unused - Stefan Hanke 2012-04-07 07:49
_rotation
is actually supposed to be i
. I will reset the model to it's previous state to get the desired results. Any advice on doing this? Is there a best practice? Should I save the matrix at the end of update and set it the start of update - Half_Duplex 2012-04-07 13:54