Can someone help me out. I need the formula for a bouncing ball for a game that I am making. Thanks
Printable View
Can someone help me out. I need the formula for a bouncing ball for a game that I am making. Thanks
There is no universal formula that describes how balls bounce. There are equations of motion, friction, spin etc.
Be more specific.
Well atm I just have a ball falling in a 3D world and when it hits the ground I want to do something to simulate a bounce at the moment I have something like this and it works very badly.
if ball.z < 0{
zspeed(the updward motion)+=grav+zspeed
}
ball.z+=zspeed
ball.z-=grav
It all depends on how complex your game is, eg how 'life-like' it simulates the ball.
The easiest example of a bouncing ball is probably by using the projectile motion equations. I will try to explain it a bit using a 2D-world but it translates quite similar to 3D I think.
Basically, you give the ball a horizontal speed vx and a vertical speed of vy = 0 (you release it from rest) (for example, you could do it differently).
Then, the horizontal, x, component of the speed will NOT change (unless it bounces against a vertical surface like a wall).
The vertical, y, component of the speed will be constantly changing due to gravity.
Basically, the y component is given by:
vy = 0 + 1/2 g t2
Here, g is the acceleration of gravity (9.8 m/s[sup2[/sup]) and t is the time.
This is just for a free-falling ball.
As soon as the ball hits the ground, it has a certain vertical speed and this vertical speed is then 'mirrored'. The equation then becomes:
vy = - vbounce - 1/2 g t2
Here, vbounce is the ball's speed at the time of the bounce (note that this speed will be negative, hence the negative sign to make it positive (up) again).
If your game is more complex you must take into account the spin of the ball. Assuming that it is moving horizontally (not just bouncing on a certain position) then each time it hits the ground, the friction with the ground will cause it to spin. When it is spinning and it hits the ground, it is given a certain 'punch' (impulse) in the other direction. (For example take a ball and drop it on the floor while spinning rapidly. You will see that it bounces off in a certain direction instead of just up). This is all much more complicated though and I don't think this is what you want, right?
Alright I half understood that. Here is what I am doing now. It is not really working so ye.
when the ball hits the ground this is what I am doing
firstly I declare the variables
time=0;
zspeed=0;(updward force)
while on_floor=0
grav+=0.7(my gravity the downward force
alright
while z>ground
time+=1
and then on the bounce event here is what I am doing
when it hits the ground
(the speed at the time)zspeed=-grav-zspeed-1/2*grav*time.
Now this mucks up pretty bad it just flickers on the ground. So can you point out what is wrong
Sounds like you're re-applying downward speed directly after the bouncing, so it starts moving upwards and you immediately tell it to start going down again.