Exemplo n.º 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);
                }
            }



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

                newVx = PhysicsCalculator.calculateVelocity(oldVx, 0, fixedTime);
                newVy = PhysicsCalculator.calculateVelocity(oldVy, ACCELERATION, fixedTime);

                //update positions accordingly
                newDx = PhysicsCalculator.calculateDistance(currentDx, 0, newVx, fixedTime);
                newDy = PhysicsCalculator.calculateDistance(currentDy, ACCELERATION, newVy, fixedTime);

                if (gunball.transform.position.y - target.transform.position.y <= 0.05 && numUpdates > 0)
                {
                    moving        = false;
                    timeText.text = "Time: " + ((numUpdates + 1) * fixedTime) + " seconds";
                }
                else
                {
                    yDisplacement = newVy * fixedTime;
                    zDisplacement = newVx * fixedTime;

                    if (target.transform.position.z < boat.transform.position.z)
                    {
                        gunball.transform.position = new Vector3(gunball.transform.position.x, gunball.transform.position.y + yDisplacement, gunball.transform.position.z - zDisplacement);
                    }
                    else
                    {
                        gunball.transform.position = new Vector3(gunball.transform.position.x, gunball.transform.position.y + yDisplacement, gunball.transform.position.z + zDisplacement);
                    }

                    // 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);
                    // }
                }
            }
            else
            {
                if (gunball.transform.position.y - target.transform.position.y <= 0.05 && numUpdates > 0)
                {
                    moving        = false;
                    timeText.text = "Time: " + ((numUpdates + 1) * fixedTime) + " seconds";
                }
                else
                {
                    //run lab 4
                    newVx = PhysicsCalculator.calculateVelocity(oldVx, 0, fixedTime);
                    newVy = PhysicsCalculator.calculateYVelocityGamma(oldVy, -ACCELERATION, fixedTime);
                    newVz = PhysicsCalculator.calculateVelocity(oldVz, 0, fixedTime);

                    newDx = PhysicsCalculator.calculateXPosition(currentDx, oldVx, angle, gamma, fixedTime);
                    newDy = PhysicsCalculator.calculateYPosition(currentDy, oldVy, -ACCELERATION, fixedTime);
                    newDz = PhysicsCalculator.calculateZPosition(oldDz, oldVz, angle, PhysicsCalculator.toDegrees(gamma), fixedTime);

                    if (target.transform.position.x < boat.transform.position.x && target.transform.position.z < boat.transform.position.z)
                    {
                        gunball.transform.position = new Vector3(gunball.transform.position.x - newVx * fixedTime, gunball.transform.position.y + newVy * fixedTime, gunball.transform.position.z - newVz * fixedTime);
                    }
                    else if (target.transform.position.x < boat.transform.position.x)
                    {
                        gunball.transform.position = new Vector3(gunball.transform.position.x - newVx * fixedTime, gunball.transform.position.y + newVy * fixedTime, gunball.transform.position.z + newVz * fixedTime);
                    }
                    else if (target.transform.position.z < boat.transform.position.z)
                    {
                        gunball.transform.position = new Vector3(gunball.transform.position.x + newVx * fixedTime, gunball.transform.position.y + newVy * fixedTime, gunball.transform.position.z - newVz * fixedTime);
                    }
                    else
                    {
                        gunball.transform.position = new Vector3(gunball.transform.position.x + newVx * fixedTime, gunball.transform.position.y + newVy * fixedTime, gunball.transform.position.z + newVz * fixedTime);
                    }
                }
            }

            omegaF = PhysicsCalculator.calculateOmegaFinal(omegaI, alphaI, (numUpdates + 1) * fixedTime);
            theta  = PhysicsCalculator.calculateThetaFinal(omegaI, alphaI, (numUpdates + 1) * fixedTime);
            angularDisplacement     = PhysicsCalculator.calculateAngularDisplacement(theta, omegaF, alphaI, fixedTime);
            totalAngleDisplacement += angularDisplacement;

            //rotate the ball
            gunball.transform.Rotate(new Vector3(-PhysicsCalculator.toDegrees(angularDisplacement), 0, 0));
        }

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

            //update UI
            gammaText.text   = "Angular Velocity: " + omegaF;
            angleText.text   = "Acceleration: " + theta;
            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;
        }
    }