1
\$\begingroup\$

I want to make a simple aircraft controller, what looks like little realistic in unity. I watch some video from airplane physics. and make a simple script in unity, but if I start, my plane cant move or if I change drag to zero, it cant lift. I tried to use real data and get it from wiki(F22 Raptor). To my game object, I gave the rigidbody component mass = 19670 kg. Engine thrust = 2 * 116000.0f Newton.

    private void calculateEnginePower()
    {
        EnginePower = engineThrust * ThrottleInput;
    }

    private void calculateForces()
    {
        angleOfAttack = Vector3.Angle(Vector3.forward, rb.velocity);
        angleOfAttack = Mathf.Clamp(angleOfAttack, 0, 90);

        coefficient = Mathf.Pow(1225.04f * rb.velocity.magnitude, 2) - 1; //M^2-2 where: M is mach.         

        if (coefficient > 0.0f)
            coefficientLift = (4 * angleOfAttack) / Mathf.Sqrt(coefficient);
        lift = 1.2754f * 0.5f * Mathf.Pow(rb.velocity.magnitude, 2) * coefficientLift * 78.04f; // densy1.2754 kg/m3, speed m/s , (F22)Wing area: 840 ft² (78.04 m²)

        coefficientDrag = 0.021f;
        rb.drag = coefficientDrag * 0.5f * Mathf.Pow(rb.velocity.magnitude,2) * 1.2754f * 78.04f;

        rb.AddForce(transform.up * lift);
        rb.AddForce(transform.forward * EnginePower);
    }

roll, pitch... inputs from keyboard and mouse, EnginePower, RollInput... are simple properties and I call the Move function in a controller inside void Update.


Move(float roll, float pitch, float yaw, float throttle)
{ 
// transfer input parameters into properties.s 
RollInput = roll; 
PitchInput = pitch; 
YawInput = yaw; 
ThrottleInput = throttle; 
AirBrakes = false; 
calculateEnginePower();
calculateForces(); 
calculateRotation(); 
}
used these formulas:

for Lift force: Lift formula for Lift coefficient: Cl formula for Drag: Drag formula and for Drag coefficient: I used data from wiki too (0.021f).

\$\endgroup\$
1
\$\begingroup\$

Introduction

Ah no, I'm afraid that this won't work. You decided to get to an intriguing yet difficult realm. Calculating accurately \$C_L\$ and \$C_D\$ is not easy (there wouldn't be aerodynamic tunnels if it was).

Also I would suggest to try modeling much older aircraft (of the Cold war era like F-86 or Mig-17). Getting aerodynamic information for fighters like F-22 is virtually impossible (they are classified). I've heard that for some few of the older jets some aerodynamic figures have been declassified but I can't find any information right now. You can search in Aviation SE or ask if it's not already asked.

Having said that, let's look into your problems:

Why can't I accelerate

You are setting the value you calculated for the aerodynamic drag to rb.drag. That won't work. Unfortunately the documentation isn't clear as in what drag represents. As I get it is aerodynamic drag which is dimensionless. You set newtons there so I guess drag will be so high that your aircraft is trying to swim not fly. So you have 2 options:

  1. Set rb.drag to zero and call

    float drag = coefficientDrag * 0.5f * Mathf.Pow(rb.velocity.magnitude,2) * 1.2754f * 78.04f;
    rb.AddForce(transform.forward * (EnginePower - drag));
    
  2. or simply do rb.drag = coefficientDrag; and then let unity handle it.

The first gives you more control while the second relies on unity's computations that I am unfamiliar with.

Why can't I takeoff

Well that's simpler: you use a formulae that applies to supersonic speeds

$$ C_{L} = \frac{4α}{\sqrt{M^2 - 1}} $$

Clearly if \$M < 1\$ then the radicand becomes negative and the root can't be calculated. Your code ignores it. So coefficientLift will be 0 (I guess, you don't show the initialization). That means that you won't get any lift until you get supersonic. Ouch. You need a big runway for that.

One thing you can do, is to create a text file (in a format of your preference) that will represent a "typical" \$C_L - AoA\$ matching. One example you can use is from Wikipedia

AoA vs CL

You can have something like

#AoA  CL
-5    0
-3    0.25
-0.5  0.5
2     0.75

and so on, and then apply linear interpolation to get the values in between.

Other remarks

You use rb.velocity.magnitude as both velocity and Mach number. You need to make conversions (from Wikipedia): $$ M = \frac{u}{c} $$

where:
M is the Mach number
u is velocity of the moving aircraft and
c is the speed of sound at the given altitude

Hope the above helps. Good luck with your game.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.