TrueSync: TSRigidBody.cs Source File

TrueSync

TSRigidBody.cs
1 using UnityEngine;
2 using UnityEngine.Serialization;
3 using TrueSync.Physics3D;
4 
5 namespace TrueSync {
6 
10  [RequireComponent(typeof(TSCollider))]
11  [AddComponentMenu("TrueSync/Physics/TSRigidBody", 11)]
12  public class TSRigidBody : MonoBehaviour {
13 
14  public enum InterpolateMode { None, Interpolate, Extrapolate };
15 
16  [FormerlySerializedAs("mass")]
17  [SerializeField]
18  private FP _mass = 1;
19 
23  public FP mass {
24  get {
25  if (tsCollider._body != null) {
26  return tsCollider._body.Mass;
27  }
28 
29  return _mass;
30  }
31 
32  set {
33  _mass = value;
34 
35  if (tsCollider._body != null) {
36  tsCollider._body.Mass = value;
37  }
38  }
39  }
40 
41  [SerializeField]
42  private bool _useGravity = true;
43 
47  public bool useGravity {
48  get {
50  return tsCollider.Body.TSAffectedByGravity;
51  }
52 
53  return _useGravity;
54  }
55 
56  set {
57  _useGravity = value;
58 
60  tsCollider.Body.TSAffectedByGravity = _useGravity;
61  }
62  }
63  }
64 
65  [SerializeField]
66  private bool _isKinematic;
67 
71  public bool isKinematic {
72  get {
74  return tsCollider.Body.TSIsKinematic;
75  }
76 
77  return _isKinematic;
78  }
79 
80  set {
81  _isKinematic = value;
82 
84  tsCollider.Body.TSIsKinematic = _isKinematic;
85  }
86  }
87  }
88 
89  [SerializeField]
90  private FP _linearDrag;
91 
95  public FP drag {
96  get {
98  return tsCollider.Body.TSLinearDrag;
99  }
100 
101  return _linearDrag;
102  }
103 
104  set {
105  _linearDrag = value;
106 
108  tsCollider.Body.TSLinearDrag = _linearDrag;
109  }
110  }
111  }
112 
113  [SerializeField]
114  private FP _angularDrag = 0.05f;
115 
119  public FP angularDrag {
120  get {
122  return tsCollider.Body.TSAngularDrag;
123  }
124 
125  return _angularDrag;
126  }
127 
128  set {
129  _angularDrag = value;
130 
132  tsCollider.Body.TSAngularDrag = _angularDrag;
133  }
134  }
135  }
136 
140  public InterpolateMode interpolation;
141 
142  [SerializeField]
143  [HideInInspector]
144  private TSRigidBodyConstraints _constraints = TSRigidBodyConstraints.None;
145 
149  public TSRigidBodyConstraints constraints {
150  get {
152  return tsCollider._body.FreezeConstraints;
153  }
154 
155  return _constraints;
156  }
157 
158  set {
159  _constraints = value;
160 
162  tsCollider._body.FreezeConstraints = value;
163  }
164  }
165  }
166 
167  private TSCollider _tsCollider;
168 
173  get {
174  if (_tsCollider == null) {
175  _tsCollider = GetComponent<TSCollider>();
176  }
177 
178  return _tsCollider;
179  }
180  }
181 
182  private TSTransform _tsTransform;
183 
188  get {
189  if (_tsTransform == null) {
190  _tsTransform = GetComponent<TSTransform>();
191  }
192 
193  return _tsTransform;
194  }
195  }
196 
202  public void AddForce(TSVector force) {
203  AddForce(force, ForceMode.Force);
204  }
205 
212  public void AddForce(TSVector force, ForceMode mode) {
213  if (mode == ForceMode.Force) {
214  tsCollider.Body.TSApplyForce(force);
215  } else if (mode == ForceMode.Impulse) {
216  tsCollider.Body.TSApplyImpulse(force);
217  }
218  }
219 
226  public void AddForceAtPosition(TSVector force, TSVector position) {
227  AddForceAtPosition(force, position, ForceMode.Impulse);
228  }
229 
236  public void AddForceAtPosition(TSVector force, TSVector position, ForceMode mode) {
237  if (mode == ForceMode.Force) {
238  tsCollider.Body.TSApplyForce(force, position);
239  } else if (mode == ForceMode.Impulse) {
240  tsCollider.Body.TSApplyImpulse(force, position);
241  }
242  }
243 
247  public TSVector GetPointVelocity(TSVector worldPoint) {
248  TSVector directionPoint = position - tsCollider.Body.TSPosition;
249  return TSVector.Cross(tsCollider.Body.TSAngularVelocity, directionPoint) + tsCollider.Body.TSLinearVelocity;
250  }
251 
257  public void AddTorque(TSVector torque) {
258  tsCollider.Body.TSApplyTorque(torque);
259  }
260 
261 
267  public void LookAt(TSVector target) {
268  rotation = TSQuaternion.CreateFromMatrix(TSMatrix.CreateFromLookAt(position, target));
269  }
270 
274  public void MovePosition(TSVector position) {
275  this.position = position;
276  }
277 
281  public void MoveRotation(TSQuaternion rot) {
282  this.rotation = rot;
283  }
284 
288  public TSVector position {
289  get {
290  return tsTransform.position;
291  }
292 
293  set {
294  tsTransform.position = value;
295  }
296  }
297 
301  public TSQuaternion rotation {
302  get {
303  return tsTransform.rotation;
304  }
305 
306  set {
307  tsTransform.rotation = value;
308  }
309  }
310 
314  public TSVector velocity {
315  get {
316  return tsCollider.Body.TSLinearVelocity;
317  }
318 
319  set {
320  tsCollider.Body.TSLinearVelocity = value;
321  }
322  }
323 
327  public TSVector angularVelocity {
328  get {
329  return tsCollider.Body.TSAngularVelocity;
330  }
331 
332  set {
333  tsCollider.Body.TSAngularVelocity = value;
334  }
335  }
336 
337  }
338 
339 }
bool useGravity
If true it uses gravity force.
Definition: TSRigidBody.cs:47
FP drag
Linear drag coeficient.
Definition: TSRigidBody.cs:95
void MoveRotation(TSQuaternion rot)
Rotates the body to a provided rotation.
Definition: TSRigidBody.cs:281
FP mass
Mass of the body.
Definition: TSRigidBody.cs:23
TSVector GetPointVelocity(TSVector worldPoint)
Returns the velocity of the body at some position in world space.
Definition: TSRigidBody.cs:247
void MovePosition(TSVector position)
Moves the body to a new position.
Definition: TSRigidBody.cs:274
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
InterpolateMode interpolation
Interpolation mode that should be used.
Definition: TSRigidBody.cs:140
TSQuaternion rotation
Property access to rotation.
Definition: TSTransform.cs:50
bool isKinematic
If true it doesn&#39;t get influences from external forces.
Definition: TSRigidBody.cs:71
Abstract collider for 3D shapes.
Definition: TSCollider.cs:13
TSRigidBodyConstraints constraints
Freeze constraints applied to this body.
Definition: TSRigidBody.cs:149
TSTransform tsTransform
Returns the TSTransform attached.
Definition: TSRigidBody.cs:187
void LookAt(TSVector target)
Changes orientation to look at target position.
Definition: TSRigidBody.cs:267
void AddForce(TSVector force, ForceMode mode)
Applies the provided force in the body.
Definition: TSRigidBody.cs:212
TSVector position
Property access to position.
Definition: TSTransform.cs:23
FP angularDrag
Angular drag coeficient.
Definition: TSRigidBody.cs:119
void AddForce(TSVector force)
Applies the provided force in the body.
Definition: TSRigidBody.cs:202
TSCollider tsCollider
Returns the TSCollider attached.
Definition: TSRigidBody.cs:172
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
void AddForceAtPosition(TSVector force, TSVector position)
Applies the provided force in the body.
Definition: TSRigidBody.cs:226
TSVector position
Position of the body.
Definition: TSRigidBody.cs:288
TSVector angularVelocity
AngularVelocity of the body.
Definition: TSRigidBody.cs:327
TSVector velocity
LinearVelocity of the body.
Definition: TSRigidBody.cs:314
void AddForceAtPosition(TSVector force, TSVector position, ForceMode mode)
Applies the provided force in the body.
Definition: TSRigidBody.cs:236
TSQuaternion rotation
Orientation of the body.
Definition: TSRigidBody.cs:301
void AddTorque(TSVector torque)
Simulates the provided tourque in the body.
Definition: TSRigidBody.cs:257
Generated by   doxygen 1.8.11