コード例 #1
0
ファイル: cueBall.cs プロジェクト: rypcer/PhysicsSimulations
    void boundaryCollision()
    {
        // Border Line points
        ballIndex      = 0;
        LineStartPoint = new Vector3(borders[ballIndex].position.x - ballRadius, 0, borders[ballIndex].position.z);
        LineEndPoint   = new Vector3(borders[ballIndex].position.x - ballRadius, 0, borders[ballIndex].position.z - (borders[ballIndex].position.z * 2));


        // Vertical X axis positive
        if (VectorLibrary.isPointOnLine(position, LineStartPoint, LineEndPoint))
        {
            // Reflect Velocity
            velocity        = VectorLibrary.getVectorReflection(velocity, true);
            overlapDistance = Mathf.Abs(VectorLibrary.getScalarDistance(position.x + ballRadius, borders[ballIndex].position.x));

            // Transposes ball back if it goes outside the boundary
            position.x -= overlapDistance;
        }

        ballIndex      = 1;
        LineStartPoint = new Vector3(borders[ballIndex].position.x + ballRadius, 0, borders[ballIndex].position.z);
        LineEndPoint   = new Vector3(borders[ballIndex].position.x + ballRadius, 0, borders[ballIndex].position.z - (borders[ballIndex].position.z * 2));

        // Vertical X axis negative
        if (VectorLibrary.isPointOnLine(position, LineStartPoint, LineEndPoint))
        {
            velocity        = VectorLibrary.getVectorReflection(velocity, true);
            overlapDistance = Mathf.Abs(VectorLibrary.getScalarDistance(position.x - ballRadius, borders[ballIndex].position.x));
            position.x     += overlapDistance;
        }


        ballIndex      = 2;
        LineStartPoint = new Vector3(borders[ballIndex].position.x, 0, borders[ballIndex].position.z - ballRadius);
        LineEndPoint   = new Vector3(borders[ballIndex].position.x - (borders[ballIndex].position.x * 2), 0, borders[ballIndex].position.z - ballRadius);

        // Horizontal Z axis positive
        if (VectorLibrary.isPointOnLine(position, LineStartPoint, LineEndPoint))
        {
            velocity        = VectorLibrary.getVectorReflection(velocity, false);
            overlapDistance = Mathf.Abs(VectorLibrary.getScalarDistance(position.z + ballRadius, borders[ballIndex].position.z));
            position.z     -= overlapDistance;
        }

        ballIndex      = 3;
        LineStartPoint = new Vector3(borders[ballIndex].position.x, 0, borders[ballIndex].position.z + ballRadius);
        LineEndPoint   = new Vector3(borders[ballIndex].position.x - (borders[ballIndex].position.x * 2), 0, borders[ballIndex].position.z + ballRadius);

        // Horizontal Z axis negative
        if (VectorLibrary.isPointOnLine(position, LineStartPoint, LineEndPoint))
        {
            velocity        = VectorLibrary.getVectorReflection(velocity, false);
            overlapDistance = Mathf.Abs(VectorLibrary.getScalarDistance(position.z - ballRadius, borders[3].position.z));
            position.z     += overlapDistance;
        }
    }