Sign in to follow this  

Cos/Sin problems

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
From wikipedia

Conversion between radians and degrees

As stated above, one radian is equal to 180/π degrees. Thus, to convert from radians to degrees, multiply by 180/π.

Share this post


Link to post
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 this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this