# Thread: Should i have to understand those sin(), cos() tricks?

1. ## Should i have to understand those sin(), cos() tricks?

Hello frnds. I m going to make a nice text effect and animation for my friends birthday. while i was reffering some code, i found that they have used some sin(), cos() technique to animate sprite on screen.
Should i need to learn them too? if so can u give me resources to learn them. Plz help

2. If you want things to move along arcs, or in circles, then some geometry knowledge is useful.

What sort of resources, maths or programming?

3. Animating a sprite does not require sin() or cos() but rotating it might.

4. Thank u bubba & salem for giving reply. But i think that i can't tell u what i wanted to say. here is the piece of code i have refered. It uses SDL to draw on screen some wavy animation.

Code:
``` void render()
{
// Lock surface if needed
if (SDL_LockSurface(screen) < 0)
return;

// Ask SDL for the time in milliseconds
int tick = SDL_GetTicks();

// Declare a couple of variables
int i, j, d, yofs, ofs;

// Draw to screen
for (i = 0, yofs = 0, d = 0; i < 480; i++, yofs += screen->pitch / 4)
for (j = 0, ofs = yofs; j < 640; j++, ofs++, d++)
((unsigned int*)screen->pixels)[ofs] = 0;

for (i = 0; i < 128; i++)
{
d = tick + i * 10;
drawsprite((int)(320 + sin(d * 0.003459734f) * 300),
(int)(240 + sin(d * 0.003345973f) * 220),
((int)(sin((tick * 0.2 + i) * 0.234897f) * 127 + 128) << 16) |
((int)(sin((tick * 0.2 + i) * 0.123489f) * 127 + 128) <<  8) |
((int)(sin((tick * 0.2 + i) * 0.312348f) * 127 + 128) <<  0));
}
// Unlock if needed
if (SDL_MUSTLOCK(screen))
SDL_UnlockSurface(screen);

// Tell SDL to update the whole screen
SDL_UpdateRect(screen, 0, 0, 640, 480);
}```

5. Friends, I just wanted to know is there any way we could learn to use such sin() , cos() functions systematically or we have to do it by trial and error method? Plz help me.

If possible plz suggest me some openGL tutorial. I wanted to make some cool text effect to wish my friend on her birthday. thanks in advance.

6. Like I said, how to use them at all is founded in the mathematics of geometry.

Of course, it then takes some skill as a programmer to use them effectively, because they're not cost-free functions to use.

For OpenGL
http://nehe.gamedev.net/lesson.asp?index=01

As for maths, that's harder - what's your math education like to begin with?
http://clusty.com/search?v&#37;3afile=vi...ec=1218047999&

7. The sin and cos functions are simply mathematical functions, belonging to trigonometry. There are some pretty good information on how those operations behave on wikipedia.

8. If you dont know the maths its probably best dealing with one co-ordinate at a time. Basically sin() or cos() return a value between -1 and 1. The simplest usage i can think of would be to make an object move up and down along a sine wave. The formula is basically:

angle = x_position % 360;
y_position = sin(angle) * MAXIMUM_Y_DISTANCE_FROM_CENTER;

Then if you just incrment the x_position in a loop the object will move up and down. Also in C you have to convert from degrees to radians, but thats not too hard to do.

9. Thank u all for replying. I think u mean that only thing i can learn is how to use function and i have to ''invent" the way to use it to get some complecated rendering. thanks for replaying.

10. Originally Posted by misterMatt
misterMatt,thank you for posting that links.I am learning on how to apply my newly learn trigo into programming but can't figure out how.
well until you posted that link

11. Thanks misterMatt for giving link
it seems interesting
will study now

12. What is your current level of math education? I remember simple trig functions to be taught in grade 9 or 10, with more advanced topics like waves and identities taught in grade 12.

13. Unfortunately, the public education system here in the states completely ignores proofs entirely. So whatever the kiddies learn in math class - they won't understand why it works how it works.

Trig functions were a perfect example of how math education failed me in high school. We get taught the Pythagorean theorem, but not how it works.

14. If they also taught you the distance formula you would know perfectly well why the Pythagorean theorem works. What irritates me more than anything is people who cannot think for themselves and must be spoon fed every formula. So if the schools don't teach them they don't learn it. Just memorizing a formula for a test or memorizing some proof does NOT mean you understand it. This is where schools fail miserably. Unfortunately there is no clear cut way to measure understanding as opposed to memorization or to differentiate between the two.

And BTW cos() and sin() are not tricks. They are proven mathematical concepts. Their application in say a game, however, is a bit limited since cos() and sin() are not exactly cheap to compute in real time. Usually these are computed once to compute direction and then are re-computed at such a time as they need to be.

For example:
Code:
```   x += cosf(angle) * speed * timeDelta;
y += sinf(angle) * speed * timeDelta;```
Would not be good in a loop that updated 100 objects. It is 'possible' that the compiler would optimize the invariant code and replace it with constants. If angle never changes then the cosf() and sinf() also never change. I would rather not rely on this and would specifically code it so that these were pre-computed. This is how I programmed my asteroids game. Each asteroid's velocity vector is computed at creation time and never changes. Same with missiles and other projectiles in the game.