Jump to content
  • Advertisement
Sign in to follow this  
ConorH

Cos/Sin problems

This topic is 3955 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
Advertisement
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 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
Quote:
Original post by ConorH
From wikipedia

*** Source Snippet Removed ***


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

Share this post


Link to post
Share on other sites
Quote:
Original post by ConorH
From wikipedia

*** Source Snippet Removed ***
Right - but don't you want a conversion from degrees to radians?

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!