// Use this for initialization void Start() { numUpdates = 0; moving = true; gunBallSpawned = false; initial = true; lastMarker = numUpdates; buffer = 5; fixedTime = Time.fixedDeltaTime; oldVz = gunBallVelocity; range = PhysicsCalculator.calculateRange(boat, target); xDifference = PhysicsCalculator.calculateXDifference(boat, target); collided = false; if (xDifference != 0) { // gamma = PhysicsCalculator.calculateGamma(xDifference, PhysicsCalculator.calculateRange(boat, target)); // gammaDegrees = PhysicsCalculator.toDegrees(gamma); // angle = PhysicsCalculator.calculateAngle(ACCELERATION, xDifference, range, gunBallVelocity); // angleAdjusted = (180 - PhysicsCalculator.toDegrees(angle)) / 2; // angle = PhysicsCalculator.toDegrees(angle) / 2; // oldVz = PhysicsCalculator.calculateGammaVelocity(gunBallVelocity, Mathf.Cos(gamma), Mathf.Sin(angleAdjusted * Mathf.PI / 180)); // oldVy = PhysicsCalculator.calculateGammaYVelocity(gunBallVelocity, angleAdjusted); // oldVx = PhysicsCalculator.calculateGammaVelocity(gunBallVelocity, Mathf.Sin(gamma), Mathf.Sin(angleAdjusted * Mathf.PI / 180)); } else { //calculate theta angle = PhysicsCalculator.calculateAngle(ACCELERATION, xDifference, range, gunBallVelocity); //change angle to degrees angle = PhysicsCalculator.toDegrees(angle) / 2; //calculate Vx oldVx = PhysicsCalculator.calculateXVelocity(gunBallVelocity, angle); //calculate Vy oldVy = PhysicsCalculator.calculateYVelocity(gunBallVelocity, angle); } //---------------- Lab #8 Additions ---------------- tau = PhysicsCalculator.calculateTau(projMass, dragCoefficient); windVelocity = 2.0f; windCoefficient = 0.1f; //---------------- Lab #9 Additions ---------------- jImpulse = PhysicsCalculator.calculateJImpulse((gunBallVelocity - targetVelocity), e, projMass, targetMass); momentumInitial.x = PhysicsCalculator.calculateMomentum(projMass, gunBallVelocity); momentumInitial.y = PhysicsCalculator.calculateMomentum(targetMass, targetVelocity); momentumInitial.z = momentumInitial.x + momentumInitial.y; }
// Use this for initialization void Start() { numUpdates = 0; range = PhysicsCalculator.calculateRange(boat, target); moving = true; gunBallSpawned = false; initial = true; lastMarker = numUpdates; buffer = 3; fixedTime = Time.fixedDeltaTime; xDifference = PhysicsCalculator.calculateXDifference(boat, target); if (xDifference != 0) { gamma = PhysicsCalculator.calculateGamma(xDifference, PhysicsCalculator.calculateRange(boat, target)); gammaDegrees = PhysicsCalculator.toDegrees(gamma); angle = PhysicsCalculator.calculateAngle(ACCELERATION, xDifference, range, gunBallVelocity); angleAdjusted = (180 - PhysicsCalculator.toDegrees(angle)) / 2; angle = PhysicsCalculator.toDegrees(angle) / 2; oldVz = PhysicsCalculator.calculateGammaVelocity(gunBallVelocity, Mathf.Cos(gamma), Mathf.Sin(angleAdjusted * Mathf.PI / 180)); oldVy = PhysicsCalculator.calculateGammaYVelocity(gunBallVelocity, angleAdjusted); oldVx = PhysicsCalculator.calculateGammaVelocity(gunBallVelocity, Mathf.Sin(gamma), Mathf.Sin(angleAdjusted * Mathf.PI / 180)); } else { //calculate theta angle = PhysicsCalculator.calculateTheta(range, gunBallVelocity); //change angle to degrees angle = angle * 180 / Mathf.PI; //calculate Vx oldVx = PhysicsCalculator.calculateXVelocity(gunBallVelocity, angle); //calculate Vy oldVy = PhysicsCalculator.calculateYVelocity(gunBallVelocity, angle); } }
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; } }
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; } }
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; } }