Friday, December 12, 2008

NeHe Tutorials, Lesson 4 Ported to iPhone

NeHe Lesson 4 was very easy to port. The newly introduced functionality all pretty much works as is.

Here is the link for the ported Lesson 4 project.

The only noteworthy difference (other than items mentioned in the previous blog postings) is the fact that I declared rtri and rquad as static variables within the drawView method rather than declare global variables. This was just to keep all the relevant code for the project together, and doesn't have any impact on the program's functinoality.

One change I might recommend would be to tie the increases to rtri and rquad to the amount of time elapsed. It seems to run pretty smoothly, but if you wanted it perfectly smooth, that would be the way to accomplish it. To do this, replace this code:
 rtri+=0.2f;      // Increase The Rotation Variable For The Triangle ( NEW )
rquad-=0.15f; // Decrease The Rotation Variable For The Quad ( NEW )
with something like this code, which rotates at a steady 20° per second for the triangle, and 15° per second for the square.
 static NSTimeInterval lastDrawTime;
if (lastDrawTime)
{
NSTimeInterval timeSinceLastDraw = [NSDate timeIntervalSinceReferenceDate] - lastDrawTime;
rtri+=20.0 * timeSinceLastDraw;
rquad-=15.0 * timeSinceLastDraw;
}
lastDrawTime = [NSDate timeIntervalSinceReferenceDate];
Another thing you can do - it won't change the functionality, but will help during debugging - is to keep the rotation within a 360° scope, which can be done simply by adding a couple of lines of code:
 static NSTimeInterval lastDrawTime;
if (lastDrawTime)
{
NSTimeInterval timeSinceLastDraw = [NSDate timeIntervalSinceReferenceDate] - lastDrawTime;
rtri+=20.0 * timeSinceLastDraw;
if (rtri > 360.0)
rtri -= 360.0;

rquad-=15.0 * timeSinceLastDraw;
if (rquad < 0.0)
rquad += 360.0;
}
lastDrawTime = [NSDate timeIntervalSinceReferenceDate];

No comments:

Post a Comment