Search Unity

Question Calculate yaw torque of a airplane

Discussion in 'Physics' started by MatheusMarkies, Jan 20, 2021.

  1. MatheusMarkies

    MatheusMarkies

    Joined:
    Apr 16, 2017
    Posts:
    67
    Hi!
    I'm trying to calculate the yaw torque for an airplane in the unit.
    I'm using the z-axis sine to calculate the torque, but I'm having problems.
    The airplane spins normally, but when I turn 180 degrees he breaks.
    Torque:
    Code (CSharp):
    1.  void Aerodynamic(float velocity, Vector3 LiftDir, Vector3 VectorDir)
    2.     {
    3.         float VectorProduct = Vector3.Dot(Vector3.forward, rb.velocity);
    4.         //VectorProduct *= VectorProduct;
    5.        
    6.         if (Input.GetKey(KeyCode.A))
    7.         {
    8.             rolling += 0.1f;
    9.         }
    10.         else
    11.         if (Input.GetKey(KeyCode.D))
    12.         {
    13.             rolling -= 0.1f;
    14.         }
    15.         else
    16.             rolling = 0;
    17.  
    18.         if (Input.GetKey(KeyCode.W))
    19.             pitch += 0.1f;
    20.         else
    21.         if (Input.GetKey(KeyCode.S))
    22.             pitch -= 0.1f;
    23.         else
    24.             pitch = 0;
    25.  
    26.         Vector3 torque = Vector3.zero;
    27.  
    28.         int ads = 0;
    29.         if (Mathf.Cos(transform.rotation.z) < 0)
    30.             ads = -1;
    31.         else
    32.             ads = 1;
    33.  
    34.         torque += (-1 * ads * Mathf.Sin(transform.rotation.z * 2)) * 2.5f * transform.up;
    35.  
    36.         torque += pitch * transform.right;
    37.         torque += rolling * transform.forward;
    38.         //}
    39.         //else
    40.         //{
    41.         //   torque += -pitch * transform.right;
    42.         //   torque += -rolling * transform.forward;
    43.         //}
    44.  
    45.         //torque += -1 * (LiftForce(LiftCoefficient, this.airDensity, velocity) * transform.right * (1-Mathf.Sin(Vector3.Dot(Vector3.forward, VectorDir) * 2))) * FlareCoefficient;
    46.  
    47.         //rb.velocity = Vector3.Lerp(rb.velocity, transform.forward * (rb.velocity.magnitude / Time.deltaTime), VectorProduct * .063f * Time.deltaTime); ;
    48.         //rb.rotation = Quaternion.Slerp(rb.rotation, Quaternion.LookRotation(rb.velocity, transform.forward), .263f * Mathf.Abs(Mathf.Sin(transform.rotation.z)) * Time.deltaTime);
    49.         rb.AddTorque(torque * rb.velocity.magnitude);
    50.     }
    51.  
    52. }
    Airplane controller
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FlyController : MonoBehaviour
    6. {
    7.  
    8.     public float LiftCoefficient = 0.009f;
    9.     public float DragCoefficient = 0.001f;
    10.     public float BrakeCoefficient = 0.09f;
    11.     public float motorEnergy = 350f;
    12.     //public float MaxSpeed = 140;
    13.     Rigidbody rb;
    14.     float Energy = 0;
    15.     public float airDensity = 2f;
    16.  
    17.     public float FlareCoefficient = 0.01f;
    18.  
    19.     //public float Acceleration = 20f;
    20.  
    21.     float breakAngle;
    22.     float angularDrag;
    23.     float drag;
    24.     float rolling;
    25.     float pitch;
    26.     float oldDrag;
    27.  
    28.     void Start()
    29.     {
    30.         rb = GetComponent<Rigidbody>();
    31.         angularDrag = rb.angularDrag;
    32.         drag = rb.drag;
    33.     }
    34.     void Update()
    35.     {
    36.  
    37.         //if (Input.GetKey(KeyCode.A))
    38.         //{
    39.         //    transform.Rotate(0, 0, 0.5f);
    40.         //}
    41.         //if (Input.GetKey(KeyCode.D))
    42.         //{
    43.         //    transform.Rotate(0, 0, -0.5f);
    44.         //}
    45.         //if (Input.GetKey(KeyCode.W))
    46.         //    transform.Rotate(0.5f, 0, 0);
    47.  
    48.         //if (Input.GetKey(KeyCode.S))
    49.         //    transform.Rotate(-0.5f, 0, 0);
    50.  
    51.         Vector3 LiftDir = transform.rotation * Vector3.up;
    52.         Vector3 VectorDir = transform.rotation * Vector3.forward;
    53.         Vector3 DragDir = transform.rotation * Vector3.back;
    54.  
    55.         float velocity = Mathf.Abs( rb.velocity.z );
    56.         //if(velocity < MaxSpeed)
    57.         if (Input.GetKey(KeyCode.Space))
    58.         Energy = (Energy + motorEnergy) - (rb.drag * 2/rb.mass) / Time.deltaTime;
    59.         else if (Energy > 0)
    60.         Energy -= rb.velocity.magnitude * Time.deltaTime * LiftForce(LiftCoefficient, this.airDensity, velocity);
    61.         if (Energy < 0)
    62.             Energy = 0;
    63.  
    64.         float Velocity = Mathf.Sqrt(Energy / 2);
    65.         rb.velocity = Velocity * VectorDir;
    66.  
    67.         rb.AddForce(LiftForce(LiftCoefficient, this.airDensity, velocity) * LiftDir * Mathf.Sin(Vector3.Dot(Vector3.forward, VectorDir)));
    68.         //Debug.Log("Cos" + rb.velocity);
    69.         if (Input.GetKey(KeyCode.LeftShift))
    70.         {
    71.             if (breakAngle < 90)
    72.                 breakAngle = breakAngle + Mathf.Lerp(0.6f, 0, breakAngle / 90);
    73.         }
    74.         else
    75.             if (breakAngle > 0)
    76.             breakAngle = breakAngle + Mathf.Lerp(-2f, 0, breakAngle / 90);
    77.         else
    78.             breakAngle = 0;
    79.  
    80.         rb.drag = (Mathf.SmoothStep(oldDrag,(DragForce(DragCoefficient, this.airDensity, velocity) * DragDir).magnitude,Time.deltaTime));
    81.         //rb.AddForce(50 * transform.position.y * rb.mass * Vector3.down);
    82.         rb.AddForce(10 * rb.mass * Vector3.down);
    83.         rb.angularDrag = angularDrag * rb.velocity.magnitude * VectorDir.magnitude * this.airDensity;
    84.  
    85.         //Debug.Log((LiftForce(LiftCoefficient, this.airDensity, velocity) * LiftDir * Mathf.Cos(transform.rotation.z) * Time.deltaTime).magnitude);
    86.  
    87.         Aerodynamic(velocity,LiftDir,VectorDir);
    88.     }
    89.     void LateUpdate()
    90.     {
    91.         oldDrag = rb.drag;
    92.     }
    93.  
    94.     float LiftForce(float liftC, float AirDensity, float Speed, float WingSurface = 1)
    95.     {
    96.         float lift = liftC * AirDensity * Speed * Speed * WingSurface / 2;
    97.         return lift;
    98.     }
    99.     float DragForce(float dragC, float AirDensity, float Speed, float WingSurface = 1)
    100.     {
    101.         float drag = this.drag + (dragC * AirDensity * Speed * Speed * WingSurface / 2);
    102.         return drag + BrakeForce(BrakeCoefficient, this.airDensity, Speed) * Mathf.Sin(breakAngle);
    103.     }
    104.     float BrakeForce(float breakC, float AirDensity, float Speed, float WingSurface = 1)
    105.     {
    106.         float Break = breakC * AirDensity * Speed * Speed * WingSurface / 2;
    107.         return Break;
    108.     }
    109.     void Aerodynamic(float velocity, Vector3 LiftDir, Vector3 VectorDir)
    110.     {
    111.         float VectorProduct = Vector3.Dot(Vector3.forward, rb.velocity);
    112.         //VectorProduct *= VectorProduct;
    113.        
    114.         if (Input.GetKey(KeyCode.A))
    115.         {
    116.             rolling += 0.1f;
    117.         }
    118.         else
    119.         if (Input.GetKey(KeyCode.D))
    120.         {
    121.             rolling -= 0.1f;
    122.         }
    123.         else
    124.             rolling = 0;
    125.  
    126.         if (Input.GetKey(KeyCode.W))
    127.             pitch += 0.1f;
    128.         else
    129.         if (Input.GetKey(KeyCode.S))
    130.             pitch -= 0.1f;
    131.         else
    132.             pitch = 0;
    133.  
    134.         Vector3 torque = Vector3.zero;
    135.  
    136.         int ads = 0;
    137.         if (Mathf.Cos(transform.rotation.z) < 0)
    138.             ads = -1;
    139.         else
    140.             ads = 1;
    141.  
    142.         torque += (-1 * ads * Mathf.Sin(transform.rotation.z * 2)) * 2.5f * transform.up;
    143.  
    144.         torque += pitch * transform.right;
    145.         torque += rolling * transform.forward;
    146.         //}
    147.         //else
    148.         //{
    149.         //   torque += -pitch * transform.right;
    150.         //   torque += -rolling * transform.forward;
    151.         //}
    152.  
    153.         //torque += -1 * (LiftForce(LiftCoefficient, this.airDensity, velocity) * transform.right * (1-Mathf.Sin(Vector3.Dot(Vector3.forward, VectorDir) * 2))) * FlareCoefficient;
    154.  
    155.         //rb.velocity = Vector3.Lerp(rb.velocity, transform.forward * (rb.velocity.magnitude / Time.deltaTime), VectorProduct * .063f * Time.deltaTime); ;
    156.         //rb.rotation = Quaternion.Slerp(rb.rotation, Quaternion.LookRotation(rb.velocity, transform.forward), .263f * Mathf.Abs(Mathf.Sin(transform.rotation.z)) * Time.deltaTime);
    157.         rb.AddTorque(torque * rb.velocity.magnitude);
    158.     }
    159.  
    160. }
    161.  
    F2.png

     

    Attached Files: