# moving ball at using sin and cos...need help

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

## Recommended Posts

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

##### Share on other sites
Try:
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.

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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....

##### Share on other sites
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.

##### Share on other sites
finally got it moving right....i kept everything a float all the way untill it made the SDL_Rect and then i just casted it as an int....works fine now....thanks for the help

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633331
• Total Posts
3011390
• ### Who's Online (See full list)

There are no registered users currently online

×