• 15
• 15
• 11
• 9
• 10

# Cos/Sin problems

This topic is 3692 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Im trying to move my sprites around screen by giving them angles. My code is:
int Physics::CalcX(int x,float ang, int vel)
{

ang=(180 / 3.1415926535897932) * ang;

float scalex=sin(ang);

float xvel = scalex * vel ;

x+=xvel;

return x;
}

int Physics::CalcY(int y,float ang, int vel)
{
ang=(180 / 3.1415926535897932) * ang;

float scaley=cos(ang);

float yvel = scaley * vel;

y+=yvel;

return y;
}


The angles given are always 0,90,180 or 270, but the sprites seem to move in arbitrary directions. Is there anything im obviously doing wrong?

##### Share on other sites
It looks like you might have your degrees-to-radians conversion backwards (you should use a constant or named function for that, by the way).

##### Share on other sites
From wikipedia

Conversion between radians and degreesAs stated above, one radian is equal to 180/&#960; degrees. Thus, to convert from radians to degrees, multiply by 180/&#960;.

##### Share on other sites
Quote:
 Original post by ConorHFrom wikipedia*** Source Snippet Removed ***

Right, but you don't want to convert from radians to degrees. Quite the opposite, in fact.

##### Share on other sites
Quote:
 Original post by ConorHFrom wikipedia*** Source Snippet Removed ***

But sin and cos take radians as input, not degrees.

##### Share on other sites
Quote:
 Original post by ConorHFrom wikipedia*** Source Snippet Removed ***
Right - but don't you want a conversion from degrees to radians?

##### Share on other sites
my bad, but even with this ammended code, its moving in weird directions

##### Share on other sites
I assume CalcX takes an angle in degrees. In that case, you'll want to multiply the angle by M_PI/180 to convert to radians.

I just went through this last night. I can't count how many times I've made this mistake. I implemented a separation axis text only to find it didn't work. Turns out it worked fine first try, I just wasn't rotating my polygon data correctly.

##### Share on other sites
Quote:
 Original post by ConorHIm trying to move my sprites around screen

This sounds like a job for vectors.