-
1 Attachment(s)
Bounce - Dealing With It
I detect a collision between some object and another, and I get a normal to the surface where the collision occured, and the point of that collision. I try to sort of pretend that the velocity vector of the colliding object and the surface normal make like an 'angle of incidence'. And then I go farther and deal with that whole dealiothingy in put it into the context of a polar system sort of. Looks like this (multiplication operator does dot product):
Code:
double incidence = std::acos( -v_ball.getNormalized() * normal );
// Into polar (theta, radius) form
double theta = std::acos( -v_ball.getNormalized() * CVector(1, 0) ) + incidence * 2;
double radius = v_ball.length();
// Into cartesian x, y form
CVector out = CVector( std::cos( theta ) * radius, std::sin( theta ) * radius );
m_ball.setVelocity( out );
So, basically my logic is like this. It's wrong I guess, because the ball goes all over the place unpredictably after the collision, but, it's my logic.
-
Given you know the normal vector of the surface and the velocity vector of the ball -
Ball.VelocityVector*=Surface.Normal;
Given both are expressed as unit or normalized vectors.
-
I ended up using the formula for vector reflection:
http://www.cse.iitb.ac.in/~adityagp/final/node3.html
( v_ball.getNormalized() - 2 * ( v_ball.getNormalized() * normal ) * normal ) * v_ball.length();
The multiplication operator denoting a dot product operation when operating on vectors there.
>> Ball.VelocityVector*=Surface.Normal;
What will that give me? It didn't seem to work in my program (even after scaling that vector)
-
I'm not sure if this works, might be worth a try though:
Ball.vel = Ball.vel - 2 * absolute(normal) * ball.vel
Worked for me on normal = (-1, 0) and (0, 1), not sure if it's the general case though.
Good luck :)
Wazaa