1. ## 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. 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);

3. y += -sin(angle)*speed;
Why not y -= sin(angle)*speed;

4. 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. 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. Using arctangent is not the right way to do it.

7. 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. 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);
};```