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

This is a discussion on Should i have to understand those sin(), cos() tricks? within the Game Programming forums, part of the General Programming Boards category; Hello frnds. I m going to make a nice text effect and animation for my friends birthday. while i was ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    14

    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. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    If you want things to move along arcs, or in circles, then some geometry knowledge is useful.

    What sort of resources, maths or programming?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Animating a sprite does not require sin() or cos() but rotating it might.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    14
    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. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    14
    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. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    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&
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    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. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    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. #9
    Registered User
    Join Date
    Dec 2006
    Posts
    14
    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. #10
    village skeptic
    Join Date
    Aug 2008
    Posts
    31

  11. #11
    Registered User
    Join Date
    Jul 2008
    Posts
    71
    Quote Originally Posted by misterMatt View Post
    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

  12. #12
    Registered User
    Join Date
    Dec 2006
    Posts
    14
    Thanks misterMatt for giving link
    it seems interesting
    will study now

  13. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,171
    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.

  14. #14
    village skeptic
    Join Date
    Aug 2008
    Posts
    31
    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.

  15. #15
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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.
    Last edited by VirtualAce; 08-13-2008 at 09:52 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with cos, sin fuctions
    By xxxhunter in forum C Programming
    Replies: 7
    Last Post: 11-16-2007, 02:33 AM
  2. Help with cos, sin and tan^-1 (arctan);
    By xIcyx in forum C Programming
    Replies: 15
    Last Post: 04-23-2007, 09:14 AM
  3. Help creating function of sin cos and tan
    By Niz in forum C Programming
    Replies: 12
    Last Post: 01-09-2007, 04:55 PM
  4. sin() and cos() that use degrees
    By dwks in forum C Programming
    Replies: 3
    Last Post: 05-14-2005, 04:29 PM
  5. integral of 1/x
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 03-19-2004, 06:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21