Search Unity

Math/Physics question: Formula for acceleration given distance

Discussion in 'Scripting' started by Marscaleb, Oct 23, 2014.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    I'm looking for a formula to use for some physics calculations.

    I have:
    -an initial starting velocity
    -a final velocity (zero)
    -the distance it needs to take to go from starting velocity to ending velocity

    I want to find:
    -the rate of acceleration needed to go make that change in velocity across that specified distance.

    I tried looking this up online but none of them were giving me formulas for solving what I need with what I have.
     
  2. NeverConvex

    NeverConvex

    Joined:
    Jun 26, 2013
    Posts:
    88
    Are you assuming the rate of acceleration is constant? If so,

    A = (v(T) - v0) / T = (V(T) - v0) / T = - v0 / T

    where T is the time it takes for v0 (initial velocity) to reduce to v(T) (= 0, final velocity), and A is the constant rate of acceleration (which is negative if v0 > 0, hence is really deceleration).

    If the rate of acceleration is constant, then the position of the object is given by a quadratic, like

    p(t) = p0 + v0 * t + 0.5 A t^2

    You said you know the distance, D, which must equal p(T) - p0, so

    D = v0 * T + 0.5 * A T^2

    which gives

    T = (-v0 + sqrt(v0^2 + 2 * A * D) ) / A

    substituting into the first equation and simplifying yields

    v0^2 + 2 * A * D = 0 =>

    A = - 0.5 v0^2 / D
     
    Last edited: Oct 24, 2014
  3. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Kinematics! if we know 3 of the 5 we can find the other two.
    • Initial Velocity (known)
    • Final Velocity (known)
    • Distance (known)
    • Acceleration (??)
    • Time (??)
    Since we know 3 of the 5, we can solve for the other two. There are four kinematic equations on this page:
    http://www.physicsclassroom.com/class/1DKin/Lesson-6/Kinematic-Equations

    Find the equation that takes your 3 known variables and has acceleration as the only unknown variable.

    NOTE: This is for one dimension! If you are in multi-dimensions, then you have to solve these equations for each dimension separately.
     
  4. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    @Garth Smith
    Those equations seem to be exactly what I needed!
    Except when I actually plugged them into my code, the acceleration it produced was WAY underpowered; it felt like something between 30 -100 times too low.

    In the start function I have:
    acceleration = Mathf.Abs((maxSpeed * -1) / decelDist / 2);

    And in my update function I have:
    Code (CSharp):
    1. float distX = target.x - transform.position.x;
    2.            
    3.             if (distX > 0) {
    4.                
    5.                 xVel += acceleration * Time.deltaTime;
    6.                 if (xVel >= maxSpeed) {
    7.                     xVel = maxSpeed;
    8.                     bFullSpeed = true;
    9.                     Debug.Log("Full speed engaged!");
    10.                 }
    11.             }
    12.             else {
    13.                
    14.                 xVel -= acceleration * Time.deltaTime;
    15.                 if (xVel <= maxSpeed * -1) {
    16.                     xVel = maxSpeed * -1;
    17.                     bFullSpeed = true;
    18.                     Debug.Log("Full speed engaged!");
    19.                 }
    20.             }
    The design is that the object is supposed to proceed at full speed until it it called to turn the other direction. Then it executes this code each frame, causing it to accelerate in the opposite direction. (Once it hits full speed I run different code.) The distance I give it (decelDist) is the distance I want it to travel before it reaches its apex and starts moving in the opposite direction.
    It functions correctly; it does move a specific distance before reaching that apex, and I can adjust that distance. It's just that distance is a good 50 times or more what I thought I was setting it to.
     
  5. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    So I'm getting this equation for acceleration. Notice that velocities are getting squared.

    a = (Vf^2 - Vi^2) / 2 / d
    acceleration = (Final Squared - Initial Squared) / (2*distance)
     
  6. NeverConvex

    NeverConvex

    Joined:
    Jun 26, 2013
    Posts:
    88
    Took a simpler route (than I used the first time, which must've been riddled with errors) to solving for A and got the same thing as Garth. That's certainly the appropriate formula.
     
  7. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,037
    D'oh! I totally dropped a square, you're right!

    Okay it's working now, thanks a lot!
     
    GarthSmith likes this.