# Flight mechanics 2D, variables vs forces

Hi!

I am working on 2D flight sim, the only rotation that is important is the pitching up and down for the airplane. No wings are needed for this.
I’m using 4 variables: Lift, Drag, Gravity, Force (which is the thrust).

Let me show you 2 of my scripts.

number 1, forces based.

var liftValue 			= 0.0;
var liftCalc				= 0.0;

var dragValue 		= 0.0;
var dragCalc 			= 0.0; //Drag calculations

var maxSpeed			= 0.0;

var throttle			= 0.0;
var gravityValue		= 0.0;
var rotateSpeed  	= 0.0;
var objAngle			= 0.0;
var xForces 			= 0.0; //sum of all x forces
var yForces 			= 0.0; //sum of all y forces

function Update(){

//For increasing/decreasing speed
if(Input.GetButtonDown("ThrottleUp"))
throttle++;

if(Input.GetButtonDown("ThrottleDown"))
throttle--;

//For pitching up and down the airplane
var rotationZ : float = Input.GetAxis ("Pitch") * rotateSpeed;
transform.RotateAround(Vector3.forward,rotationZ * Time.deltaTime);

}

function FixedUpdate(){

objAngle = transform.localEulerAngles.z;
//This is getting the attack angle.
var vel  = rigidbody.velocity.magnitude;
//This is receiving the airplane speed.
dragCalc = vel*dragValue/10;
//This is calculating the drag based on the speed.
liftCalc	= vel*liftValue/10;
//This is calculating the lift based on the speed.

//I've given these calculations my own kind of values.

//You can ignore this, this is simply for the airplane
//to move straight without any effect at start
if (objAngle == 0)

//Pitching up.
if (objAngle>0&&objAngle<=90)
{
xForces  = Mathf.Sin(objAngle*Mathf.PI/180)*(throttle*5-liftCalc-dragCalc);
yForces  = Mathf.Cos(objAngle*Mathf.PI/180)*(liftCalc+throttle-dragCalc)-gravityValue;
}

//Pitching down.
if (objAngle>=270&&objAngle<360)
{
xForces  = Mathf.Cos(objAngle*Mathf.PI/180)*(throttle*5+liftCalc+dragCalc);
yForces  = Mathf.Sin(objAngle*Mathf.PI/180)*(liftCalc+dragCalc-throttle)-gravityValue;
}
if(vel<maxSpeed)
constantForce.relativeForce = Vector3(xForces,yForces,0);
//I'll note that I've tried to put in different use of rigidbody
else
vel--;
print(vel);
//To check up and critisize my velocity.

}

My airplane never stops, the forces are always adding, how can I limit them?
Or is there another way of putting them?

and my second script, variables format, basically was calculated by putting the velocity in the translate command:
F = ma
a = v/t
v = F*t/m

(Basically the same script except this)

function FixedUpdate(){

objAngle = transform.localEulerAngles.z; //This is getting the attack angle.

if (objAngle == 0)
transform.Translate(0.5, 0 , 0);

if (objAngle>0&&objAngle<=90)
{
xForces  = Mathf.Cos(objAngle*Mathf.PI/180)*(velSpeed-liftValue-dragValue);
yForces  = Mathf.Sin(objAngle*Mathf.PI/180)*(liftValue+velSpeed-dragValue)-gravityValue;
}
if (objAngle>=270&&objAngle<360)
{
xForces  = Mathf.Cos(objAngle*Mathf.PI/180)*(velSpeed+liftValue+dragValue);
yForces  = Mathf.Sin(objAngle*Mathf.PI/180)*(liftValue+dragValue-velSpeed)-gravityValue;
}

transform.Translate(xForces*Time.deltaTime,yForces*Time.deltaTime,0 , Space.World);
}

One of the problems with adding force is that you continuously add the force. This means that you’re adding the force leftover from the last movement to this force, meaning there’s even more force on the object. This is your “moving faster and faster” problem.

In my opinion, the best way to have a consistent speed is to use translates/transformations. Though there are probably other ways of doing it, this is the easiest way to do it.