TrueSync: TSCollider2D.cs Source File

TrueSync

TSCollider2D.cs
1 using System;
2 using UnityEngine;
3 using UnityEngine.Serialization;
4 
5 namespace TrueSync {
9  [RequireComponent(typeof(TSTransform2D))]
10  [Serializable]
11  [ExecuteInEditMode]
12  public abstract class TSCollider2D : MonoBehaviour, ICollider {
13 
14  private Physics2D.Shape shape;
15 
19  public Physics2D.Shape Shape {
20  get {
21  if (shape == null)
22  shape = CreateShape();
23  return shape;
24  }
25  protected set { shape = value; }
26  }
27 
28  [FormerlySerializedAs("isTrigger")]
29  [SerializeField]
30  private bool _isTrigger;
31 
35  public bool isTrigger {
36 
37  get {
38  if (_body != null) {
39  return _body.IsSensor;
40  }
41 
42  return _isTrigger;
43  }
44 
45  set {
46  _isTrigger = value;
47 
48  if (_body != null) {
49  _body.IsSensor = value;
50  }
51  }
52 
53  }
54 
59 
60  [SerializeField]
61  private TSVector2 center;
62 
63  private Vector3 scaledCenter;
64 
65  internal Physics2D.Body _body;
66 
70  public IBody2D Body {
71  get {
72  if (_body == null) {
73  CheckPhysics();
74  }
75 
76  return _body;
77  }
78  }
79 
83  public TSVector2 Center {
84  get {
85  return center;
86  }
87  set {
88  center = value;
89  }
90  }
91 
95  public TSVector2 ScaledCenter {
96  get {
97  return TSVector2.Scale(center, new TSVector2(lossyScale.x, lossyScale.y));
98  }
99  }
100 
104  public virtual Physics2D.Shape CreateShape() {
105  return null;
106  }
107 
108  public virtual Physics2D.Shape[] CreateShapes() {
109  return new Physics2D.Shape[0];
110  }
111 
112  private TSRigidBody2D tsRigidBody;
113 
114  [HideInInspector]
115  public TSTransform2D tsTransform;
116 
121  get {
122  return tsRigidBody;
123  }
124  }
125 
129  [SerializeField]
130  [HideInInspector]
131  protected TSVector lossyScale = TSVector.one;
132 
136  public void Awake() {
137  tsTransform = this.GetComponent<TSTransform2D>();
138  tsRigidBody = this.GetComponent<TSRigidBody2D>();
139 
140  if (lossyScale == TSVector.one) {
141  lossyScale = TSVector.Abs(transform.localScale.ToTSVector());
142  }
143  }
144 
145  public void Update() {
146  if (!Application.isPlaying) {
147  lossyScale = TSVector.Abs(transform.lossyScale.ToTSVector());
148  }
149  }
150 
151  private void CreateBodyFixture(Physics2D.Body body, Physics2D.Shape shape) {
152  Physics2D.Fixture fixture = body.CreateFixture(shape);
153 
154  if (tsMaterial != null) {
155  fixture.Friction = tsMaterial.friction;
156  fixture.Restitution = tsMaterial.restitution;
157  }
158  }
159 
160  private void CreateBody(Physics2D.World world) {
161  Physics2D.Body body = Physics2D.BodyFactory.CreateBody(world);
162 
163  if (tsMaterial == null) {
164  tsMaterial = GetComponent<TSMaterial>();
165  }
166 
167  Physics2D.Shape shape = Shape;
168  if (shape != null) {
169  CreateBodyFixture(body, shape);
170  } else {
171  Physics2D.Shape[] shapes = CreateShapes();
172  for (int index = 0, length = shapes.Length; index < length; index++) {
173  CreateBodyFixture(body, shapes[index]);
174  }
175  }
176 
177  if (tsRigidBody == null) {
178  body.BodyType = Physics2D.BodyType.Static;
179  } else {
180  if (tsRigidBody.isKinematic) {
181  body.BodyType = TrueSync.Physics2D.BodyType.Kinematic;
182  } else {
183  body.BodyType = Physics2D.BodyType.Dynamic;
184  body.IgnoreGravity = !tsRigidBody.useGravity;
185  }
186 
187  if (tsRigidBody.mass <= 0) {
188  tsRigidBody.mass = 1;
189  }
190 
191  body.FixedRotation = tsRigidBody.freezeZAxis;
192  body.Mass = tsRigidBody.mass;
193  body.TSLinearDrag = tsRigidBody.drag;
194  body.TSAngularDrag = tsRigidBody.angularDrag;
195  }
196 
197  body.IsSensor = isTrigger;
198  body.CollidesWith = Physics2D.Category.All;
199 
200  _body = body;
201  }
202 
206  public void Initialize(Physics2D.World world) {
207  CreateBody(world);
208  }
209 
210  private void CheckPhysics() {
211  if (_body == null && PhysicsManager.instance != null) {
212  PhysicsManager.instance.AddBody(this);
213  }
214  }
215 
219  public virtual void OnDrawGizmos() {
220  if (!this.enabled) {
221  return;
222  }
223 
224  Vector3 position = _body != null ? _body.TSPosition.ToVector() : (transform.position + ScaledCenter.ToVector());
225  Quaternion rotation = _body != null ? Quaternion.Euler(0, 0, (_body.Rotation * FP.Rad2Deg).AsFloat()) : transform.rotation;
226 
227  Gizmos.color = Color.yellow;
228 
229  Matrix4x4 cubeTransform = Matrix4x4.TRS(position, rotation, GetGizmosSize());
230  Matrix4x4 oldGizmosMatrix = Gizmos.matrix;
231 
232  Gizmos.matrix *= cubeTransform;
233 
234  DrawGizmos();
235 
236  Gizmos.matrix = oldGizmosMatrix;
237  }
238 
242  protected abstract Vector3 GetGizmosSize();
243 
247  protected abstract void DrawGizmos();
248 
252  public bool IsBodyInitialized {
253  get {
254  return _body != null;
255  }
256  }
257 
258  }
259 
260 }
void Awake()
Creates a new TSRigidBody when there is no one attached to this GameObject.
bool isTrigger
If it is only a trigger and doesn&#39;t interfere on collisions.
Definition: TSCollider2D.cs:35
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 ...
A deterministic version of Unity&#39;s Transform component for 2D physics.
Definition: TSTransform2D.cs:9
FP angularDrag
Angular drag coeficient.
bool IsBodyInitialized
Returns true if the body was already initialized.
static IPhysicsManager instance
Returns a proper implementation of IPhysicsManager.
bool useGravity
If true it uses gravity force.
Simulates physical properties of a body.
Definition: TSMaterial.cs:9
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:83
virtual Physics2D.Shape CreateShape()
Creates the shape related to a concrete implementation of TSCollider.
TSRigidBody2D attachedRigidbody
Returns the TSRigidBody attached.
Physics2D.Shape Shape
Shape used by a collider.
Definition: TSCollider2D.cs:19
abstract void DrawGizmos()
Draws the specific gizmos of concrete collider (for example "Gizmos.DrawWireCube" for a TSBoxCollider...
FP drag
Linear drag coeficient.
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).
bool isKinematic
If true it doesn&#39;t get influences from external forces.
TSMaterial tsMaterial
Simulated material.
Definition: TSCollider2D.cs:58
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:95
Abstract collider for 2D shapes.
Definition: TSCollider2D.cs:12
abstract Vector3 GetGizmosSize()
Returns the gizmos size.
IBody2D Body
Returns RigidBody associated to this TSRigidBody.
Definition: TSCollider2D.cs:70
Generated by   doxygen 1.8.11