private TSCollision2D GetCollisionInfo(Physics2D.Body body1, Physics2D.Body body2, TrueSync.Physics2D.Contact c) { if (!collisionInfo.ContainsKey(body1)) { collisionInfo.Add(body1, new Dictionary <Physics2D.Body, TSCollision2D>()); } Dictionary <Physics2D.Body, TSCollision2D> collisionInfoBody1 = collisionInfo[body1]; TSCollision2D result = null; if (collisionInfoBody1.ContainsKey(body2)) { result = collisionInfoBody1[body2]; } else { result = new TSCollision2D(); collisionInfoBody1.Add(body2, result); } result.Update(gameObjectMap[body2], c); return(result); }
public void AddBody(ICollider iCollider) { if (!(iCollider is TSCollider2D)) { Debug.LogError("You have a 3D object but your Physics 3D is disabled."); return; } TSCollider2D tsCollider = (TSCollider2D)iCollider; if (tsCollider._body != null) { //already added return; } tsCollider.Initialize(world); gameObjectMap[tsCollider._body] = tsCollider.gameObject; if (tsCollider.gameObject.transform.parent != null && tsCollider.gameObject.transform.parent.GetComponentInParent <TSCollider2D>() != null) { TSCollider2D parentCollider = tsCollider.gameObject.transform.parent.GetComponentInParent <TSCollider2D>(); Physics2D.Body childBody = tsCollider._body; childBody.bodyConstraints.Add(new ConstraintHierarchy2D(((Physics2D.Body)parentCollider.Body), tsCollider._body, (tsCollider.GetComponent <TSTransform2D>().position + tsCollider.ScaledCenter) - (parentCollider.GetComponent <TSTransform2D>().position + parentCollider.ScaledCenter))); } world.ProcessAddedBodies(); }
// CALLBACKS private void OnRemovedRigidbody(Physics2D.Body i_Body) { GameObject go = m_GameObjectMap[i_Body]; // Remove cached rb. if (go != null) { TSRigidBody2D attachedRigidBody = go.GetComponent <TSRigidBody2D>(); if (attachedRigidBody != null) { m_RigidBodies.Remove(attachedRigidBody); } } // Destroy object. if (go != null) { GameObject.Destroy(go); } // Clear object map. m_GameObjectMap.Remove(i_Body); }
// INTERNALS private void CreateBodyFixture(Physics2D.Body i_Body, Physics2D.Shape i_Shape, FP i_Friction, FP i_Restitution) { Physics2D.Fixture fixture = i_Body.CreateFixture(i_Shape); fixture.Friction = i_Friction; fixture.Restitution = i_Restitution; }
private void OnRemovedRigidBody(TrueSync.Physics2D.Body body) { GameObject go = gameObjectMap[body]; if (go != null) { GameObject.Destroy(go); } }
private void CreateBodyFixture(Physics2D.Body body, Physics2D.Shape shape) { Physics2D.Fixture fixture = body.CreateFixture(shape); if (tsMaterial != null) { fixture.Friction = tsMaterial.friction; fixture.Restitution = tsMaterial.restitution; } }
private void CreateBody(Physics2D.World world) { Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world); if (tsMaterial == null) { tsMaterial = GetComponent <TSMaterial>(); } Physics2D.Shape shape = Shape; if (shape != null) { CreateBodyFixture(body, shape); } else { Physics2D.Shape[] shapes = CreateShapes(); for (int index = 0, length = shapes.Length; index < length; index++) { CreateBodyFixture(body, shapes[index]); } } if (tsRigidBody == null) { body.BodyType = Physics2D.BodyType.Static; } else { if (tsRigidBody.isKinematic) { body.BodyType = TrueSync.Physics2D.BodyType.Kinematic; } else { body.BodyType = Physics2D.BodyType.Dynamic; body.IgnoreGravity = !tsRigidBody.useGravity; } if (tsRigidBody.mass <= 0) { tsRigidBody.mass = 1; } body.FixedRotation = tsRigidBody.freezeZAxis; body.Mass = tsRigidBody.mass; body.TSLinearDrag = tsRigidBody.drag; body.TSAngularDrag = tsRigidBody.angularDrag; } body.IsSensor = isTrigger; body.CollidesWith = Physics2D.Category.All; _body = body; }
public void AddBody(ICollider i_Collider) { if (i_Collider == null) { return; } bool is2dCollider = (i_Collider is TSCollider2D); Debug.Assert(is2dCollider, "3D Physics is not supported."); if (!is2dCollider) { return; } TSCollider2D tsCollider = (TSCollider2D)i_Collider; Debug.Assert(tsCollider.body == null, "Body already added."); if (tsCollider.body != null) { return; } tsCollider.Initialize(m_World); m_GameObjectMap[tsCollider.body] = tsCollider.gameObject; Transform parent = tsCollider.transform.parent; TSCollider2D parentCollider = (parent != null) ? parent.GetComponentInParent <TSCollider2D>() : null; if (parentCollider != null) { Physics2D.Body childBody = tsCollider.body; TSTransform2D parentTransform = parentCollider.GetComponent <TSTransform2D>(); TSTransform2D colliderTransform = tsCollider.GetComponent <TSTransform2D>(); childBody.bodyConstraints.Add(new ConstraintHierarchy2D(parentCollider.body, tsCollider.body, (colliderTransform.position + tsCollider.center) - (parentTransform.position + parentCollider.center))); } TSRigidBody2D attachedRigidBody = tsCollider.GetComponent <TSRigidBody2D>(); if (attachedRigidBody != null) { m_RigidBodies.Add(attachedRigidBody); } m_World.ProcessAddedBodies(); }
private void CreateBody(Physics2D.World world) { Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world); Physics2D.Fixture fixture = body.CreateFixture(Shape); if (tsMaterial == null) { tsMaterial = GetComponent <TSMaterial>(); } if (tsMaterial != null) { fixture.Friction = tsMaterial.friction; fixture.Restitution = tsMaterial.restitution; } if (tsRigidBody == null) { body.BodyType = Physics2D.BodyType.Static; } else { if (tsRigidBody.isKinematic) { body.BodyType = TrueSync.Physics2D.BodyType.Kinematic; } else { body.BodyType = Physics2D.BodyType.Dynamic; body.IgnoreGravity = !tsRigidBody.useGravity; } if (tsRigidBody.mass <= 0) { tsRigidBody.mass = 1; } body.FixedRotation = tsRigidBody.freezeZAxis; body.Mass = tsRigidBody.mass; body.TSLinearDrag = tsRigidBody.drag; body.TSAngularDrag = tsRigidBody.angularDrag; } body.IsSensor = isTrigger; body.CollidesWith = Physics2D.Category.All; _body = body; }
private void CallSyncedTriggerExit(Physics2D.Body i_Body1, Physics2D.Body i_Body2, Physics2D.Contact i_Contact) { GameObject b1; m_GameObjectMap.TryGetValue(i_Body1, out b1); GameObject b2; m_GameObjectMap.TryGetValue(i_Body2, out b2); if (b1 == null || b2 == null) { return; } // Notify b1. { TrueSyncBehaviour[] trueSyncBehaviours = b1.GetComponentsInChildren <TrueSyncBehaviour>(); if (trueSyncBehaviours != null) { for (int index = 0; index < trueSyncBehaviours.Length; ++index) { TrueSyncBehaviour behaviour = trueSyncBehaviours[index]; behaviour.OnSyncedTriggerExit(FillCollisionInfo(b2, i_Contact)); } } } // Notify b2. { TrueSyncBehaviour[] trueSyncBehaviours = b2.GetComponentsInChildren <TrueSyncBehaviour>(); if (trueSyncBehaviours != null) { for (int index = 0; index < trueSyncBehaviours.Length; ++index) { TrueSyncBehaviour behaviour = trueSyncBehaviours[index]; behaviour.OnSyncedTriggerExit(FillCollisionInfo(b1, i_Contact)); } } } // Clear cache. ClearCollisionCache(); }
private static object OverlapGeneric(Physics2D.Shape i_Shape, TSVector2 i_Position, Physics2D.BodySpecialSensor i_SensorType, int i_Mask) { Physics2D.World world = (Physics2D.World)PhysicsManager.GetWorld(); Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world); body.CreateFixture(i_Shape); body.BodyType = Physics2D.BodyType.Static; body.CollisionCategories = Physics2D.Category.All; // Category.All is used for sweep test objects. body.CollidesWith = (Physics2D.Category)i_Mask; body.CollisionGroup = 0; body.IsSensor = true; body.SpecialSensor = i_SensorType; body.SpecialSensorMask = (int)Physics2D.Category.All; body.Position = i_Position; world.RemoveBody(body); world.ProcessRemovedBodies(); if (body._specialSensorResults.Count > 0) { if (i_SensorType == Physics2D.BodySpecialSensor.ActiveOnce) { return(PhysicsManager.GetGameObject(body._specialSensorResults[0]).GetComponent <TSCollider2D>()); } else { TSCollider2D[] result = new TSCollider2D[body._specialSensorResults.Count]; for (int i = 0; i < body._specialSensorResults.Count; i++) { result[i] = PhysicsManager.GetGameObject(body._specialSensorResults[i]).GetComponent <TSCollider2D>(); } return(result); } } return(null); }
private void CollisionDetected(Physics2D.Body body1, Physics2D.Body body2, TrueSync.Physics2D.Contact contact, string callbackName) { if (!gameObjectMap.ContainsKey(body1) || !gameObjectMap.ContainsKey(body2)) { return; } GameObject b1 = gameObjectMap[body1]; GameObject b2 = gameObjectMap[body2]; if (b1 == null || b2 == null) { return; } b1.SendMessage(callbackName, GetCollisionInfo(body1, body2, contact), SendMessageOptions.DontRequireReceiver); b2.SendMessage(callbackName, GetCollisionInfo(body2, body1, contact), SendMessageOptions.DontRequireReceiver); TrueSyncManager.UpdateCoroutines(); }
public void AddBody(ICollider iCollider) { if (!(iCollider is TSCollider2D)) { Debug.LogError("You have a 3D object but your Physics 3D is disabled."); return; } TSCollider2D tsCollider = (TSCollider2D)iCollider; if (tsCollider._body != null) { //already added return; } tsCollider.Initialize(world); GameObject gameObject = tsCollider.gameObject; gameObjectMap[tsCollider._body] = gameObject; HashList <TrueSyncBehaviour> behaviours = new HashList <TrueSyncBehaviour>(); TrueSyncBehaviour[] behavioursArray = gameObject.GetComponents <TrueSyncBehaviour>(); for (int i = 0, count = behavioursArray.Length; i < count; i++) { behaviours.Add(behavioursArray[i]); } behavioursMap[tsCollider._body] = behaviours; if (tsCollider.gameObject.transform.parent != null && tsCollider.gameObject.transform.parent.GetComponentInParent <TSCollider2D>() != null) { TSCollider2D parentCollider = tsCollider.gameObject.transform.parent.GetComponentInParent <TSCollider2D>(); Physics2D.Body childBody = tsCollider._body; childBody.bodyConstraints.Add(new ConstraintHierarchy2D(((Physics2D.Body)parentCollider.Body), tsCollider._body, (tsCollider.GetComponent <TSTransform2D>().position + tsCollider.ScaledCenter) - (parentCollider.GetComponent <TSTransform2D>().position + parentCollider.ScaledCenter))); } world.ProcessAddedBodies(); }
private static object OverlapGeneric(Physics2D.Shape shape, TSVector2 position, Physics2D.BodySpecialSensor sensorType, int layerMask) { Physics2D.World world = (Physics2D.World)Physics2DWorldManager.instance.GetWorld(); Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world); body.CreateFixture(shape); body.BodyType = Physics2D.BodyType.Static; body.IsSensor = true; body.CollidesWith = Physics2D.Category.All; body.SpecialSensor = sensorType; body.SpecialSensorMask = layerMask; body.Position = position; world.RemoveBody(body); world.ProcessRemovedBodies(); if (body._specialSensorResults.Count > 0) { if (sensorType == Physics2D.BodySpecialSensor.ActiveOnce) { return(Physics2DWorldManager.instance.GetGameObject(body._specialSensorResults[0]).GetComponent <TSCollider2D>()); } else { TSCollider2D[] result = new TSCollider2D[body._specialSensorResults.Count]; for (int i = 0; i < body._specialSensorResults.Count; i++) { result[i] = Physics2DWorldManager.instance.GetGameObject(body._specialSensorResults[i]).GetComponent <TSCollider2D>(); } return(result); } } return(null); }
protected Joint(Body bodyA, Body bodyB) : this() { Debug.Assert(bodyA != bodyB); this.BodyA = bodyA; this.BodyB = bodyB; }
protected Joint(Body body) : this() { this.BodyA = body; }
public AngleJoint(Body bodyA, Body bodyB) : base(bodyA, bodyB) { base.JointType = JointType.Angle; this.BiasFactor = 0.2f; this.MaxImpulse = FP.MaxValue; }
private void CreateBody(Physics2D.World i_World) { Physics2D.Body body = Physics2D.BodyFactory.CreateBody(i_World); // Get collision/restitution data from physics material. FP materialFriction = FP.Zero; FP materialRestitution = FP.Zero; if (m_PhysicsMaterial != null) { materialFriction = m_PhysicsMaterial.friction; materialRestitution = m_PhysicsMaterial.restitution; } else { // Check for a TSMaterial2D. Note: this is deprecated. m_TSMaterial = GetComponent <TSMaterial>(); if (m_TSMaterial != null) { materialFriction = m_TSMaterial.friction; materialRestitution = m_TSMaterial.restitution; } } // Create collision shape(s). Physics2D.Shape shape = CreateShape(); if (shape != null) { CreateBodyFixture(body, shape, materialFriction, materialRestitution); } else { Physics2D.Shape[] shapes = CreateShapes(); for (int index = 0, length = shapes.Length; index < length; index++) { CreateBodyFixture(body, shapes[index], materialFriction, materialRestitution); } } // Setup RigidBody. if (m_TSRigidBody == null) { body.BodyType = Physics2D.BodyType.Static; } else { if (m_TSRigidBody.isKinematic) { body.BodyType = Physics2D.BodyType.Kinematic; } else { body.BodyType = Physics2D.BodyType.Dynamic; body.IgnoreGravity = !m_TSRigidBody.useGravity; } if (m_TSRigidBody.mass <= 0) { m_TSRigidBody.mass = 1; } body.FixedRotation = m_TSRigidBody.freezeZAxis; body.Mass = m_TSRigidBody.mass; body.TSLinearDrag = m_TSRigidBody.linearDrag; body.TSAngularDrag = m_TSRigidBody.angularDrag; } m_Body = body; m_Shape = shape; SetIsSensor(m_IsTrigger); SetCollisionLayer(gameObject.layer); }
private void CollisionDetected(Physics2D.Body body1, Physics2D.Body body2, TrueSync.Physics2D.Contact contact, string callbackName) { if (!gameObjectMap.ContainsKey(body1) || !gameObjectMap.ContainsKey(body2)) { return; } GameObject b1 = gameObjectMap[body1]; GameObject b2 = gameObjectMap[body2]; if (b1 == null || b2 == null) { return; } HashList <TrueSyncBehaviour> b1Behaviours = behavioursMap[body1]; for (int i = 0, count = b1Behaviours.Count; i < count; i++) { TSCollision2D collision = GetCollisionInfo(body1, body2, contact); if (String.Equals(callbackName, "OnSyncedCollisionEnter", StringComparison.InvariantCultureIgnoreCase)) { b1Behaviours[i].OnSyncedCollisionEnter2D(collision); } else if (String.Equals(callbackName, "OnSyncedCollisionStay", StringComparison.InvariantCultureIgnoreCase)) { b1Behaviours[i].OnSyncedCollisionStay2D(collision); } else if (String.Equals(callbackName, "OnSyncedCollisionExit", StringComparison.InvariantCultureIgnoreCase)) { b1Behaviours[i].OnSyncedCollisionExit2D(collision); } else if (String.Equals(callbackName, "OnSyncedTriggerEnter", StringComparison.InvariantCultureIgnoreCase)) { b1Behaviours[i].OnSyncedTriggerEnter2D(collision); } else if (String.Equals(callbackName, "OnSyncedTriggerStay", StringComparison.InvariantCultureIgnoreCase)) { b1Behaviours[i].OnSyncedTriggerStay2D(collision); } else if (String.Equals(callbackName, "OnSyncedTriggerExit", StringComparison.InvariantCultureIgnoreCase)) { b1Behaviours[i].OnSyncedTriggerExit2D(collision); } } HashList <TrueSyncBehaviour> b2Behaviours = behavioursMap[body2]; for (int i = 0, count = b2Behaviours.Count; i < count; i++) { TSCollision2D collision = GetCollisionInfo(body2, body1, contact); if (String.Equals(callbackName, "OnSyncedCollisionEnter", StringComparison.InvariantCultureIgnoreCase)) { b2Behaviours[i].OnSyncedCollisionEnter2D(collision); } else if (String.Equals(callbackName, "OnSyncedCollisionStay", StringComparison.InvariantCultureIgnoreCase)) { b2Behaviours[i].OnSyncedCollisionStay2D(collision); } else if (String.Equals(callbackName, "OnSyncedCollisionExit", StringComparison.InvariantCultureIgnoreCase)) { b2Behaviours[i].OnSyncedCollisionExit2D(collision); } else if (String.Equals(callbackName, "OnSyncedTriggerEnter", StringComparison.InvariantCultureIgnoreCase)) { b2Behaviours[i].OnSyncedTriggerEnter2D(collision); } else if (String.Equals(callbackName, "OnSyncedTriggerStay", StringComparison.InvariantCultureIgnoreCase)) { b2Behaviours[i].OnSyncedTriggerStay2D(collision); } else if (String.Equals(callbackName, "OnSyncedTriggerExit", StringComparison.InvariantCultureIgnoreCase)) { b2Behaviours[i].OnSyncedTriggerExit2D(collision); } } TrueSyncManager.UpdateCoroutines(); }