Making particle bounce off a line with friction
- by Dlaor
So I'm making a game and I need a particle to bounce off a line. I've got this so far:
public static Vector2f Reflect(this Vector2f vec, Vector2f axis) //vec is velocity
{
Vector2f result = vec - 2f * axis * axis.Dot(vec);
return result;
}
Which works fine, but then I decided I wanted to be able to change the bounciness and friction of the bounce. I got bounciness down...
public static Vector2f Reflect(this Vector2f vec, Vector2f axis, float bounciness) //Bounciness goes from 0 to 1, 0 being not bouncy and 1 being perfectly bouncy
{
var reflect = (1 + bounciness); //2f
Vector2f result = vec - reflect * axis * axis.Dot(vec);
return result;
}
But when I tried to add friction, everything went to hell and back...
public static Vector2f Reflect(this Vector2f vec, Vector2f axis, float bounciness, float friction) //Does not work at all!
{
var reflect = (1 + bounciness); //2f
Vector2f subtract = reflect * axis * axis.Dot(vec);
Vector2f subtract2 = axis * axis.Dot(vec);
Vector2f result = vec - subtract;
result -= axis.PerpendicularLeft() * subtract2.Length() * friction;
return result;
}
Any physics guys willing to help me out with this?
(if you're not sure what I mean with the friction of a bounce see this: http://www.metanetsoftware.com/technique/diagrams/A-1_particle_collision.swf)