Making particle bounce off a line with friction

Posted by Dlaor on Game Development See other posts from Game Development or by Dlaor
Published on 2012-03-10T22:49:04Z Indexed on 2012/03/29 5:42 UTC
Read the original article Hit count: 227

Filed under:
|

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)

© Game Development or respective owner

Related posts about c#

Related posts about physics