TrueSync
TSCollider2D.cs
1 using System;
2 using UnityEngine;
3 
4 namespace TrueSync {
8  [RequireComponent(typeof(TSTransform2D))]
9  [Serializable]
10  [ExecuteInEditMode]
11  public abstract class TSCollider2D : MonoBehaviour, ICollider {
12 
13  private Physics2D.Shape shape;
14 
18  public Physics2D.Shape Shape {
19  get {
20  if (shape == null)
21  shape = CreateShape();
22  return shape;
23  }
24  protected set { shape = value; }
25  }
26 
30  public bool isTrigger;
31 
36 
37  [SerializeField]
38  private TSVector2 center;
39 
40  private Vector3 scaledCenter;
41 
42  internal Physics2D.Body _body;
43 
47  public IBody2D Body {
48  get {
49  if (_body == null) {
50  CheckPhysics();
51  }
52 
53  return _body;
54  }
55  }
56 
60  public TSVector2 Center {
61  get {
62  return center;
63  }
64  set {
65  center = value;
66  }
67  }
68 
72  public TSVector2 ScaledCenter {
73  get {
74  return TSVector2.Scale(center, new TSVector2(lossyScale.x, lossyScale.y));
75  }
76  }
77 
81  public abstract Physics2D.Shape CreateShape();
82 
83  private TSRigidBody2D tsRigidBody;
84 
85  [HideInInspector]
86  public TSTransform2D tsTransform;
87 
92  get {
93  return tsRigidBody;
94  }
95  }
96 
100  [SerializeField]
101  [HideInInspector]
103 
107  public void Awake() {
108  tsTransform = this.GetComponent<TSTransform2D>();
109  tsRigidBody = this.GetComponent<TSRigidBody2D>();
110 
111  if (lossyScale == TSVector.one) {
112  lossyScale = TSVector.Abs(transform.localScale.ToTSVector());
113  }
114  }
115 
116  public void Update() {
117  if (!Application.isPlaying) {
118  lossyScale = TSVector.Abs(transform.lossyScale.ToTSVector());
119  }
120  }
121 
122  private void CreateBody(Physics2D.World world) {
123  Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world);
124  Physics2D.Fixture fixture = body.CreateFixture(Shape);
125 
126  if (tsMaterial == null) {
127  tsMaterial = GetComponent<TSMaterial>();
128  }
129 
130  if (tsMaterial != null) {
131  fixture.Friction = tsMaterial.friction;
132  fixture.Restitution = tsMaterial.restitution;
133  }
134 
135  if (tsRigidBody == null) {
136  body.BodyType = Physics2D.BodyType.Static;
137  } else {
138  if (tsRigidBody.isKinematic) {
139  body.BodyType = TrueSync.Physics2D.BodyType.Kinematic;
140  } else {
141  body.BodyType = Physics2D.BodyType.Dynamic;
142  body.IgnoreGravity = !tsRigidBody.useGravity;
143  }
144 
145  if (tsRigidBody.mass <= 0) {
146  tsRigidBody.mass = 1;
147  }
148 
149  body.FixedRotation = tsRigidBody.freezeZAxis;
150  body.Mass = tsRigidBody.mass;
151  }
152 
153  body.IsSensor = isTrigger;
154  body.CollidesWith = Physics2D.Category.All;
155 
156  _body = body;
157  }
158 
162  public void Initialize(Physics2D.World world) {
163  CreateBody(world);
164  }
165 
166  private void CheckPhysics() {
167  if (_body == null && PhysicsManager.instance != null) {
168  PhysicsManager.instance.AddBody(this);
169  }
170  }
171 
175  public virtual void OnDrawGizmos() {
176  if (!this.enabled) {
177  return;
178  }
179 
180  Vector3 position = _body != null ? _body.TSPosition.ToVector() : (transform.position + ScaledCenter.ToVector());
181  Quaternion rotation = _body != null ? Quaternion.Euler(0, 0, (_body.Rotation * FP.Rad2Deg).AsFloat()) : transform.rotation;
182 
183  Gizmos.color = Color.yellow;
184 
185  Matrix4x4 cubeTransform = Matrix4x4.TRS(position, rotation, GetGizmosSize());
186  Matrix4x4 oldGizmosMatrix = Gizmos.matrix;
187 
188  Gizmos.matrix *= cubeTransform;
189 
190  DrawGizmos();
191 
192  Gizmos.matrix = oldGizmosMatrix;
193  }
194 
198  protected abstract Vector3 GetGizmosSize();
199 
203  protected abstract void DrawGizmos();
204 
208  public bool IsBodyInitialized {
209  get {
210  return _body != null;
211  }
212  }
213 
214  }
215 
216 }
void Awake()
Creates a new TSRigidBody when there is no one attached to this GameObject.
FP y
The Y component of the vector.
Definition: TSVector.cs:39
bool isTrigger
If it is only a trigger and doesn&#39;t interfere on collisions.
Definition: TSCollider2D.cs:30
FP mass
Mass of the body.
void Initialize(Physics2D.World world)
Initializes Shape and RigidBody and sets initial values to position and orientation based on Unity&#39;s ...
abstract Physics2D.Shape CreateShape()
Creates the shape related to a concrete implementation of TSCollider.
A deterministic version of Unity&#39;s Transform component for 2D physics.
Definition: TSTransform2D.cs:9
bool IsBodyInitialized
Returns true if the body was already initialized.
static IPhysicsManager instance
Returns a proper implementation of IPhysicsManager.
A vector structure.
Definition: TSVector.cs:29
bool useGravity
If true it uses gravity force.
Simulates physical properties of a body.
Definition: TSMaterial.cs:9
static readonly TSVector one
A vector with components (1,1,1);
Definition: TSVector.cs:75
virtual void OnDrawGizmos()
Do a base matrix transformation to draw correctly all collider gizmos.
FP friction
Static friction when in contact.
Definition: TSMaterial.cs:14
FP restitution
Coeficient of restitution.
Definition: TSMaterial.cs:19
TSVector2 Center
Center of the collider shape.
Definition: TSCollider2D.cs:60
TSRigidBody2D attachedRigidbody
Returns the TSRigidBody attached.
Definition: TSCollider2D.cs:91
Physics2D.Shape Shape
Shape used by a collider.
Definition: TSCollider2D.cs:18
abstract void DrawGizmos()
Draws the specific gizmos of concrete collider (for example "Gizmos.DrawWireCube" for a TSBoxCollider...
Represents an interface to 2D bodies.
Definition: IBody2D.cs:6
TSVector lossyScale
Holds an first value of the GameObject&#39;s lossy scale.
Manages physics simulation.
bool freezeZAxis
If true it freezes Z rotation of the RigidBody (it only appears when in 2D Physics).
FP x
The X component of the vector.
Definition: TSVector.cs:37
bool isKinematic
If true it doesn&#39;t get influences from external forces.
TSMaterial tsMaterial
Simulated material.
Definition: TSCollider2D.cs:35
Represents a physical 2D rigid body.
TSVector2 ScaledCenter
Returns a version of collider&#39;s center scaled by parent&#39;s transform.
Definition: TSCollider2D.cs:72
Abstract collider for 2D shapes.
Definition: TSCollider2D.cs:11
abstract Vector3 GetGizmosSize()
Returns the gizmos size.
IBody2D Body
Returns RigidBody associated to this TSRigidBody.
Definition: TSCollider2D.cs:47