I am trying to implement free fall motion of a ball towards a plane.
I implemented collision detection and if collision happens, I change sign of velocity of a ball so it bounces off and climbs back up.
Now, it should climb back to its original position if there is no loss of energy, but after some testing I realized that after each bounce I have less speed than the bounce before.
Part of my code related to this:
if (collision)
{
velocity.y = -velocity.y;
std::cout << velocity.y << std::endl;
}
velocity.y += gravityAccel * ts.getSeconds();
position.x += velocity.x * ts.getSeconds() + ent.getComponent<RigidBody>().InitVelocity.x * ts.getSeconds();
position.y += velocity.y * ts.getSeconds() + ent.getComponent<RigidBody>().InitVelocity.y * ts.getSeconds();
position.z += velocity.z * ts.getSeconds() + ent.getComponent<RigidBody>().InitVelocity.z * ts.getSeconds();
I think that my problem lies in the calculations that happen right before ball reaches its maximum height. I think that he ball has some speed that is less than the speed change calculated by the next time step, so what I get is negative speed and reversal of motion direction along y axes a bit before it should actually happen. If this is so, I am trying to figure out the best way to solve this problem and have relatively nice free fall motion.
I know, you could say that this is not a bug but a feature since I have energy loss without asking for it. :)
velocity
andent.getComponent<RigidBody>().InitVelocity
to the position? What does this latter velocity represent, that's distinct from the velocity stored in thevelocity
variable? \$\endgroup\$velocity
field a single time, at the moment of the throw? \$\endgroup\$