TrueSync: TSCollider.cs Source File

TrueSync

TSCollider.cs
1 using System;
2 using UnityEngine;
3 using UnityEngine.Serialization;
4 using TrueSync.Physics3D;
5 
6 namespace TrueSync {
10  [RequireComponent(typeof(TSTransform))]
11  [Serializable]
12  [ExecuteInEditMode]
13  public abstract class TSCollider : MonoBehaviour, ICollider {
14 
15  private Shape shape;
16 
20  public Shape Shape {
21  get {
22  if (shape == null)
23  shape = CreateShape();
24  return shape;
25  }
26  protected set { shape = value; }
27  }
28 
29  [FormerlySerializedAs("isTrigger")]
30  [SerializeField]
31  private bool _isTrigger;
32 
36  public bool isTrigger {
37  get {
38  if (_body != null) {
39  return _body.IsColliderOnly;
40  }
41 
42  return _isTrigger;
43  }
44  set {
45  _isTrigger = value;
46 
47  if (_body != null) {
48  _body.IsColliderOnly = _isTrigger;
49  }
50  }
51  }
52 
57 
58  [SerializeField]
59  private TSVector center;
60 
61  private Vector3 scaledCenter;
62 
63  internal RigidBody _body;
64 
68  public TSVector Center {
69  get {
70  return center;
71  }
72  set {
73  center = value;
74  }
75  }
76 
80  public TSVector ScaledCenter {
81  get {
82  return TSVector.Scale (Center, lossyScale);
83  }
84  }
85 
89  public abstract Shape CreateShape();
90 
91  private TSRigidBody tsRigidBody;
92 
97  get {
98  return tsRigidBody;
99  }
100  }
101 
105  public TSBBox bounds {
106  get {
107  return this._body.BoundingBox;
108  }
109  }
110 
114  public IBody3D Body {
115  get {
116  if (_body == null) {
117  CheckPhysics();
118  }
119 
120  return _body;
121  }
122  }
123 
127  [SerializeField]
128  [HideInInspector]
129  protected TSVector lossyScale = TSVector.one;
130 
131  [HideInInspector]
132  public TSTransform tsTransform;
133 
137  public void Awake() {
138  tsTransform = this.GetComponent<TSTransform>();
139  tsRigidBody = this.GetComponent<TSRigidBody>();
140 
141  if (lossyScale == TSVector.one) {
142  lossyScale = TSVector.Abs(transform.localScale.ToTSVector());
143  }
144  }
145 
146  public void Update() {
147  if (!Application.isPlaying) {
148  lossyScale = TSVector.Abs(transform.lossyScale.ToTSVector());
149  }
150  }
151 
152  private void CreateBody() {
153  RigidBody newBody = new RigidBody(Shape);
154 
155  if (tsMaterial == null) {
156  tsMaterial = GetComponent<TSMaterial>();
157  }
158 
159  if (tsMaterial != null) {
160  newBody.TSFriction = tsMaterial.friction;
161  newBody.TSRestitution = tsMaterial.friction;
162  }
163 
164  newBody.IsColliderOnly = isTrigger;
165  newBody.IsKinematic = tsRigidBody != null && tsRigidBody.isKinematic;
166 
167  bool isStatic = tsRigidBody == null || tsRigidBody.isKinematic;
168 
169  if (tsRigidBody != null) {
170  newBody.AffectedByGravity = tsRigidBody.useGravity;
171 
172  if (tsRigidBody.mass <= 0) {
173  tsRigidBody.mass = 1;
174  }
175 
176  newBody.Mass = tsRigidBody.mass;
177  newBody.TSLinearDrag = tsRigidBody.drag;
178  newBody.TSAngularDrag = tsRigidBody.angularDrag;
179  } else {
180  newBody.SetMassProperties();
181  }
182 
183  if (isStatic) {
184  newBody.AffectedByGravity = false;
185  newBody.IsStatic = true;
186  }
187 
188  _body = newBody;
189  }
190 
194  public void Initialize() {
195  CreateBody();
196  }
197 
198  private void CheckPhysics() {
199  if (_body == null && PhysicsManager.instance != null) {
200  PhysicsManager.instance.AddBody(this);
201  }
202  }
203 
207  public virtual void OnDrawGizmos() {
208  if (!this.enabled) {
209  return;
210  }
211 
212  Vector3 position = _body != null ? _body.Position.ToVector() : (transform.position + ScaledCenter.ToVector());
213  Quaternion rotation = _body != null ? _body.Orientation.ToQuaternion() : transform.rotation;
214 
215  Gizmos.color = Color.yellow;
216 
217  Matrix4x4 cubeTransform = Matrix4x4.TRS(position, rotation, GetGizmosSize());
218  Matrix4x4 oldGizmosMatrix = Gizmos.matrix;
219 
220  Gizmos.matrix *= cubeTransform;
221 
222  DrawGizmos();
223 
224  Gizmos.matrix = oldGizmosMatrix;
225  }
226 
230  protected abstract Vector3 GetGizmosSize();
231 
235  protected abstract void DrawGizmos();
236 
240  public bool IsBodyInitialized {
241  get {
242  return _body != null;
243  }
244  }
245  }
246 
247 }
Shape Shape
Shape used by a collider.
Definition: TSCollider.cs:20
bool useGravity
If true it uses gravity force.
Definition: TSRigidBody.cs:47
FP drag
Linear drag coeficient.
Definition: TSRigidBody.cs:95
FP mass
Mass of the body.
Definition: TSRigidBody.cs:23
Represents a physical 3D rigid body.
Definition: TSRigidBody.cs:12
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:36
static IPhysicsManager instance
Returns a proper implementation of IPhysicsManager.
bool isKinematic
If true it doesn&#39;t get influences from external forces.
Definition: TSRigidBody.cs:71
Simulates physical properties of a body.
Definition: TSMaterial.cs:9
FP friction
Static friction when in contact.
Definition: TSMaterial.cs:14
Abstract collider for 3D shapes.
Definition: TSCollider.cs:13
TSVector ScaledCenter
Returns a version of collider&#39;s center scaled by parent&#39;s transform.
Definition: TSCollider.cs:80
TSBBox bounds
Returns body&#39;s boundind box.
Definition: TSCollider.cs:105
TSRigidBody attachedRigidbody
Returns the TSRigidBody attached.
Definition: TSCollider.cs:96
TSVector lossyScale
Holds an first value of the GameObject&#39;s lossy scale.
Definition: TSCollider.cs:129
void Awake()
Creates a new TSRigidBody when there is no one attached to this GameObject.
Definition: TSCollider.cs:137
FP angularDrag
Angular drag coeficient.
Definition: TSRigidBody.cs:119
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:194
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:68
Manages physics simulation.
abstract Vector3 GetGizmosSize()
Returns the gizmos size.
bool IsBodyInitialized
Returns true if the body was already initialized.
Definition: TSCollider.cs:240
IBody3D Body
Returns the body linked to this collider.
Definition: TSCollider.cs:114
virtual void OnDrawGizmos()
Do a base matrix transformation to draw correctly all collider gizmos.
Definition: TSCollider.cs:207
TSMaterial tsMaterial
Simulated material.
Definition: TSCollider.cs:56
Generated by   doxygen 1.8.11