Original Post
🔒 Locked
bouncing ball
Advertisement
If you are talking about realistically animating a bouncing ball, then the only acceleration that should come into play is gravity, and gravity should be constant.
If you want your ball to bounce up to less than the height it reached on the last bounce, then the solution for that is to simulate air friction by constantly multiplying the balls velocity by a number very close to, but just under, 1.
When the ball hits the ground you simply reverse it's vertical velocity
Here's some code for a bouncing ball...
If you want your ball to bounce up to less than the height it reached on the last bounce, then the solution for that is to simulate air friction by constantly multiplying the balls velocity by a number very close to, but just under, 1.
When the ball hits the ground you simply reverse it's vertical velocity
Here's some code for a bouncing ball...
const int GROUND_HEIGHT = 1;const int GRAVITY = 0.098;const int AIR_FRICTION_MULTIPLIER = 0.999;if (pos.y < GROUND_HEIGHT) vel.y = -abs(vel.y)vel.y += GRAVITY;vel *= AIR_FRICTION_MULTIPLIER;pos += vel;renderTheBall(pos);
Quote:
if (pos.y < GROUND_HEIGHT)
vel.y = -abs(vel.y)
I had issue with this some time ago. If time delta is a large enough, ball stucks somwhere in plane.
1)
\/
-----------
2)
----\/-----
3)
-----------
\/
case 1 is when ball is falling down or flying up and everything is ok.
but cases 2 and 3 can occour, when pos.y is below plane. Imagine that ball is falling and test stops in case 3 because of large timestep, velocity is reversed but next timestep is not 1, but 2 instead (because of friction). Velocity gets reversed again and you get case 3 again, prolly even further below plane.
I found a simple way to fix this by adding 1 more line:
if (pos.y < GROUND_HEIGHT){pos.x=0vel.y = -abs(vel.y)}
Quote:That problem will happen if you use:
Original post by _Madman_ Quote:
if (pos.y < GROUND_HEIGHT)
vel.y = -abs(vel.y)
I had issue with this some time ago. If time delta is a large enough, ball stucks somwhere in plane.
1)
\/
-----------
2)
----\/-----
3)
-----------
\/
case 1 is when ball is falling down or flying up and everything is ok.
but cases 2 and 3 can occour, when pos.y is below plane. Imagine that ball is falling and test stops in case 3 because of large timestep, velocity is reversed but next timestep is not 1, but 2 instead (because of friction). Velocity gets reversed again and you get case 3 again, prolly even further below plane.
I found a simple way to fix this by adding 1 more line:if (pos.y < GROUND_HEIGHT){pos.x=0vel.y = -abs(vel.y)}
vel.y = -vel.y;You'll see that I postedvel.y = -abs(vel.y);which will only ever convert a positive into a negative. A negative will stay negative. Zeroing the x position must be a mistake. Perhaps you meant to write:pos.y = GROUND_HEIGHT;however, to do it absolutely properly you'd make the ball travel through a vector composed of the portion up until the collision, plus the reflected portion of the distance remaining. Here's the whole thing:const float GROUND_HEIGHT = 240;const float CEILING_HEIGHT = 0;const float LEFT_SIDE = 0;const float RIGHT_SIDE = 320;const float GRAVITY = 0.098;const float AIR_FRICTION_MULTIPLIER = 0.999;vel.y += GRAVITY;vel *= AIR_FRICTION_MULTIPLIER;pos += vel;if (pos.y > GROUND_HEIGHT){ vel.y = -abs(vel.y); pos.y = 2 * GROUND_HEIGHT - pos.y;}else if (pos.y < CEILING_HEIGHT){ vel.y = abs(vel.y); pos.y = 2 * CEILING_HEIGHT - pos.y;}if (pos.x > RIGHT_SIDE){ vel.x = -abs(vel.x); pos.x = 2 * RIGHT_SIDE - pos.x;}else if (pos.x < LEFT_SIDE){ vel.x = abs(vel.x); pos.x = 2 * LEFT_SIDE - pos.x;}renderTheBall(pos);Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement