3 posts
• Page **1** of **1**

You can do the motion with vars for velocityX, velocityY and gravity. Then the kinetic motion can be calculated with:

x = x + velocityX*dt

velocityY = velocityY + gravity*dt

y = y + velocityY*dt

To make it bounce there are a few approaches. One simple one it to move horizontally first, then vertically. For either motion the idea is to:

1. move

2. if ball is overlapping wall then unto the move and reverse the velocity.

To make the ball always bounce back up to the same height is a bit trickier. In an idea world we would bounce at the exact point of collision, which can be calculated but even then there will be rounding errors over time.

A simpler idea would be to conserve total energy. AKA total_energy=potential_energy+kinetic_energy

Potential energy (PE) is the height of the object off the ground times gravity, and kinetic energy (KE) is the speed squared divided by two.

PE = -y*gravity

KE = 0.5*speed^2

As the ball moves energy is transferred back and forth between PE and KE, and if energy is conserved then the total energy (E) will always be the same.

E = KE + PE

This is useful because with that we can calculate what the speed is for any y. You can work out the algebra yourself but it comes out to:

speed = sqrt(2*gravity*(y - start_y))

So with that we can correct the speed so the ball always bounces to the same height.

x = x + velocityX*dt

velocityY = velocityY + gravity*dt

y = y + velocityY*dt

To make it bounce there are a few approaches. One simple one it to move horizontally first, then vertically. For either motion the idea is to:

1. move

2. if ball is overlapping wall then unto the move and reverse the velocity.

To make the ball always bounce back up to the same height is a bit trickier. In an idea world we would bounce at the exact point of collision, which can be calculated but even then there will be rounding errors over time.

A simpler idea would be to conserve total energy. AKA total_energy=potential_energy+kinetic_energy

Potential energy (PE) is the height of the object off the ground times gravity, and kinetic energy (KE) is the speed squared divided by two.

PE = -y*gravity

KE = 0.5*speed^2

As the ball moves energy is transferred back and forth between PE and KE, and if energy is conserved then the total energy (E) will always be the same.

E = KE + PE

This is useful because with that we can calculate what the speed is for any y. You can work out the algebra yourself but it comes out to:

speed = sqrt(2*gravity*(y - start_y))

So with that we can correct the speed so the ball always bounces to the same height.

You do not have the required permissions to view the files attached to this post.

**Posts:**5,238**Reputation:**67,762

B

91
S

31
G

103
3 posts
• Page **1** of **1**