I am testing collision between numerous moving squares in a quadtree.
When two square collides, I invert their velocity, like this:
for (auto pair : collisionPairs)
{
auto& square1 = static_cast<Square&>(*(pair.first));
auto& square2 = static_cast<Square&>(*(pair.second));
square1.setVelocity(-square1.getVelocity());
square2.setVelocity(-square2.getVelocity());
}
The problem is, collision detection is not always during one frame.
Sometimes, they are collision between same squares more than one frame, because the squares have been intersecting to deep.
So squares overlap and they keep reversing their velocity infinitely, which results a 0 velocity ( -velocity + velocity = 0 )
How could I fix this ?
EDIT : I've discovered that the deep intersection issue was caused by my quadtree, I forgot to check collision with the objects of children trees.
Now it STILL happens but very rarely, like 1 in 100 collisions, before it was ~20 from 100 collisions