TrueSync
TSCollider.cs
1 using System;
2 using System.Linq;
3 using UnityEngine;
4 
5 namespace TrueSync {
9  [RequireComponent(typeof(TSTransform))]
10  [Serializable]
11  [ExecuteInEditMode]
12  public abstract class TSCollider : MonoBehaviour, ICollider {
13 
14  private Shape shape;
15 
19  public Shape Shape {
20  get {
21  if (shape == null)
22  shape = CreateShape();
23  return shape;
24  }
25  protected set { shape = value; }
26  }
27 
31  public bool isTrigger;
32 
37 
38  [SerializeField]
39  private TSVector center;
40 
41  private Vector3 scaledCenter;
42 
43  internal RigidBody _body;
44 
48  public TSVector Center {
49  get {
50  return center;
51  }
52  set {
53  center = value;
54  }
55  }
56 
61  get {
62  return TSVector.Scale (Center, lossyScale);
63  }
64  }
65 
69  public abstract Shape CreateShape();
70 
71  private TSRigidBody tsRigidBody;
72 
77  get {
78  return tsRigidBody;
79  }
80  }
81 
85  public TSBBox bounds {
86  get {
87  return this._body.BoundingBox;
88  }
89  }
90 
94  public IBody3D Body {
95  get {
96  if (_body == null) {
97  CheckPhysics();
98  }
99 
100  return _body;
101  }
102  }
103 
107  [SerializeField]
108  [HideInInspector]
110 
111  [HideInInspector]
112  public TSTransform tsTransform;
113 
117  public void Awake() {
118  tsTransform = this.GetComponent<TSTransform>();
119  tsRigidBody = this.GetComponent<TSRigidBody>();
120 
121  if (lossyScale == TSVector.one) {
122  lossyScale = TSVector.Abs(transform.localScale.ToTSVector());
123  }
124  }
125 
126  public void Update() {
127  if (!Application.isPlaying) {
128  lossyScale = TSVector.Abs(transform.lossyScale.ToTSVector());
129  }
130  }
131 
132  private void CreateBody() {
133  RigidBody newBody = new RigidBody(Shape);
134 
135  if (tsMaterial == null) {
136  tsMaterial = GetComponent<TSMaterial>();
137  }
138 
139  if (tsMaterial != null) {
140  newBody.Material = new BodyMaterial();
141  newBody.Material.KineticFriction = tsMaterial.friction;
142  newBody.Material.StaticFriction = tsMaterial.friction;
143  newBody.Material.Restitution = tsMaterial.restitution;
144  }
145 
146  newBody.IsColliderOnly = isTrigger;
147  newBody.IsKinematic = tsRigidBody != null && tsRigidBody.isKinematic;
148 
149  bool isStatic = tsRigidBody == null || tsRigidBody.isKinematic;
150 
151  if (isStatic) {
152  newBody.AffectedByGravity = false;
153  newBody.IsStatic = true;
154  } else if (tsRigidBody != null) {
155  newBody.AffectedByGravity = tsRigidBody.useGravity;
156 
157  if (tsRigidBody.mass <= 0) {
158  tsRigidBody.mass = 1;
159  }
160 
161  newBody.Mass = tsRigidBody.mass;
162  } else {
163  newBody.SetMassProperties();
164  }
165 
166  _body = newBody;
167  }
168 
172  public void Initialize() {
173  CreateBody();
174  }
175 
176  private void CheckPhysics() {
177  if (_body == null && PhysicsManager.instance != null) {
178  PhysicsManager.instance.AddBody(this);
179  }
180  }
181 
185  public virtual void OnDrawGizmos() {
186  if (!this.enabled) {
187  return;
188  }
189 
190  Vector3 position = _body != null ? _body.Position.ToVector() : (transform.position + ScaledCenter.ToVector());
191  Quaternion rotation = _body != null ? _body.Orientation.ToQuaternion() : transform.rotation;
192 
193  Gizmos.color = Color.yellow;
194 
195  Matrix4x4 cubeTransform = Matrix4x4.TRS(position, rotation, GetGizmosSize());
196  Matrix4x4 oldGizmosMatrix = Gizmos.matrix;
197 
198  Gizmos.matrix *= cubeTransform;
199 
200  DrawGizmos();
201 
202  Gizmos.matrix = oldGizmosMatrix;
203  }
204 
208  protected abstract Vector3 GetGizmosSize();
209 
213  protected abstract void DrawGizmos();
214 
218  public bool IsBodyInitialized {
219  get {
220  return _body != null;
221  }
222  }
223  }
224 
225 }
Shape Shape
Shape used by a collider.
Definition: TSCollider.cs:19
bool useGravity
If true it uses gravity force.
Definition: TSRigidBody.cs:46
FP mass
Mass of the body.
Definition: TSRigidBody.cs:22
Represents a physical 3D rigid body.
Definition: TSRigidBody.cs:11
A deterministic version of Unity&#39;s Transform component for 3D physics.
Definition: TSTransform.cs:9
bool isTrigger
If it is only a trigger and doesn&#39;t interfere on collisions.
Definition: TSCollider.cs:31
static IPhysicsManager instance
Returns a proper implementation of IPhysicsManager.
A vector structure.
Definition: TSVector.cs:29
void Scale(TSVector other)
Multiplies each component of the vector by the same components of the provided vector.
Definition: TSVector.cs:168
bool isKinematic
If true it doesn&#39;t get influences from external forces.
Definition: TSRigidBody.cs:70
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
FP friction
Static friction when in contact.
Definition: TSMaterial.cs:14
Abstract collider for 3D shapes.
Definition: TSCollider.cs:12
FP restitution
Coeficient of restitution.
Definition: TSMaterial.cs:19
TSVector ScaledCenter
Returns a version of collider&#39;s center scaled by parent&#39;s transform.
Definition: TSCollider.cs:60
TSBBox bounds
Returns body&#39;s boundind box.
Definition: TSCollider.cs:85
TSRigidBody attachedRigidbody
Returns the TSRigidBody attached.
Definition: TSCollider.cs:76
TSVector lossyScale
Holds an first value of the GameObject&#39;s lossy scale.
Definition: TSCollider.cs:109
void Awake()
Creates a new TSRigidBody when there is no one attached to this GameObject.
Definition: TSCollider.cs:117
Represents an interface to 3D bodies.
Definition: IBody3D.cs:6
abstract Shape CreateShape()
Creates the shape related to a concrete implementation of TSCollider.
void Initialize()
Initializes Shape and RigidBody and sets initial values to position and orientation based on Unity&#39;s ...
Definition: TSCollider.cs:172
abstract void DrawGizmos()
Draws the specific gizmos of concrete collider (for example "Gizmos.DrawWireCube" for a TSBoxCollider...
TSVector Center
Center of the collider shape.
Definition: TSCollider.cs:48
Manages physics simulation.
abstract Vector3 GetGizmosSize()
Returns the gizmos size.
bool IsBodyInitialized
Returns true if the body was already initialized.
Definition: TSCollider.cs:218
IBody3D Body
Returns the body linked to this collider.
Definition: TSCollider.cs:94
virtual void OnDrawGizmos()
Do a base matrix transformation to draw correctly all collider gizmos.
Definition: TSCollider.cs:185
TSMaterial tsMaterial
Simulated material.
Definition: TSCollider.cs:36
Bounding Box defined through min and max vectors.
Definition: TSBBox.cs:25