Jump to content
  • Advertisement
Sign in to follow this  
Stone_Devil

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.

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

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


Link to post
Share on other sites
Advertisement
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 this post


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


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


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


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


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

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!