Last active
May 7, 2018 09:32
-
-
Save tsubaki/1a5321cfb3a3d76637e5252aa2d032a6 to your computer and use it in GitHub Desktop.
This file contains 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 UnityEngine.Timeline; | |
public class PhysicsSimComponent : MonoBehaviour, ITimeControl | |
{ | |
private Vector3[] positions, velocitys, angularVelocitys; | |
private Quaternion[] rotations; | |
private Rigidbody[] rigidbodys; | |
private double preTime = 0; | |
public void OnControlTimeStart() | |
{ | |
Physics.autoSimulation = false; | |
rigidbodys = FindObjectsOfType<Rigidbody>(); | |
positions = new Vector3[rigidbodys.Length]; | |
velocitys = new Vector3[rigidbodys.Length]; | |
angularVelocitys = new Vector3[rigidbodys.Length]; | |
rotations = new Quaternion[rigidbodys.Length]; | |
for (int i = 0; i < rigidbodys.Length; i++) | |
{ | |
positions[i] = rigidbodys[i].position; | |
velocitys[i] = rigidbodys[i].velocity; | |
angularVelocitys[i] = rigidbodys[i].angularVelocity; | |
rotations[i] = rigidbodys[i].rotation; | |
} | |
} | |
public void OnControlTimeStop() | |
{ | |
Physics.autoSimulation = true; | |
if (!Application.isPlaying) | |
{ | |
ResetPhysicsObjects(); | |
} | |
rigidbodys = null; | |
positions = null; | |
velocitys = null; | |
angularVelocitys = null; | |
} | |
public void SetTime(double time) | |
{ | |
var deltaTime = time - preTime; | |
preTime = time; | |
if (deltaTime == 0) | |
return; | |
if (deltaTime > 0) | |
{ | |
while( deltaTime > Time.fixedDeltaTime) | |
{ | |
Physics.Simulate(Time.fixedDeltaTime); | |
deltaTime -= Time.fixedDeltaTime; | |
} | |
} | |
else | |
{ | |
ResetPhysicsObjects(); | |
var diffTime = time; | |
while (diffTime > Time.fixedDeltaTime) | |
{ | |
Physics.Simulate(Time.fixedDeltaTime); | |
diffTime -= Time.fixedDeltaTime; | |
} | |
} | |
} | |
void ResetPhysicsObjects() | |
{ | |
for (int i = 0; i < rigidbodys.Length; i++) | |
{ | |
if (rigidbodys[i] == null) | |
continue; | |
rigidbodys[i].transform.position = positions[i]; | |
rigidbodys[i].transform.rotation = rotations[i]; | |
rigidbodys[i].velocity = velocitys[i]; | |
rigidbodys[i].angularVelocity = angularVelocitys[i]; | |
} | |
} | |
} |
Author
tsubaki
commented
May 5, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment