hi, everyone, I am trying to simulate a bouncing ball in Allegro but don't quite know the physics, please help me please. Now I just want it to have velocity and gravity, no friction. What I see some people do is that they do something like this:
vel_y += gravity ... ball.Y += vel_y ... if(ball.Y > SCREEN_H - ball.height) { vel_y = -vel_y }
That doesn't work very well because the ball bounce higher and higher, what I want is to have the ball keep bounce up to the same level, and when I add friction I want it to bounce lower and lower.
Well, instead of doing it the way you did, you should have gravity change. When the ball should bounce, make gravity a negative value. Each "step" (logic loop):
if (gravity < max_gravity) gravity += 0.01;
myobj.Y += gravity;
when I add friction I want it to bounce lower and lower.
It isn't friction which makes the ball bounce lower.
Well, instead of doing it the way you did, you should have gravity change.
Not if he wants a realistic physical simulation.
if(ball.Y > SCREEN_H - ball.height)
{
vel_y = -vel_y
}
This is the part that should change when you want the ball to bounce lower; for example by changinge "vel_y = -vel_y" to "vel_y = -vel_y * elasticity" where elasticity is between 0 and 1, a value of 0 corresponding to 'no bounce' and a value of 1 corresponding to 'full bounce'.
As to the ball bouncing higher and higher, it's probably because you're using a numerical method which gives inexact results, Euler integration. (edit: Although from the code you posted I think it would go lower and lower if anything ...)
are you sure you don't have these lines:
vel_y += gravity ... ball.Y += vel_y
reversed?
Zaphos is right. Listen to Zaphos and you will get a realistic bouncing ball.
I don't quite understand your method Elverion, is there no velocity if I use your method? and I just change the gravity value to achieve the result?
are you sure you don't have these lines:
vel_y += gravity
...
ball.Y += vel_y
reversed?
yes, I checked the code again and I am sure of it. I don't know why the ball bouncing higher and higher, that seems a very odd physics.
It isn't friction which makes the ball bounce lower.
Is it the gravity then?
I tried vel_y = -vel_y * elasticity, I set elasticity to 0.9 and the ball starts to bounce higher after some times, when I set elasticity to 0.8 the ball bounce lower for a while then bounce no lower, it bounce at the same level. I don't get it, I still need help
Do you think it is because you are using integer value variables? If so, change them to floating point variables. When using the variable as an int later, just use "(int)variable".
No, I am using float.
Is it the gravity then?
No, it's a property of the ball and the ground; some materials conserve kinetic energy better than others. A collision which conserves kinetic energy is an elastic collision. Collisions in the 'real world' are not fully elastic, as there is always some energy dissipation on impact.
To some extent it is also an effect of air drag, but it is not caused by ground friction or gravity.
I don't get it, I still need help
I feel it might help to see more of the code; without seeing it all I can say is maybe you should start worrying about the impact point more precisely, perhaps with something like:
vel_y += gravity ... oldY = ball.Y ball.Y += vel_y ... if(ball.Y > SCREEN_H - ball.height) { fraction = (ball.Y - SCREEN_H - ball.height) / (ball.Y - oldY); vel_y -= gravity * fraction vel_y = -vel_y * elasticity if (vel_y * gravity > 0) vel_y = 0; ball.Y = SCREEN_H - ball.height + vel_y * fraction; }
edit:
is there no velocity if I use your method? and I just change the gravity value to achieve the result?
I would say it is more that he is conflating gravity and velocity, so his gravity variable corresponds to your velocity variable, his hardcoded .01 corresponds to your gravity variable, and then he limits velocity ... which I suppose can be seen as a very simple approximation of air drag, although of course it will have no effect unless you're bouncing high enough to reach the maximum velocity.
If I were doing a bouncing ball, I'd have gravity as a negative constant. The velocity moving across the floor in x & y would remain constant without friction (air, energy absorption by ball deformation etc). Gravity always increases the balls vertical fall (where "going up" is simply a sign change). If the ball was dropped at some given height above the floor, it's vertical motion would be zero at the exact instant of release, then the vertical motion would increase as the gravity was added in each logic loop. When the ball hits the floor, simply change the sign of the vertical velocity, then continue to add the gravity constant. The ball would then slow down relative to the floor, and eventually approach the floor again.
Arthur: That's what miica neo was already doing.