Thread: Trig Ratios

  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

    Trig Ratios

    Hi,

    I had been calculating movement on a 2-d axis a very complicated way that required lots of code, but I recently have come to understand how you can use cos and sin to calculate the movement of X and Y based of a heading (out of 360 deg.)

    I'm having trouble implementing it though. The way I was attempting to do it was like this:
    Code:
    xShots[count] += sin(shotHeading[count])*0.3f;
    yShots[count] += cos(shotHeading[count])*0.3f;
    Then I realized that the ratios have a pattern (or something like it) every 90 deg. I looked at a nehe tutorial that used this method and it was like this:
    Code:
    xpos -= (float)sin(heading*piover180) * 0.05f;
    zpos -= (float)cos(heading*piover180) * 0.05f;
    Can someone attempt to show me how to do this, or should I just go read a math book?

    David

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The two methods aren't really different. The main point is that sin() and cos() take the angle in radians; the piover180 is the conversion factor.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    So the pie over 180 converts it to the radians? Is it required that I convert it to a radian?

    Edit: Well adding in the pieover180 worked, but I don't know why and that really bugs me.

    Edit again: Never mind, I just didn't read what you wrote well. pi over 180 is the conversion to degrees.

    Thanks!
    Last edited by IdioticCreation; 05-03-2007 at 04:06 PM.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902


    pi / 180 is the conversion to radians. When you multiply by it, you essentially multiply by 1. pi radians is equal to 180 degrees. Go read up about what Radians are.
    Also, read this article as it contains a skill that is invaluable, and will aid you in converting between units. (radians to degrees, feet to miles, or more exotic units.) Has to be one of the greatest things a Chem teacher I had taught me.

    It's "pi", not food.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Thanks for the article. That was a typo eariler, I had looked radians up.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Actually you are still going to run into problems. What is the 0.05f for? Is that your speed?

    Code:
    #define PI 3.14159f
    #define DEGTORAD(angle)       (float)((float)angle * PI / 180.0f)
    
    Pos.x+=cosf(DEGTORAD(AngleInDegrees))*fSpeed;
    Pos.y+=sinf(DEGTORAD(AngleInDegrees))*fSpeed;
    Of course I don't recommend doing the conversion in a time critical loop.

  7. #7
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Well it's not too time critical, but are you saying that I should make the calculation once, then just increment by the number I get each loop? Because the angle isn't going to change in this particular instance.

    And yea the 0.05f was speed.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Cactus_Hugger View Post
    Also, read this article as it contains a skill that is invaluable, and will aid you in converting between units. (radians to degrees, feet to miles, or more exotic units.) Has to be one of the greatest things a Chem teacher I had taught me.
    Dimensional analysis (algebra on units) can even be used to deduce various laws of physics. For instance, the formula for the speed of transverse wave propagation on a string can be derived using dimensional arguments alone, without doing any physical calculations. At least up to various dimensionless constants.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by IdioticCreation View Post
    Well it's not too time critical, but are you saying that I should make the calculation once, then just increment by the number I get each loop? Because the angle isn't going to change in this particular instance.

    And yea the 0.05f was speed.
    Yes that's what he's saying, since addition is faster than division and multiplication.

    Just remember that +1 radian is not +1 degree...

  10. #10
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Well I actually was talking about incriminating the X and Y not the angle. Like calculate it to find how much X and and Y should increase each frame (The Slope) then just add them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. indices with trig frunctions
    By Christine21 in forum Windows Programming
    Replies: 0
    Last Post: 04-17-2005, 08:40 AM
  2. Trig Identities.
    By Krak in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 11-06-2004, 11:42 PM
  3. Expression Manipulator v0.4 (bugs fixed, derivatives work with trig funcs)
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-08-2003, 08:10 AM
  4. IDEA: Trig Function Grapher
    By BMJ in forum Contests Board
    Replies: 4
    Last Post: 08-18-2002, 06:32 PM
  5. trig
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 01-17-2002, 07:15 AM