Alright, so I've got hold of some simple physics mechanics, and am currently trying to implement bouncing. Based on the first answer of this question, I've developed the following algorithm:
Vector2 velocity;
float gravity;
float elasticity;
Hitbox dummy;
// ---
void bounce() {
dummy.MoveY(velocity.Y);
velocity.Y += gravity;
if (dummy.ThereWasContact.South) velocity.Y *= -elasticity;
}
Gravity can be any value, and Elasticity is always between 0.0f
and 1.0f
. Velocity.Y's initial value can also be anything.
Weight factors aside, which would determine a minimal rebound force for the object to be able to leave the ground again and (probably) aren't related to the problem, this is apparently fallacious.
What happens here is somewhat like this:
If I drop an object from a height of 128 units, for example, 0u marking ground height, with a gravity of 1u and elasticity of 0.9, the object would bounce once, reach a maximum height of ~126u from the rebound, and from then on keep alternating on maximum heights between this and ~122u.
This example is based on one of my own experiments, in which I drop an object from a height of 64 pixels and there is a floor at 192 pixels. The maximum height will keep going from ~66 pixels to ~70.
What am I missing? Does weight after all do more than I think? If I need to explain something better or details appear to be missing, let me know.