How to stop this infinite loop
I'm making a newtonian physics simulator as my computer science project but for some reason Unity keeps freezing as soon as I run the program and the whole application stops responding and the only way to stop it is through ending it with the task manager, however I suspect that the program has just entered an infinite loop as when I check the task manager it is still using up memory and the CPU. I really need this fixed as the deadline is in less than 2 weeks, any help on this would be greatly appreciated.
Here is the code I currently have for it, if anyone would like to use some of it for their own project then by all means use it.
UI:
using System;
using UnityEngine;
using UnityEngine.UI;
public class UI : MonoBehaviour
{
public InputField massInput;
public InputField ivInput;
public InputField angleInput;
public GameObject Ball;
public GameObject Cube;
public GameObject Car;
public Text Error_Mass;
public Text Error_IV;
public Text Error_Angle;
public Text Error_Dropdown;
public static int mass;
public static int initialVelocity;
public static int angle;
public Dropdown objects;
//Use this for initialization
void Start () {
massCatch();
ivCatch();
angleCatch();
objectCatch();
}
//This detects what object was chosen in the dropdown box and then destroys the game objects that were not chosen.
public void objectCatch()
{
if (objects.value != 0)
{
Error_Dropdown.text = "";
}
if (objects.value == 1)
{
DestroyObject(Cube);
DestroyObject(Car);
}
else if (objects.value == 2)
{
DestroyObject(Ball);
DestroyObject(Car);
}
else if (objects.value == 3)
{
DestroyObject(Ball);
DestroyObject(Cube);
}
}
public void angleCatch()
{
string angleInputString = angleInput.text;
if (angleInputString == "")
{
}
else
{
angle = Convert.ToInt32(angleInputString);
if (angle > 0)
{
Error_Angle.text = "";
}
else
{
Error_Angle.text = "Please input suitable starting angle (0 - 90):";
}
}
}
//This takes the value inputted for the initial velocity and converts it into a variable to be used for calculations.
public void ivCatch()
{
string velocityInputString = ivInput.text;
if (velocityInputString == "")
{
}
else
{
initialVelocity = Convert.ToInt32(velocityInputString);
if (initialVelocity > 0)
{
Error_IV.text = "";
}
else
{
Error_IV.text = "Please input suitable initial velocity:";
}
}
}
//This takes the value inputted for the mass and converts it into a variable to be used for calculations.
public void massCatch()
{
string massInputString = massInput.text;
if (massInputString == "")
{
}
else
{
mass = Convert.ToInt32(massInputString);
if (mass > 0)
{
Error_Mass.text = "";
}
else
{
Error_Mass.text = "Please input suitable mass:";
}
}
}
//Update is called once per frame
void Update()
{
}
}
Forces:
using System;
using UnityEngine;
using UnityEngine.UI;
public class Forces : MonoBehaviour {
public int objectMass = 0;
public int initialVelocity = 0;
public int theta = 0; //initial angle of movement set by user in the UI
private double hori_U;
private double vert_U;
private double g = 9.8; //gravity
private double t;
private double vert_V;
private double hori_V = 0;
private double hori_S;
private double vert_V_S;
private double depth_S;
public double horizontalAcceleration = 0;
public double verticalAcceleration = 0;
public static double horizontalForce = 0;
public static double verticalForce = 0;
public static double vert_Sm = 0;
public GameObject Ball;
public GameObject Cube;
public GameObject Car;
public Dropdown Dropdown;
public Vector3 movementUpdate;
// Use this for initialization
void Start() {
objectSettings();
}
//This sets the initial starting position that all of the game objects will go to when they are picked
public void objectSettings()
{
if (Dropdown.value == 1)
{
Ball.transform.position = new Vector3(16, 27, 285);
}
else if (Dropdown.value == 2)
{
Cube.transform.position = new Vector3(16, 27, 285);
}
else if (Dropdown.value == 3)
{
Car.transform.position = new Vector3(16, 27, 285);
}
}
// Update is called once per frame
void Update()
{
objectMass = UI.mass;
initialVelocity = UI.initialVelocity;
theta = UI.angle;
//Horizontal and Verticle component variables
hori_U = initialVelocity * Math.Cos(theta); //horizontal initial velocity
vert_U = initialVelocity * Math.Sin(theta); //vertical initial velocity
t = (2 * initialVelocity * Math.Sin(theta)) / 9.8; //time
vert_V = -1; //vertical current velocity
hori_V = 0; //horizontal current velocity
hori_S = hori_U * t; //horizontal displacement (X)
vert_V_S = (vert_U * t) + (0.5 * g * t * t); //vertical displacement used to solve the current vertical velocity
depth_S = hori_S; //depth displacement (Z)
do
{
vert_V = Math.Sqrt((2 * g * vert_V_S) + (vert_U * vert_U));
} while (vert_V < 0);
vert_Sm = -(vert_U * vert_U) / (2 * g);
do
{
vert_V = Math.Sqrt((2 * g * vert_V_S) + (vert_U * vert_U));
} while (vert_V >= 0);
double vert_S = ((vert_U + vert_V) / 2) * t;
hori_V = Math.Sqrt((2 * g * hori_S) + (hori_U * hori_U));
float x = Convert.ToSingle(hori_S); //x co-ordinate for translation
float y = Convert.ToSingle(vert_S); //y co-ordinate for translation
float z = Convert.ToSingle(depth_S); //z co-ordinate for translation
movementUpdate = new Vector3(x, y, z);
horizontalAcceleration = (hori_V - hori_U) / t;
verticalAcceleration = (vert_V - vert_U) / t;
horizontalForce = objectMass * horizontalAcceleration;
verticalForce = objectMass * verticalAcceleration;
if (Dropdown.value == 1)
{
Ball.transform.Translate(movementUpdate);
}
else if (Dropdown.value == 2)
{
Cube.transform.position = movementUpdate;
}
else if (Dropdown.value == 3)
{
Car.transform.position = movementUpdate;
}
}
}
CalcBox:
using System;
using UnityEngine;
using UnityEngine.UI;
public class CalcBox : MonoBehaviour {
public Text calculationBoxOutput; //calc box output
public int mass = 0;
public int initialVelocity = 0;
public double hForce = 0; //horizontal force of moving object
public double vForce = 0; //vertical force of moving object
public double maximumVerticalDisplacement;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
mass = UI.mass;
initialVelocity = UI.initialVelocity;
hForce = Forces.horizontalForce;
vForce = Forces.verticalForce;
maximumVerticalDisplacement = Forces.vert_Sm;
calculationBoxOutput.text = "Mass: " + mass + Environment.NewLine + "Initial Velocity: " + initialVelocity + Environment.NewLine + "Horizontal Force: " + hForce
+ Environment.NewLine + "Vertical Force: " + vForce + Environment.NewLine + "Peak of curve from start position: " + maximumVerticalDisplacement + " units";
}
}
Answer by Asherian · Apr 14, 2018 at 10:29 PM
I've solved the problem it was my do while loop, it was causing an infinite loop of a very CPU taxing operation causing the application to crash. I've since traded the loops for two if statements containing the same code which now allows the code to run.