コード例 #1
0
    //function for the friction on the horizontal ground
    void applyFrictionOfGround(CannonBall ball)
    {
        //use a friction coefficient to decelerate the ball
        float frictionCoeff = 0.95f;

        ball.addVelocity(new Vector2(-ball.getVelocity().x *(1 - frictionCoeff), 0));
    }
コード例 #2
0
    //function for the bouncing solution of collison, taking a ball and a line represented by two points
    //as parameters
    void bouncingAfterTerrainCollision(CannonBall ball, float x1, float y1, float x2, float y2)
    {
        //calculate bouncing velocity using the formula V+ = V- + dotProduct(j,n)/m,
        //where V- is the velocity when colliding, V+ is the velocity after colliding,
        //dotProduct(j,n) = J which is the impulse, and j = -(1+epsilon) * m * Vn- where
        //Vn- = dotProduct(V-,n).

        Vector2 unitNormal = CollisionCalculation.getNormal(x1, y1, x2, y2);                          //calculate the unit normal of the line
        float   epsilon    = 0.55f;                                                                   //the coefficient of restitution (0~1, when it's 1 we have perfect bouncing)
        float   mass       = 1f;                                                                      //mass of the ball

        float VnCollided = unitNormal.x * ball.getVelocity().x + unitNormal.y * ball.getVelocity().y; //Vn-: the normal component of collided velocity = dot product of normal and velocity

        float   j       = -(1 + epsilon) * mass * VnCollided;                                         //the impulse scalar j = -(1+epsilon) * m * Vn-
        Vector2 impulse = j * unitNormal;                                                             //J

        ball.addVelocity(impulse / mass);                                                             //update the velocity based on the impulse (V+ = V- + impulse/mass)
    }