Cos/Sin problems

Started by
7 comments, last by Driv3MeFar 16 years, 2 months ago
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?
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).
From wikipedia

Conversion between radians and degreesAs stated above, one radian is equal to 180/π degrees. Thus, to convert from radians to degrees, multiply by 180/π. 
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.
Quote:Original post by ConorH
From wikipedia

*** Source Snippet Removed ***


But sin and cos take radians as input, not degrees.
Quote:Original post by ConorH
From wikipedia

*** Source Snippet Removed ***
Right - but don't you want a conversion from degrees to radians?
my bad, but even with this ammended code, its moving in weird directions
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.
Quote:Original post by ConorH
Im trying to move my sprites around screen


This sounds like a job for vectors.

This topic is closed to new replies.

Advertisement