Author Topic: SpringDampen Performance optimization  (Read 6769 times)

mixd

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
SpringDampen Performance optimization
« on: July 05, 2013, 11:15:25 PM »
Noticed this was starting to eat up some CPU cycles and the current implementation seemed odd. This is roughly 4x faster than the current method (rough benchmark).

Original Way:
  1. static public Vector3 SpringDampen (ref Vector3 velocity, float strength, float deltaTime)
  2.         {
  3.                 // Dampening factor applied each millisecond
  4.                 if (deltaTime > 1f) deltaTime = 1f;
  5.                 float dampeningFactor = 1f - strength * 0.001f;
  6.                 int ms = Mathf.RoundToInt(deltaTime * 1000f);
  7.                 Vector3 offset = Vector3.zero;
  8.  
  9.                 // Apply the offset for each millisecond
  10.                 for (int i = 0; i < ms; ++i)
  11.                 {
  12.                         // Mimic 60 FPS the editor runs at
  13.                         offset += velocity * 0.06f;
  14.                         velocity *= dampeningFactor;
  15.                 }
  16.                 return offset;
  17.         }


Faster Way (Use raw math instead of a loop)
  1. static public Vector3 SpringDampen (ref Vector3 velocity, float strength, float deltaTime)
  2.         {
  3.                 // Dampening factor applied each millisecond
  4.                 if (deltaTime > 1f) deltaTime = 1f;
  5.                 float dampeningFactor = 1f - strength * 0.001f;
  6.                 int ms = Mathf.RoundToInt(deltaTime * 1000f);
  7.  
  8.                 float totalDampening = Mathf.Pow(dampeningFactor, ms);
  9.                 Vector3 vTotal = velocity * ((totalDampening - 1f) / Mathf.Log(dampeningFactor));
  10.                 velocity = velocity * totalDampening;
  11.                
  12.                 return vTotal * .06f;
  13.         }
« Last Edit: July 05, 2013, 11:29:55 PM by mixd »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: SpringDampen Performance optimization
« Reply #1 on: July 06, 2013, 10:19:15 AM »
As long as the implementation behaves the same and remains framerate-independent, I'm fine with it.

sintua

  • Jr. Member
  • **
  • Thank You
  • -Given: 7
  • -Receive: 0
  • Posts: 63
    • View Profile
Re: SpringDampen Performance optimization
« Reply #2 on: July 06, 2013, 04:05:14 PM »
Is this something you'd implement into the next build, or something we'd have to change ourselves directly?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: SpringDampen Performance optimization
« Reply #3 on: July 06, 2013, 10:05:17 PM »
I've added it locally, and will see if it affects anything adversely.

mixd

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 22
    • View Profile
Re: SpringDampen Performance optimization
« Reply #4 on: July 10, 2013, 02:01:50 PM »
Great -- should be the same as the original.

All I did was take the integral of that loop.

(Probably the first thing I've ever used calculus for in real life)

Also, the implementation is exactly the same for the Vector2 version, but with Vector2 instead of Vector3 I just didn't post it here.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: SpringDampen Performance optimization
« Reply #5 on: July 10, 2013, 08:41:49 PM »
SpringLerp can also probably benefit from this approach. Integral math = outside my scope of expertise though.