2 using System.Collections.Generic;
14 Dictionary<IBody, GameObject> gameObjectMap;
16 Dictionary<RigidBody, Dictionary<RigidBody, TSCollision>> collisionInfo;
34 public FP LockedTimeStep {
41 ChecksumExtractor.Init(
this);
43 gameObjectMap =
new Dictionary<IBody, GameObject>();
44 collisionInfo =
new Dictionary<RigidBody, Dictionary<RigidBody, TSCollision>>();
46 CollisionSystemPersistentSAP collisionSystem =
new CollisionSystemPersistentSAP();
49 world =
new World(collisionSystem);
50 collisionSystem.world = world;
52 world.physicsManager =
this;
55 world.Events.BodiesBeginCollide += CollisionEnter;
56 world.Events.BodiesStayCollide += CollisionStay;
57 world.Events.BodiesEndCollide += CollisionExit;
59 world.Events.TriggerBeginCollide += TriggerEnter;
60 world.Events.TriggerStayCollide += TriggerStay;
61 world.Events.TriggerEndCollide += TriggerExit;
63 world.Events.RemovedRigidBody += OnRemovedRigidBody;
72 world.Step(LockedTimeStep,
false);
82 void AddRigidBodies() {
84 List<TSCollider> sortedBodies =
new List<TSCollider>(bodies);
87 for (
int i = 0; i < sortedBodies.Count; i++) {
97 public void AddBody(ICollider iCollider) {
99 Debug.LogError(
"You have a 2D object but your Physics 2D is disabled.");
103 TSCollider tsCollider = (TSCollider) iCollider;
105 if (tsCollider._body != null) {
111 world.AddBody(tsCollider._body);
112 gameObjectMap[tsCollider._body] = tsCollider.gameObject;
114 if (tsCollider.gameObject.transform.parent != null && tsCollider.gameObject.transform.parent.GetComponentInParent<TSCollider>() != null) {
115 TSCollider parentCollider = tsCollider.gameObject.transform.parent.GetComponentInParent<TSCollider>();
120 public void RemoveBody(
IBody iBody) {
121 world.RemoveBody((RigidBody) iBody);
124 public void OnRemoveBody(
System.Action<
IBody> OnRemoveBody){
125 world.Events.RemovedRigidBody += delegate (RigidBody rb) {
130 public bool Raycast(
TSVector rayOrigin,
TSVector rayDirection, RaycastCallback raycast, out
IBody body, out
TSVector normal, out FP fraction) {
132 bool result = world.CollisionSystem.Raycast(rayOrigin, rayDirection, raycast, out rb, out normal, out fraction);
138 public TSRaycastHit Raycast(
TSRay ray, FP maxDistance, RaycastCallback callback = null) {
146 if (
PhysicsManager.
instance.Raycast(origin, direction, callback, out hitBody, out hitNormal, out hitFraction)) {
147 if (hitFraction <= maxDistance) {
152 return new TSRaycastHit(bodyComponent, colliderComponent, transformComponent, hitNormal, ray.origin, ray.direction, hitFraction);
155 direction *= maxDistance;
156 if (
PhysicsManager.
instance.Raycast(origin, direction, callback, out hitBody, out hitNormal, out hitFraction)) {
161 return new TSRaycastHit(bodyComponent, colliderComponent, transformComponent, hitNormal, ray.origin, direction, hitFraction);
167 private void OnRemovedRigidBody(RigidBody body) {
168 GameObject go = gameObjectMap[body];
171 GameObject.Destroy(go);
175 private void CollisionEnter(Contact c) {
176 CollisionDetected(c.body1, c.body2, c,
"OnSyncedCollisionEnter");
179 private void CollisionStay(Contact c) {
180 CollisionDetected(c.body1, c.body2, c,
"OnSyncedCollisionStay");
183 private void CollisionExit(RigidBody body1, RigidBody body2) {
184 CollisionDetected(body1, body2, null,
"OnSyncedCollisionExit");
187 private void TriggerEnter(Contact c) {
188 CollisionDetected(c.body1, c.body2, c,
"OnSyncedTriggerEnter");
191 private void TriggerStay(Contact c) {
192 CollisionDetected(c.body1, c.body2, c,
"OnSyncedTriggerStay");
195 private void TriggerExit(RigidBody body1, RigidBody body2) {
196 CollisionDetected(body1, body2, null,
"OnSyncedTriggerExit");
199 private void CollisionDetected(RigidBody body1, RigidBody body2, Contact c,
string callbackName) {
200 if (!gameObjectMap.ContainsKey(body1) || !gameObjectMap.ContainsKey(body2)) {
204 GameObject b1 = gameObjectMap[body1];
205 GameObject b2 = gameObjectMap[body2];
207 if (b1 == null || b2 == null) {
211 b1.SendMessage(callbackName, GetCollisionInfo(body1, body2, c), SendMessageOptions.DontRequireReceiver);
212 b2.SendMessage(callbackName, GetCollisionInfo(body2, body1, c), SendMessageOptions.DontRequireReceiver);
217 private TSCollision GetCollisionInfo(RigidBody body1, RigidBody body2, Contact c) {
218 if (!collisionInfo.ContainsKey(body1)) {
219 collisionInfo.Add(body1,
new Dictionary<RigidBody, TSCollision>());
222 Dictionary<RigidBody, TSCollision> collisionInfoBody1 = collisionInfo[body1];
226 if (collisionInfoBody1.ContainsKey(body2)) {
227 result = collisionInfoBody1[body2];
230 collisionInfoBody1.Add(body2, result);
234 result.Update(gameObjectMap[body2], c);
245 return gameObjectMap[rigidBody];
258 public IWorldClone GetWorldClone() {
259 return new WorldClone();
Manages the collision matrix of physics simulation.
Manages creation of player prefabs and lockstep execution.
Represents few information about a raycast hit.
TSVector Gravity
Property access to simulated gravity.
static void UpdateCoroutines()
Update all coroutines created.
Provides a few utilities to be used on TrueSync exposed classes.
IWorld GetWorld()
Instance of the current simulated world.
bool IsCollisionEnabled(IBody rigidBody1, IBody rigidBody2)
Check if the collision between two RigidBodies is enabled.
Represents a physical 3D rigid body.
static IPhysicsManager instance
Returns a proper implementation of IPhysicsManager.
Manages the 3D physics simulation.
Abstract collider for 3D shapes.
TSVector ScaledCenter
Returns a version of collider's center scaled by parent's transform.
GameObject GetGameObject(IBody rigidBody)
Get the GameObject related to a specific RigidBody.
static bool CollisionEnabled(int layerA, int layerB)
Returns true if the given layers can collide.
Represents a ray with origin and direction.
void Initialize()
Initializes Shape and RigidBody and sets initial values to position and orientation based on Unity's ...
Manages physics simulation.
IBody3D Body
Returns the body linked to this collider.
static TSBodyComparer bodyComparer
Instance of a TSBodyComparer.
Represents information about a contact between two 3D bodies.
bool SpeculativeContacts
Property access to speculative contacts.
Represents a common interface to 2D and 3D bodies.
void AddBody(ICollider iCollider)
Add a new RigidBody to the world.
void UpdateStep()
Goes one step further on the physics simulation.