The function that comes to mind is a y=abs(cos(x)) with a simple iterator which decreases the peak after each jump. |
A more practical approach is to have a gravity constant that is applied to the ball each frame.
Any object that moves should have 2 basic parts:
1) An x,y position
2) An x,y speed
Every update (typically every frame) you apply motion by adding the speed to the position, then you apply acceleration and other forces to the speed (ie, if the player hits a wall and you want them to stop, you force their speed to zero).
In the case of a bouncing ball, you would do 2 things:
1) apply gravity by adding a constant gravitational value to the ball's y speed every frame.
2) when the ball hits the ground, you negate its Y speed (having it bounce up again), but apply some kind of dampener to absorb some of the shock. IE: multiplying by 1.0 would mean it bounces the same height every time... less would mean it bounces less, and greater would mean it bounces higher and higher each time.
Try to avoid using trig functions like sin/cos unless you are sure it's the best (or only) way to solve the problem at hand.
But that's not even the issue. My problem is blitting doubles onto the screen instead of integers. |
Well you should be drawing
strings, not integers. In which case all you have to do is convert a double to a string and then print the string... done more or less the same way you'd convert an integer to a string.
1 2 3 4 5 6 7 8 9 10 11 12
|
//assuming you have a function like this:
void printstring( const char* str ); // which prints the given string
// with stringstream:
stringstream ss;
ss << yourdouble;
printstring( ss.str().c_str() );
// with c-style strings and sprintf
char buffer[30];
sprintf("%f", yourdouble );
printstring( buffer );
|