moving ball at using sin and cos...need help
im using sdl and trying to move a ball around the screen....it can move at any angle...and i use the cos and sin funtions to get my xmove and ymove variables....my code looks like this:
balldir = 25;
ballspeed = 10;
//add some friction
ballspeed-=0.05;
//move the ball
int xmove = cos(balldir*3.14/180)*ballspeed;
int ymove = sin(balldir*3.14/180)*ballspeed;
ball.xadd(xmove);
ball.yadd(ymove);
when i put friction onto the ball to try to slow it down it caused the ball to shack back and forth a bit as it was moving at the proper angle....is there a better way to determain the xmove and ymove variables....
any help is appreciated
Try:
With friction being between 0 and 1.0. This may help, and is clearer to read anyway.
float xmove = cos(balldir*3.14/180);float ymove = sin(balldir*3.14/180);xmove *= ballspeed * friction;ymove *= ballspeed * friction;...
With friction being between 0 and 1.0. This may help, and is clearer to read anyway.
i tried that method but it doesnt seem tro want to work....the path of the ball is still wobbly and doesnt run a straight line
can you give a better explanation of whats going on, and or post an exe and your source.
its likely to be a rounding problem from float to int somewhere.
its likely to be a rounding problem from float to int somewhere.
yeah your right....i think its a rounding problem....but i dont think there is a way to fix it becuase SDL_Rect only take integer values....so somewhere its gotta to be rounded down....
im just try to make a mini putt game....i have the ball moving around the course and bouncing off walls....
im ive usually just moved objects along specific x or y coordinates....but i need the ball to be able to go in all directions.....i have a variable for the angle....and im trying to convert that angle into x and y movement for the ball....
im just try to make a mini putt game....i have the ball moving around the course and bouncing off walls....
im ive usually just moved objects along specific x or y coordinates....but i need the ball to be able to go in all directions.....i have a variable for the angle....and im trying to convert that angle into x and y movement for the ball....
if I cant look at your source all I can suggest is changing all values to floats so there is no numerical problems and letting the conversion occur on input to the function.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement