예제 #1
0
    void FixedUpdate()
    {
        if (moving && gunBallSpawned)
        {
            if (initial)
            {
                initial = false;
                timeBeg = Time.time;
                //rotates the cannon by the calculated angle
                boat.transform.rotation = Quaternion.Euler(-angle, 0, 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: " + totalTime + " seconds";
            }
            else
            {
                yDisplacement = newVy * fixedTime;
                zDisplacement = newVx * fixedTime;
                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);
                }
            }
        }

        if (moving && gunBallSpawned)
        {
            //update UI
            initialVelText.text = "Init. Velocity: " + gunBallVelocity + " m/s";
            rangeText.text      = "Range: " + range + " m";
            timeText.text       = "Time: " + (Time.time - timeBeg) + " seconds";
            updatesText.text    = "Updates: " + (numUpdates + 1) + " frames";
            gunBallText.text    = "Gunball Z: " + gunball.transform.position.z + " m";

            numUpdates++;
            oldVx     = newVx;
            oldVy     = newVy;
            currentDx = newDx;
            currentDy = newDy;
        }
    }
예제 #2
0
    void FixedUpdate()
    {
        //calculate what frame it is
        t = numUpdates * Time.deltaTime;

        if (moving)
        {
            if (useDrag)
            {
                newD = PhysicsCalculator.calculateDistanceDrag(currentD, oldV, Time.fixedDeltaTime, dragConstant);
                newV = PhysicsCalculator.calculateVelocityDrag(oldV, acceleration, Time.fixedDeltaTime, dragConstant);

                if (t >= dt)
                {
                    moving = false;
                }
            }
            else
            {
                newD = PhysicsCalculator.calculateDistance(currentD, acceleration, oldV, Time.fixedDeltaTime);
                newV = PhysicsCalculator.calculateVelocity(oldV, acceleration, Time.fixedDeltaTime);

                if (t >= time)
                {
                    moving = false;
                }
            }

            timeText.text     = "Time: " + t + " seconds";
            frameText.text    = "Frame: " + numUpdates + " frames";
            velocityText.text = "Velocity: " + oldV + " m/s";
            distanceText.text = "Distance: " + currentD + " m";
        }

        //need to recheck if moving before going to next frame.
        //moving could be changed to false before it reaches here so need to recheck.
        if (moving)
        {
            boat.transform.Translate(Vector3.forward * oldV * Time.deltaTime);
            currentD = newD;
            oldV     = newV;
            numUpdates++;
        }
    }
예제 #3
0
    void FixedUpdate()
    {
        if (moving)
        {
            if (numUpdates == 100 && !dynamicControls)
            {
                moving = false;
            }

            //calculate the new distances and velocities
            newDx = PhysicsCalculator.calculateDistance(oldDx, acceleration.x, oldVx, fixedTime);
            newVx = PhysicsCalculator.calculateVelocity(oldVx, acceleration.x, fixedTime);

            newDz = PhysicsCalculator.calculateDistance(oldDz, acceleration.z, oldVz, Time.fixedDeltaTime);
            newVz = PhysicsCalculator.calculateVelocity(oldVz, acceleration.z, Time.fixedDeltaTime);


            if (rotLeft)
            {
                temp1 = accelerationL;
            }

            if (rotRight)
            {
                temp1 = accelerationR;
            }

            angularVelocity = oldAngularVelocity + (temp1 * fixedTime);

            angularDisplacement += Mathf.Abs(angularVelocity * fixedTime);

            distanceX += newVx * fixedTime;
            distanceZ += newVz * fixedTime;

            //------- LAB #7 Additions -------
            tau  = PhysicsCalculator.calculateTau(mass_com, dragCoefficient);
            vMax = PhysicsCalculator.calculteTerminalVelocity(force, dragCoefficient);

            newVz = PhysicsCalculator.calculateVelocityWithDrag(vMax, dragCoefficient, oldVz, fixedTime, mass_com);
            newDz = PhysicsCalculator.calculatePositionWithDrag(oldDz, force, dragCoefficient, fixedTime, mass_com, oldVz);

            accelerationWithDrag = PhysicsCalculator.calculateAccelerationWithDrag(force, dragCoefficient, oldVz, mass_com);

            totalBoat.transform.Translate(newVx * fixedTime, 0, newVz * fixedTime);
        }

        if (moving)
        {
            numUpdates++;

            //update UI
            timeText.text = "Time: " + ((numUpdates) * fixedTime) + " seconds, " + (numUpdates) + " updates";
            posText.text  = "Velocity: " + newVz + "m/s";
            angDText.text = "Acceleration " + accelerationWithDrag + " m/s^2";
            disText.text  = "Distance: " + newDz + " m";

            oldDx = newDx;
            oldVx = newVx;

            oldDz = newDz;
            oldVz = newVz;
            oldAngularVelocity = angularVelocity;
        }
        else
        {
            v_final = oldVz;
        }
    }
예제 #4
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;

            //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 (Mathf.Abs(gunball.transform.position.z - target.transform.position.z) < 1 && numUpdates > 0 && !collided)
            {
                pause();
                timeText.text = "Time: " + ((numUpdates + 1) * fixedTime) + " seconds";
            }
            else
            {
                if (xDifference == 0)
                {
                    gunball.transform.Translate(gunball.transform.position.x, gunball.transform.position.y, 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 (!collided)
                    {
                        gunball.transform.Translate(gunball.transform.position.x, gunball.transform.position.y, gunBallVelocity * fixedTime);
                        target.transform.Translate(0, 0, targetVelocity * fixedTime);
                    }
                    else
                    {
                        gunball.transform.Translate(bulletXFinal * fixedTime, gunball.transform.position.y, bulletZfinal * fixedTime);
                        target.transform.Translate(targetXFinal * fixedTime, target.transform.position.y, targetZfinal * 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     = "Masses (Ball/Target): " + projMass + "kg, " + targetMass + "kg";
            timeText.text    = "Time: " + ((numUpdates + 1) * fixedTime) + " seconds";
            updatesText.text = "Updates: " + (numUpdates + 1) + " frames";

            oldVx = newVx;
            oldVz = newVz;

            currentDx = newDx;
            currentDy = newDy;
            oldDz     = newDz;
        }
    }
예제 #5
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;
        }
    }
예제 #6
0
    void FixedUpdate()
    {
        if (moving)
        {
            if (Mathf.Abs(dinZ) - distanceZ <= 0.05)
            {
                moving = false;
            }

            if (angularDisplacement >= rD && rotRight)
            {
                moving = false;
            }

            if (angularDisplacement >= lD && rotLeft)
            {
                moving = false;
            }

            //calculate the new distances and velocities
            newDx = PhysicsCalculator.calculateDistance(oldDx, acceleration.x, oldVx, fixedTime);
            newVx = PhysicsCalculator.calculateVelocity(oldVx, acceleration.x, fixedTime);

            newDz = PhysicsCalculator.calculateDistance(oldDz, acceleration.z, oldVz, Time.fixedDeltaTime);
            newVz = PhysicsCalculator.calculateVelocity(oldVz, acceleration.z, Time.fixedDeltaTime);

            if (rotLeft)
            {
                temp1 = accelerationL;
            }

            if (rotRight)
            {
                temp1 = accelerationR;
            }

            angularVelocity = oldAngularVelocity + (temp1 * fixedTime);

            angularDisplacement += Mathf.Abs(angularVelocity * fixedTime);

            float temp = Mathf.Rad2Deg * (angularVelocity * fixedTime);


            //update the position of the boat
            // totalBoat.transform.position = new Vector3(totalBoat.transform.position.x + newVx * fixedTime, totalBoat.transform.position.y, totalBoat.transform.position.z + newVz * fixedTime);
            totalBoat.transform.Translate(newVx * fixedTime, 0, newVz * fixedTime);
            totalBoat.transform.Rotate(0, temp, 0, Space.Self);

            distanceX += newVx * fixedTime;
            distanceZ += newVz * fixedTime;
        }

        if (moving)
        {
            numUpdates++;

            //update UI
            timeText.text = "Time: " + ((numUpdates) * fixedTime) + " seconds, " + (numUpdates) + " updates";
            posText.text  = "Position: " + distanceX + ", " + totalBoat.transform.position.y + ", " + distanceZ;
            angDText.text = "Ang Displacement: " + angularDisplacement;

            oldDx = newDx;
            oldVx = newVx;

            oldDz = newDz;
            oldVz = newVz;
            oldAngularVelocity = angularVelocity;
        }
        else
        {
            v_final = oldVz;
        }
    }