Trig question...

This is a discussion on Trig question... within the Game Programming forums, part of the General Programming Boards category; I wasn't sure what board this belonged on (if any), but since it pertains to the creation of my game ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    11

    Trig question...

    I wasn't sure what board this belonged on (if any), but since it pertains to the creation of my game I put it here.

    For those of you who are familiar with the following code:

    Code:
    x += cos(angle)*speed;
    y += -sin(angle)*speed;
    Basically, this increases the X/Y in the direction that the 'thing' is facing (angle-wise).

    What I need to do is the reverse of this. After I get the x/y values, how can I determine the angle?

    Thanks!

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    Just reverse the operations!
    if i haven't made some idiotinc error... the angle can be found like so,

    angle = acos(x / speed);
    or
    angle = -asin(y / speed);
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    y += -sin(angle)*speed;
    Why not y -= sin(angle)*speed;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    But acos and asin require the use of sqrt(). Both of them are very slow - quicker to normalize the vector created by subtracting the points or to do two separate linear interpolations between the points.

    Do not use euler angles to do this - very slow.


    But if you must look up arccos and arcsin on www.google.com. There is C code all over the net that shows how to do these.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    11
    Originally posted by Magos
    Why not y -= sin(angle)*speed; :p
    It's merely a question of preference. There's no right or wrong way, unless it's inefficient. ;)

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Using arctangent is not the right way to do it.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    My two cents.
    All measure angle as 0 = North, 90 = East.
    All measure angle in degrees, not radians.
    All are probably bloody slow.
    Code:
    // ===============================================
    double GetLineAngle(double startX, double startY, double endX, double endY) {
    	double dblTemp;
    
    	dblTemp = atan2(endY - startY, endX - startX);
    	if (isnan(dblTemp)) return 0;
    
    	dblTemp = ((180 / M_PI) * dblTemp) + 90;
    
    	if (dblTemp < 0) return dblTemp + 360;
    	else if (dblTemp > 360) return dblTemp - 360;
    	else return dblTemp;
    }
    
    // ===============================================
    double GetXCoordinate(double dblDistance, double dblDegrees) {
    	return cos( (dblDegrees - 90) * (M_PI / 180) ) * dblDistance;
    }
    
    
    // ===============================================
    double GetYCoordinate(double dblDistance, double dblDegrees) {
    	return sin( (dblDegrees - 90) * (M_PI / 180) ) * dblDistance;
    }

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Code:
    #define MIN (a,b) (if (a<b)?a:b)
    
    struct point2D
    {
      double x;
      double y;
      point2D(tx,ty) {x=tx;y=ty;};
    };
    
    double FastDist2D(double dx,double dy)
    {
      int mn=(int)MIN(dx,dy);
      
      return (dx+dy-(mn>>1)-(mn>>2)+(mn>>4));
    }
    
    
    point2D GetInterceptVector(point2D Source,point2D Target)
    {
      double diffx=Source.x-Target.x;
      double diffy=Source.y-Target.y;
      double dist=FastDist2D(diffx,diffy);
      return point2D(diffx/dist,diffy/dist);
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  3. Trig question
    By Robert602 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-12-2004, 05:58 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

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