public void BeginSimulation(uint tick) { lastIndex = MomentumIndexForTick(tick); if (!lastIndex.valid) { lastIndex = TrailIndex.zero; } if (currentIndex != lastIndex) { ClearContacts(); ApplyMomentum(this[lastIndex]); } if (lastIndex.segment < momentums.Count - 1) { momentums.RemoveRange( lastIndex.segment + 1, momentums.Count - (lastIndex.segment + 1)); } int nextMomI = lastIndex.momentum + 1; while (nextMomI < momentums[lastIndex.segment].Length && momentums[lastIndex.segment][nextMomI] != null) { Momentum2D.Release(momentums[lastIndex.segment][nextMomI++]); momentums[lastIndex.segment][nextMomI++] = null; } }
void Start() { body = GetComponent <Rigidbody2D>(); momentums = new List <Momentum2D[]>(1); currentIndex = lastIndex = TrailIndex.zero; Momentum2D[] segment = new Momentum2D[segmentSize]; segment[0] = ExtractMomentum((uint)(Time.fixedTime / Time.fixedDeltaTime)); momentums.Add(segment); }
public void SendContactEvents(uint tick) { Momentum2D momentum = MomentumForTick(tick); if (CollisionEnterEvent != null && momentum.collisionsEnter != null) { foreach (Collision2D collision in momentum.collisionsEnter) { CollisionEnterEvent(tick, collision); } } if (CollisionStayEvent != null && momentum.collisionsStay != null) { foreach (Collision2D collision in momentum.collisionsStay) { CollisionStayEvent(tick, collision); } } if (CollisionExitEvent != null && momentum.collisionsExit != null) { foreach (Collision2D collision in momentum.collisionsExit) { CollisionExitEvent(tick, collision); } } if (TriggerEnterEvent != null && momentum.triggersEnter != null) { foreach (Collider2D collision in momentum.triggersEnter) { TriggerEnterEvent(tick, collision); } } if (TriggerStayEvent != null && momentum.triggersStay != null) { foreach (Collider2D collision in momentum.triggersStay) { TriggerStayEvent(tick, collision); } } if (TriggerExitEvent != null && momentum.triggersExit != null) { foreach (Collider2D collision in momentum.triggersExit) { TriggerExitEvent(tick, collision); } } }
Momentum2D ExtractMomentum(uint tick) { return(Momentum2D.GetMomentum( tick, transform.position, transform.rotation, body.velocity, body.angularVelocity, body.IsSleeping(), collisionsEnter, collisionsStay, collisionsExit, triggersEnter, triggersStay, triggersExit )); }
void ApplyMomentum(Momentum2D momentum) { transform.SetPositionAndRotation( momentum.position, momentum.rotation ); body.velocity = momentum.velocity; body.angularVelocity = momentum.angularVelocity; if (body.IsSleeping() && !momentum.sleeping) { body.WakeUp(); } else if (body.IsAwake() && momentum.sleeping) { body.Sleep(); } }
public bool GoToTime(uint tick, uint keepTick = 0) { TrailIndex i = MomentumIndexForTick(tick); bool success = GoToIndex(i); uint pastTick = Math.Max(tick - keepTick, 0); int pastSegI = 0; while (pastSegI + 1 <= i.segment && momentums[pastSegI + 1][0].tick <= pastTick) { pastSegI++; } if (pastSegI > 0) { currentIndex = new TrailIndex( currentIndex.segment - pastSegI, currentIndex.momentum ); lastIndex = new TrailIndex( lastIndex.segment - pastSegI, lastIndex.momentum ); if (!Momentum2D.IsPoolFull()) { for (int segI = 0; segI < pastSegI; segI++) { Momentum2D.Release(momentums[segI]); if (Momentum2D.IsPoolFull()) { break; } } } momentums.RemoveRange(0, pastSegI); } return(success); }