Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Is this the proper way to add air resistance to a ball?

Discussion in 'Physics' started by FreeGameDev, May 30, 2020.

  1. FreeGameDev

    FreeGameDev

    Joined:
    Dec 1, 2015
    Posts:
    67
    Can anyone tell me if this is the proper way to add air drag to my rigidbody(rb)?
    Here is my code:
    Code (CSharp):
    1.  
    2.     private void FixedUpdate()
    3.     {
    4.         //Cd = 0.24f                        // drag coefficient of a golf ball
    5.         //A = Pi*radius*radius              // crossSectionalArea of sphere in [m^2]
    6.         //ad.p = 1.225f                     // air density in [kg/m^3]
    7.         float A = b.crossSectionalArea;    
    8.         Vector3 v;
    9.         //_P.AirResistance = 0.5f * Cd * A * v * v * ρ
    10.         v.x = -_P.AirResistance(b.Cd, A, rb.velocity.x, ad.p);
    11.         v.y = -_P.AirResistance(b.Cd, A, rb.velocity.y, ad.p);
    12.         v.z = -_P.AirResistance(b.Cd, A, rb.velocity.z, ad.p);
    13.         rb.AddForce(v.x, v.y, v.z);
    14.     }
    15.  
    I did an experiment to add gravity.
    Code (CSharp):
    1. rb.AddForce(Physics.gravity * rb.mass);
    This worked exactly as unity's gravity.
    The air drag seams to be a little on the strong side?
    Also: Is there a air drag simulator that I can test my results against?

    Thanks
     
  2. FreeGameDev

    FreeGameDev

    Joined:
    Dec 1, 2015
    Posts:
    67
    Figured it out one issue: The force needs to work in the opposite direction to the velocity.
    I corrected this by Mathf.Abs(velocity.x) on of the velocities being squared. Are there still issues?
     
    Last edited: May 30, 2020