- Home /
Angular Velocity is causing object to spin out of control
I am currently working on a Unity 3D machine learning application that teaches an airplane to fly through a set of hoops. I have the neural network programmed properly, but I'm struggling with the optimization part of it. Basically, when my airplane is facing towards the target hoop and moving closer to it, it should have a high fitness, while it should have a low fitness when it is moving away from it. My problem is that once it reaches the first hoop, it starts spinning out of control. I have a feeling it's how my angular velocity is set up, as well as how the euler angle of the airplane changes. My airplane moves straight along the x-axis, with the y-axis pointing up and down and the z-axis pointing left and right. Here is the code I have so far for reference:
private void UpdateNet()
{
if (initialized == true)
{
float[] input = new float[layers[0]];
float angle = transform.eulerAngles.x;
Vector3 deltaVector = (targetHoopPosition.position - transform.position).normalized;
float deltaVectorAngle = Mathf.Atan2(deltaVector.y, deltaVector.x);
deltaVectorAngle *= Mathf.Rad2Deg;
deltaVectorAngle -= angle;
deltaVectorAngle *= Mathf.Deg2Rad;
input[0] = deltaVectorAngle / (Mathf.PI); //Get value between -1 and 1 for input.
float[] output = manager.neuralNet.FeedForward(input);
rAirplane.velocity = transform.forward * 1f;
var angularVelocity = rAirplane.angularVelocity;
angularVelocity.y = 500f * output[0];
angularVelocity.z = 500f * output[1];
rAirplane.angularVelocity = angularVelocity;
manager.neuralNet.AddFitness(1f - Mathf.Abs(input[0]));
}
}
I am relatively new to how euler angles work as well as how they translate to angular velocity. Am I supposed to also calculate the y and z components of the euler angle too? I wasn't sure since right now the hoops are along the x-axis, they just have different heights to them and I figured only the x euler angle needed to be changed. You can also see in my code that I have two outputs from my neural network. One is to turn left and right and the other is to move up and down.
One more thing, the strange thing is that this works fine for the first hoop, because it is directly in front of me so the angle as well as the deltaVectorAngle is zero, so it doesn't move anywhere. But as soon as it gets the new coordinates of the next hoop which is in the negative y-axis, then it starts spinning out of control around the hoop it already hit, which doesn't make sense. The delta vector angle that gets returned is about -26 degrees, which is approximately correct, but the gameobject never maintains that angle or direction. If anyone is able to help point me in the right direction, I'd greatly appreciate it.