views:

331

answers:

1

I'm trying to better understand what exactly this code is doing. It's written in Objective-C, but should be familiar to anyone with a C background. What exactly are sin/cos mathematics doing here? Also, does anyone have a good recommendation for learning trig for gaming concepts such as these?

for (int i = 0; i < GAME_CIRCLES; i++)
{
 point.x = center.x - sin (degree) * RADIUS;
 point.y = center.y + cos (degree) * RADIUS;
 mPieRect[i] = CGRectMakeWithCenter (point, RADIUS - 4);
 degree += PI / 3.0;
}
+7  A: 

That is the parametric equation for a circle (see wikipedia)

I'll guess that the "degree" variable is actually in radians rather than degrees though. There are 360 degrees in a circle, or 2*Pi radians.

By advancing the degree variable by Pi/3, it's stepping around 1/6 of a circle

Paul Dixon
Thanks Paul! Makes sense. Quick question though, how are the x/y coords figured out, what exactly is center.x - sin ( degree ) * RADIUS and the subsequent line doing?
randombits
that calculates the position from the center point at angle 'degree' and radius 'RADIUS'. If you were to loop the degree from 0...2PI in small increments and plot the resulting points, you'd get a circle.
Paul Dixon
Also, I would guess that GAME_CIRCLES is 6 to make the loop step around 6 points of a hexagon, and so it stores a rectangle centered on each point in the mPieRect array.
Paul Dixon
Paul: any advice on a good book to catch on these concepts? Trying to get the fundamentals down for basic game programming.
randombits
I don't want to sound too negative, but that's pretty basic trig :( - however, check out the answers to this question http://stackoverflow.com/questions/188030/mathematics-and-game-programming
Paul Dixon
Trigonometric functions are not directly related to game programming. Or to programming in general. This is a kind of math you can use to describe (and in this case draw) something which lies on a circle. You could try http://en.wikipedia.org/wiki/Trigonometric_functions#Sine for a start, or google for "sine" if this is too advanced.
drhirsch