Esempio n. 1
0
    /*
     * This method detects collision between a cannonball and a point on the string
     * If a collision is detected, the string moves away from the cannonball
     * NOTE: If the segment length of the string is larger than the collision radius of the cannonball,
     * than a collision may not be detected
     */
    private void DetectAndMoveOnStringCollision()
    {
        foreach (GameObject cannonball in OperateCannons.activeCannonballs) // For each active cannonball
        {
            if (cannonball == null)
            {
                continue;
            }

            CannonballMotion motion           = cannonball.GetComponent <CannonballMotion>(); // Get the CannonballMotion component of the cannonball
            Vector3          cannonballCenter = new Vector3(motion.GetXPosition(), motion.GetYPosition(), 0f);
            Vector3          centerToPoint;                                                   // The vector from the center of the cannonball to a point on the string
            float            distance;                                                        // The distance between the center and the point

            // For each point of the string, except the point connected to the body
            for (int i = 1; i < NUM_STRING_POINTS; i++)
            {
                centerToPoint = cannonballCenter - currentStringPoints[i];
                distance      = centerToPoint.magnitude;

                // If the distance from the center of the cannonball to the point is less than or equal to the collision radius of the cannonball
                if (System.Math.Abs(distance) <= CannonballCollisionDetection.collisionRadius + COLLISION_BUFFER_ZONE)
                {
                    // The point is inside the cannonball
                    float error = CannonballCollisionDetection.collisionRadius + COLLISION_BUFFER_ZONE - distance;
                    float direction; // The direction in which to correct the position of the string points

                    // Set the direction to the direction the cannonball is moving
                    if (motion.GetXVelocity() > 0)
                    {
                        direction = 1f; // Right direction
                    }
                    else
                    {
                        direction = -1f; // Left direction
                    }

                    // For the string point and every string point below it
                    for (int j = i; j < NUM_STRING_POINTS; j++)
                    {
                        currentStringPoints[j].x += 2f * error * direction; // Correct the x position of the point
                        currentStringPoints[j].y += 2f * error;             // Correct the y position of the point (always upward)
                    }
                }
            }
        }
    }