Exemple #1
0
    void FixedUpdate()
    {
        if (moving && gunBallSpawned)
        {
            if (initial)
            {
                initial = false;

                if (xDifference == 0)
                {
                    //rotates the cannon by the calculated angle
                    boat.transform.rotation = Quaternion.Euler(-angle, 0, 0);
                }
                else
                {
                    //gamma/alpha rotation of the cannon
                    boat.transform.rotation = Quaternion.Euler(-angle, PhysicsCalculator.toDegrees(gamma), 0);
                }
            }

            angle = PhysicsCalculator.calculateTheta(range, gunBallVelocity) * 180 / Mathf.PI;

            newVx = PhysicsCalculator.calculateXVelocityWithWind(fixedTime, tau, oldVx, windCoefficient, windVelocity, gamma, dragCoefficient);
            newVy = PhysicsCalculator.calculateYVelocityWithWind(dragCoefficient, fixedTime, projMass, oldVy);
            newVz = PhysicsCalculator.calculateZVelocityWithWind(fixedTime, tau, oldVz, windCoefficient, windVelocity, gamma, dragCoefficient);

            newDx = PhysicsCalculator.calculateXPositionWithWind(currentDx, oldVx, tau, fixedTime, windCoefficient, windVelocity, dragCoefficient, gamma);
            newDy = PhysicsCalculator.calculateYPositionWithWind(currentDy, oldVy, tau, fixedTime);
            newDz = PhysicsCalculator.calculateZPositionWithWind(oldDz, oldVz, tau, fixedTime, windVelocity, gamma, windCoefficient, dragCoefficient);

            if (gunball.transform.position.y <= 0.05 && numUpdates > 0)
            {
                moving        = false;
                timeText.text = "Time: " + ((numUpdates + 1) * fixedTime) + " seconds";
            }
            else
            {
                if (xDifference == 0)
                {
                    if (target.transform.position.z > boat.transform.position.z)
                    {
                        gunball.transform.Translate(gunball.transform.position.x, newVy * fixedTime, newVx * fixedTime);
                    }
                    else
                    {
                        gunball.transform.Translate(gunball.transform.position.x, newVy * fixedTime, -newVx * fixedTime);
                    }
                }
                else
                {
                    //handles any quadrant case
                    if (target.transform.position.x < boat.transform.position.x && target.transform.position.z < boat.transform.position.z)
                    {
                        gunball.transform.Translate(-newVx * fixedTime, newVy * fixedTime, -newVz * fixedTime);
                    }
                    else if (target.transform.position.x < boat.transform.position.x)
                    {
                        gunball.transform.Translate(-newVx * fixedTime, newVy * fixedTime, newVz * fixedTime);
                    }
                    else if (target.transform.position.z < boat.transform.position.z)
                    {
                        gunball.transform.Translate(newVx * fixedTime, newVy * fixedTime, -newVz * fixedTime);
                    }
                    else
                    {
                        gunball.transform.Translate(newVx * fixedTime, newVy * fixedTime, newVz * fixedTime);
                    }
                }

                if (lastMarker + buffer < numUpdates)
                {
                    lastMarker = numUpdates;
                    Object.Instantiate(flightMarker, new Vector3(gunball.transform.position.x, gunball.transform.position.y + yDisplacement, gunball.transform.position.z + zDisplacement), Quaternion.identity);
                }
            }
        }

        if (moving && gunBallSpawned)
        {
            numUpdates++;

            //update UI
            posText.text     = "Position: " + gunball.transform.position.x + ", " + gunball.transform.position.y + ", " + gunball.transform.position.z;
            timeText.text    = "Time: " + ((numUpdates + 1) * fixedTime) + " seconds";
            updatesText.text = "Updates: " + (numUpdates + 1) + " frames";

            oldVx = newVx;
            oldVy = newVy;
            oldVz = newVz;

            currentDx = newDx;
            currentDy = newDy;
            oldDz     = newDz;
        }
    }