Last active
August 29, 2015 14:15
-
-
Save nicloay/861fbaad7392f0f2bbff to your computer and use it in GitHub Desktop.
Gravity test calculation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| using UnityTest; | |
| /// <summary> | |
| /// Gravity test. | |
| /// | |
| /// see formulas here http://error454.com/2013/10/23/platformer-physics-101-and-the-3-fundamental-equations-of-platformers/ | |
| /// | |
| /// d = v*t + 0.5f * a * t * t //d -distance, v - initial velocity, a - aceleration, t - time | |
| /// | |
| /// for this test v = 0 (initial speed = 0) | |
| /// | |
| /// d = 0.5f * a * t * t | |
| /// 2d = a * t * t | |
| /// t * t = (2 * d) / a | |
| /// t = sqrt ((2*d) /a ) | |
| /// | |
| /// regarding this link http://www.iforce2d.net/b2dtut/projected-trajectory | |
| /// we have to avoid use timing, and instead of it use frame numbers. | |
| /// also there you can find formula used by box2d | |
| /// p(n) = p0 +n*v + (n2 +n)*a*0.5 | |
| /// in our case | |
| /// p(n) = H height | |
| /// p(n) = 0 ground point where our object is falling down | |
| /// v = 0 start velocity | |
| /// finaly we have folowing formula | |
| /// n*n + n + 2*H/a = 0 | |
| /// </summary> | |
| public class GravityTest : MonoBehaviour { | |
| public float StartHeight = 1.0f; | |
| public float FloatEpsilon = 0.05f; | |
| [HideInInspector] | |
| public float FallDownTime; | |
| public int FallDownFrameNumber; | |
| float startTime; | |
| int StartFrame; | |
| FloatComparer timeComparer; | |
| IntComparer fixedUpdateComparer; | |
| void Awake(){ | |
| transform.position = new Vector3(0,StartHeight, 0); | |
| timeComparer = AssertionComponent.Create<FloatComparer>(CheckMethod.OnCollisionEnter2D, gameObject, "GravityTest.FallDownTime", 0.0f); | |
| timeComparer.floatingPointError = FloatEpsilon; | |
| timeComparer.compareTypes = FloatComparer.CompareTypes.Equal; | |
| fixedUpdateComparer = AssertionComponent.Create<IntComparer>(CheckMethod.OnCollisionEnter2D, gameObject, "GravityTest.FallDownFrameNumber", 0); | |
| fixedUpdateComparer.compareType = IntComparer.CompareType.Equal; | |
| } | |
| void Start () { | |
| startTime = Time.timeSinceLevelLoad; | |
| float gravityPerFrame = Physics2D.gravity.y * Time.fixedDeltaTime * Time.fixedDeltaTime; | |
| float c = 2 * transform.position.y / gravityPerFrame; | |
| float expectedFixedUpdateNumber = (-1.0f + Mathf.Sqrt(1 - 4 * c))/2.0f; | |
| timeComparer.ConstValue = expectedFixedUpdateNumber * Time.fixedDeltaTime; | |
| fixedUpdateComparer.ConstValue = (int)expectedFixedUpdateNumber; | |
| } | |
| void OnCollisionEnter2D(Collision2D coll) { | |
| FallDownTime = Time.timeSinceLevelLoad - startTime; | |
| FallDownFrameNumber = (int)(FallDownTime / Time.fixedDeltaTime); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment