Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

3D Trajectory prediction of a sphere with linear drag

Discussion in 'Scripting' started by trondehx, Apr 2, 2014.

  1. trondehx

    trondehx

    Joined:
    Feb 3, 2014
    Posts:
    3
    Hi folks,

    Found some code on the net for prediction of the trajectory for a sphere given velocity and gravity.
    Works great for a sphere with mass 1 and linear drag 0. Here's the position code:

    Code (csharp):
    1. Vector3 GetTrajectoryPoint(Vector3 startingPosition, Vector3 initialVelocity, float timestep, Vector3 gravity)
    2.     {
    3.         float physicsTimestep = Time.fixedDeltaTime;
    4.         Vector3 stepVelocity = initialVelocity * physicsTimestep;
    5.  
    6.         //Gravity is already in meters per second, so we need meters per second per second
    7.         Vector3 stepGravity = physicsTimestep * physicsTimestep * gravity;
    8.  
    9.         return startingPosition + (timestep * stepVelocity) + (((timestep * timestep + timestep) * stepGravity) / 2.0f);
    10.     }
    If I set the mass to about 0.5f and drag to 0.7f the sphere falls short due to the drag affect.

    Searched the net for this but can't find a good solution, any ideas on how to update the code ?

    Brgds Trond
     
    Last edited: Apr 2, 2014
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    look if this yields anything usefull for you.
     
  3. trondehx

    trondehx

    Joined:
    Feb 3, 2014
    Posts:
    3
    Thanks, complex formula stuff, but doesn't seem to make use of any linear drag coefficient.
     
  4. trondehx

    trondehx

    Joined:
    Feb 3, 2014
    Posts:
    3
    Surprised that this such an issue, and nobody has had this problem before...

    Does anyone know how unity (physx) use the drag force equation and apply the Fd each step to the velocity delta ?
    For example for a unit sphere with radius 0.5 scaled to 0.25, rigidbody.drag = 0.7 and initial velocity of [0,5,5] =approx 7.07 :

    Fd = 0.5 * p * v^2 * Cd * A , p is air density, Cd is rigidbody.drag and negative to counter velocity, A is pi*r^2 for the sphere

    Fd = 0.5 * p * 7.07^2 * -0.7 * pi*(0.25*0.5)^2

    Does anyone know the air density ? Let's say its known,

    would it be as easy as applying Fd_step to v_step ?
    (step meaning divided by fixedDeltaTime)

    Brgds Trond
     
    Last edited: Apr 3, 2014