# Making particle bounce off a line with friction

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)

• Wouldn't it just be a matter of multiplying the component velocity parallel to the surface by the desired friction? – tyjkenn Mar 11 '12 at 0:51

## 1 Answer

Here's the physics answer. (I'm not sure exactly what bounciness and all refer to, but this should be easy to calculate)

Let's take a particle moving towards the wall with uy, and parallel to it with ux. It's coefficient of restitution is e (akin to 'bounciness', for perfectly bouncy it's 1. For normal items it's less than 1). Also, let the friction coefficient be m (The correct mathematical symbol for this is mu, represented as μ. But since that's visually similar to the 'u' value we're already using, we'll instead use 'm' for clarity). A friction coefficient is usually greater than 1.

Now, the resultant velocities will be:

• vy = -euy
• vx = ux + m(e-1)uy
• there's something wrong here: after your edits, the friction coefficient no longer is taken into account... – Mac Mar 29 '12 at 5:53
• @Mac: Oh whoops, I forgot that I'd used "m" for friction coefficient. To me, It looked like a dimensional error, since m usually refers to mass--I though I'd neglected to cancel it out. I had come to this question to quickly copy the formulae for use here, and the physicist in me evidently didn't like the dimensional error. – Manishearth Mar 29 '12 at 6:00