Search code examples
c#unity-game-enginegame-physics

How to apply friction to this physics model?


I'm trying to make my little small engine in order to make a table Puck slider game like this one :

Then , i'm having problems to move pucks well , cause i'm using velocity normal to make aceleration be opposite to velocity vector:

        velocity = ((Vector2) (initMousePosition - Input.mousePosition)).normalized*1.42f*Mathf.Min(1f,((Vector2) (initMousePosition - Input.mousePosition)).magnitude/Screen.height);
        acceleration = - velocity.normalized;
 ...

This are our usefull vars to resolve my problem:

Board.COEFFICIENTE_OF_FRICTION = 0.1f;

public Vector2 position;

public Vector2 velocity;

public Vector2 acceleration;

The code:

private static void Move()
{
    foreach(BoardObject obj in objects)
    {
        if (obj is Puck)
        {
            auxPuck = (Puck) obj;
            if (auxPuck.velocity.magnitude > 0f)
            {
                auxPuck.acceleration += (-auxPuck.acceleration * 9.8f*auxPuck.mass*Board.COEFFICIENTE_OF_FRICTION*Time.deltaTime);
                auxPuck.velocity +=  auxPuck.acceleration * Time.deltaTime;
                auxPuck.position += auxPuck.velocity * Time.deltaTime;

                if (VectorsChangeMagnitudeSign(auxPuck.velocity,auxPuck.velocity-auxPuck.acceleration*Time.deltaTime))
                {
                    auxPuck.acceleration = Vector2.zero;
                    auxPuck.velocity = Vector2.zero;
                }
            }
        }
    }
}

Maybe i'll must use acceleration like a float , or starting with another vector that's not the normalized of velocity. My problem comes when velocity got higher magnitude , it seems to have an strange remaining velocity that makes it moves constantly at the end.

Any idea of how must i do this the good way to run it smothly?


Solution

  • Sorry i have found the problem . I told you . I started to use normalized vector , when i throw the puck , the problem comes cause the puck changes direction vs walls , then the accel rest was decreasing the friction force , causing the const velocity.

    Sorry for your time , and thanks anyway.