private void FixedUpdate() { // Apply custom gravity to ship. Vector3 gravity = NBodySimulation.CalculateAcceleration(rb.position); rb.AddForce(gravity, ForceMode.Acceleration); // Apply thruster force. Vector3 thrustDir = transform.TransformVector(thrust); rb.AddForce(thrustDir * thrustMultiplier, ForceMode.Acceleration); // Rotate while airborne if (numCollisionTouches == 0) { rb.MoveRotation(smoothRotation); } // Check if the pilot is outside of the ship. if (FindObjectOfType <FirstPersonController>() != null) { // If so, check the distance between the ship and pilot. if (Vector3.Distance(FindObjectOfType <FirstPersonController>().transform.localPosition, transform.localPosition) < 3f) { shipInRange = true; } else { shipInRange = false; } } }
void SimulatePhysics() { SceneManager.SetActiveScene(parallelScene); GameObject simulationObject = Instantiate(mainObject); GameObject simulationSphere3 = Instantiate(sphere3); GameObject simulationSphere = Instantiate(sphere); GameObject simulationSphere2 = Instantiate(sphere2); GameObject simulationSphere4 = Instantiate(sphere4); simulationObject.GetComponent <Rigidbody>().velocity = mainObject.GetComponent <Rigidbody>().velocity + initalVelocity; simulationObject.GetComponent <Rigidbody>().angularVelocity = mainObject.GetComponent <Rigidbody>().angularVelocity; simulationSphere.GetComponent <Rigidbody>().velocity = sphere.GetComponent <Rigidbody>().velocity; simulationSphere.GetComponent <Rigidbody>().angularVelocity = sphere.GetComponent <Rigidbody>().angularVelocity; simulationSphere2.GetComponent <Rigidbody>().velocity = sphere2.GetComponent <Rigidbody>().velocity; simulationSphere2.GetComponent <Rigidbody>().angularVelocity = sphere2.GetComponent <Rigidbody>().angularVelocity; simulationSphere3.GetComponent <Rigidbody>().velocity = sphere3.GetComponent <Rigidbody>().velocity; simulationSphere3.GetComponent <Rigidbody>().angularVelocity = sphere3.GetComponent <Rigidbody>().angularVelocity; simulationSphere4.GetComponent <Rigidbody>().velocity = sphere4.GetComponent <Rigidbody>().velocity; simulationSphere4.GetComponent <Rigidbody>().angularVelocity = sphere4.GetComponent <Rigidbody>().angularVelocity; for (int i = 0; i < lineRenderer.positionCount; i++) { foreach (StellarBody sb in StellarBody.StellarBodies) { if (!(sb.gameObject.scene.name == parallelScene.name)) { continue; } NBodySimulation.SimulateStellarSystem(Parallel_trajectory.parallelScene.name, sb); } parallelPhysicsScene.Simulate(Time.fixedDeltaTime); lineRenderer.SetPosition(i, simulationObject.transform.position); } Destroy(simulationObject); Destroy(simulationSphere3); Destroy(simulationSphere2); Destroy(simulationSphere); Destroy(simulationSphere4); SceneManager.SetActiveScene(mainScene); }
void FixedUpdate() { // Gravity Vector3 gravity = NBodySimulation.CalculateAcceleration(rb.position); rb.AddForce(gravity, ForceMode.Acceleration); // Thrusters Vector3 thrustDir = transform.TransformVector(thrusterInput); rb.AddForce(thrustDir * thrustStrength, ForceMode.Acceleration); if (numCollisionTouches == 0) { rb.MoveRotation(smoothedRot); } }
// Use this for initialization void Start() { //Get the simulation variable NBodySimulation[] sims = gameObject.GetComponents <NBodySimulation>(); if (sims == null) { Debug.LogWarning("Warning: No NBodySimulation attached to object " + gameObject.name + " which has NBodyDemoMode component; demo will not run."); return; } if (sims.Length != 1) { Debug.LogWarning("Warning: More than one NBodySimulation attached to object " + gameObject.name + "; using first found."); } sim = sims[0]; //Load saved preferences if (loadPlayerPrefs) { LoadPlayerPrefsValues(); } //Generate Body Objects GBody[] bodies = new GBody[numberOfBodies]; for (int i = 0; i < bodies.Length; i++) { GameObject renderObject; if (i == trailsBodyIndex) { renderObject = (GameObject)GameObject.Instantiate(trailsObjectPrefab); trail = (GameObject)GameObject.Instantiate(trailObject); trail.transform.parent = renderObject.transform; trailsBodyObject = renderObject; } else { renderObject = (GameObject)GameObject.Instantiate(bodyObjectPrefab); } renderObject.transform.parent = this.transform; Vector3 pos = UnityEngine.Random.insideUnitSphere; pos.Scale(genRadii); Vector3 vel = UnityEngine.Random.insideUnitSphere; vel.Scale(genVelocityRange); if (perpendicularVelocities) { vel = new Vector3(pos.z / perpendicularVelocitiesScaling, 0, -pos.x / perpendicularVelocitiesScaling); } float mass = UnityEngine.Random.Range(genMinMass, genMaxMass); bodies[i] = new GBody(pos, vel, mass, renderObject, renderScale); if (i == trailsBodyIndex) { trailsBody = bodies[i]; } } sim.SetGBodyObjects(bodies); lookCamera.gameObject.AddComponent <SimpleObjectLogger>(); trailsBodyObject.AddComponent <SimpleObjectLogger>(); //Configure look response parameters startMass = trailsBody.Mass; lookMass = startMass * massScaling; startScale = trailsBodyObject.transform.localScale; lookScale = trailsBodyObject.transform.localScale * sizeScaling; //Configure particle emitter particles = trailsBodyObject.GetComponent <ParticleSystem>(); particles.enableEmission = false; //Populate UI element components progressSlider = accuracySliderObject.GetComponent <Slider>(); progressSlider.maxValue = sim.GetGBodyObjects().Length; itemsRemainingText = itemsRemainingTextObject.GetComponent <Text>(); timerText = timerTextObject.GetComponent <Text>(); bestScoreText = bestScoreTextObject.GetComponent <Text>(); totalTaskTimeText = totalTaskTimeTextObject.GetComponent <Text>(); //Initialize High Score UI Component bestScoreText.text = "\r\nBest Score\r\n" + bestScore.ToString("F3") + "s"; //Initialize System State Parameters isLooking = false; startTime = DateTime.Now; restartTime = DateTime.Now; if (PlayerPrefs.HasKey("showTime")) { bool showTime = PlayerPrefs.GetFloat("showTime") != 0; if (!showTime) { totalTaskTimeTextObject.GetComponent <Text>().enabled = false; } } if (PlayerPrefs.HasKey("taskTime")) { taskTime = PlayerPrefs.GetFloat("taskTime") * 60; } totalTaskTimeText.text = taskTime + "s"; log = gameObject.GetComponent <Logger>(); log.GenerateLoggableObjectsList(); log.BeginLogging(); }
void Start() { self = this; NewtonSetup(); }