2 using System.Collections.Generic;
17 private Physics2D.World world;
19 Dictionary<IBody, GameObject> gameObjectMap;
21 Dictionary<Physics2D.Body, Dictionary<Physics2D.Body,
TSCollision2D>> collisionInfo;
39 public FP LockedTimeStep {
46 ChecksumExtractor.Init(
this);
51 Physics2D.ContactManager.physicsManager =
this;
53 world.BodyRemoved += OnRemovedRigidBody;
54 world.ContactManager.BeginContact += CollisionEnter;
55 world.ContactManager.StayContact += CollisionStay;
56 world.ContactManager.EndContact += CollisionExit;
58 gameObjectMap =
new Dictionary<IBody, GameObject>();
59 collisionInfo =
new Dictionary<Physics2D.Body, Dictionary<Physics2D.Body,
TSCollision2D>>();
69 world.Step(LockedTimeStep);
72 public IWorld GetWorld() {
76 void AddRigidBodies() {
78 List<TSCollider2D> sortedBodies =
new List<TSCollider2D>(bodies);
81 for (
int i = 0; i < sortedBodies.Count; i++) {
82 AddBody(sortedBodies[i]);
86 public void AddBody(ICollider iCollider) {
88 Debug.LogError(
"You have a 3D object but your Physics 3D is disabled.");
92 TSCollider2D tsCollider = (TSCollider2D) iCollider;
94 if (tsCollider._body != null) {
100 gameObjectMap[tsCollider._body] = tsCollider.gameObject;
102 if (tsCollider.gameObject.transform.parent != null && tsCollider.gameObject.transform.parent.GetComponentInParent<TSCollider2D>() != null) {
103 TSCollider2D parentCollider = tsCollider.gameObject.transform.parent.GetComponentInParent<TSCollider2D>();
104 Physics2D.
Body childBody = tsCollider._body;
109 world.ProcessAddedBodies();
112 internal string GetChecksum(
bool plain) {
113 return ChecksumExtractor.GetEncodedChecksum();
116 public void RemoveBody(
IBody iBody) {
117 world.RemoveBody((
TrueSync.Physics2D.Body) iBody);
118 world.ProcessRemovedBodies();
121 public void OnRemoveBody(Action<IBody> OnRemoveBody) {
122 world.BodyRemoved += delegate (
TrueSync.Physics2D.Body body) {
127 private void OnRemovedRigidBody(
TrueSync.Physics2D.Body body) {
128 GameObject go = gameObjectMap[body];
131 GameObject.Destroy(go);
135 private bool CollisionEnter(
TrueSync.Physics2D.Contact contact) {
136 if (contact.FixtureA.IsSensor || contact.FixtureB.IsSensor) {
137 TriggerEnter(contact);
142 CollisionDetected(contact.FixtureA.Body, contact.FixtureB.Body, contact,
"OnSyncedCollisionEnter");
146 private void CollisionStay(
TrueSync.Physics2D.Contact contact) {
147 if (contact.FixtureA.IsSensor || contact.FixtureB.IsSensor) {
148 TriggerStay(contact);
152 CollisionDetected(contact.FixtureA.Body, contact.FixtureB.Body, contact,
"OnSyncedCollisionStay");
155 private void CollisionExit(
TrueSync.Physics2D.Contact contact) {
156 if (contact.FixtureA.IsSensor || contact.FixtureB.IsSensor) {
157 TriggerExit(contact);
161 CollisionDetected(contact.FixtureA.Body, contact.FixtureB.Body, null,
"OnSyncedCollisionExit");
164 private void TriggerEnter(
TrueSync.Physics2D.Contact contact) {
165 CollisionDetected(contact.FixtureA.Body, contact.FixtureB.Body, contact,
"OnSyncedTriggerEnter");
168 private void TriggerStay(
TrueSync.Physics2D.Contact contact) {
169 CollisionDetected(contact.FixtureA.Body, contact.FixtureB.Body, contact,
"OnSyncedTriggerStay");
172 private void TriggerExit(
TrueSync.Physics2D.Contact contact) {
173 CollisionDetected(contact.FixtureA.Body, contact.FixtureB.Body, null,
"OnSyncedTriggerExit");
176 private void CollisionDetected(Physics2D.Body body1, Physics2D.Body body2,
TrueSync.Physics2D.Contact contact,
string callbackName) {
177 if (!gameObjectMap.ContainsKey(body1) || !gameObjectMap.ContainsKey(body2)) {
181 GameObject b1 = gameObjectMap[body1];
182 GameObject b2 = gameObjectMap[body2];
184 if (b1 == null || b2 == null) {
188 b1.SendMessage(callbackName, GetCollisionInfo(body1, body2, contact), SendMessageOptions.DontRequireReceiver);
189 b2.SendMessage(callbackName, GetCollisionInfo(body2, body1, contact), SendMessageOptions.DontRequireReceiver);
194 private TSCollision2D GetCollisionInfo(Physics2D.Body body1, Physics2D.Body body2,
TrueSync.Physics2D.Contact c) {
195 if (!collisionInfo.ContainsKey(body1)) {
196 collisionInfo.Add(body1,
new Dictionary<Physics2D.Body, TSCollision2D>());
199 Dictionary<Physics2D.Body,
TSCollision2D> collisionInfoBody1 = collisionInfo[body1];
203 if (collisionInfoBody1.ContainsKey(body2)) {
204 result = collisionInfoBody1[body2];
207 collisionInfoBody1.Add(body2, result);
210 result.Update(gameObjectMap[body2], c);
215 public GameObject GetGameObject(
IBody body) {
216 return gameObjectMap[body];
219 public bool Raycast(
TSVector rayOrigin,
TSVector rayDirection, RaycastCallback raycast, out
IBody body, out
TSVector normal, out FP fraction) {
220 throw new NotImplementedException();
223 public TSRaycastHit Raycast(
TSRay ray, FP maxDistance, RaycastCallback callback = null) {
224 throw new NotImplementedException();
227 public bool IsCollisionEnabled(
IBody rigidBody1,
IBody rigidBody2) {
228 if (gameObjectMap.ContainsKey(rigidBody1) && gameObjectMap.ContainsKey(rigidBody2)) {
235 public IWorldClone GetWorldClone() {
236 return new WorldClone2D();
Manages the collision matrix of physics simulation.
Manages creation of player prefabs and lockstep execution.
FP y
The Y component of the vector.
Represents information about a contact between two 2D bodies.
Represents few information about a raycast hit.
static Physics2DWorldManager instance
Public access to a manager instance.
static void UpdateCoroutines()
Update all coroutines created.
void Initialize(Physics2D.World world)
Initializes Shape and RigidBody and sets initial values to position and orientation based on Unity's ...
Provides a few utilities to be used on TrueSync exposed classes.
TSVector Gravity
Property access to simulated gravity.
static TSBody2DComparer body2DComparer
Instance of a TSBody2DComparer.
static bool CollisionEnabled(int layerA, int layerB)
Returns true if the given layers can collide.
Represents a ray with origin and direction.
Manages the 2D physics simulation.
bool SpeculativeContacts
Property access to speculative contacts.
FP x
The X component of the vector.
TSVector2 ScaledCenter
Returns a version of collider's center scaled by parent's transform.
Abstract collider for 2D shapes.
IBody2D Body
Returns RigidBody associated to this TSRigidBody.
void UpdateStep()
Goes one step further on the physics simulation.
Represents a common interface to 2D and 3D bodies.