TrueSync
TSRigidBody.cs
1 using UnityEngine;
2 using UnityEngine.Serialization;
3 
4 namespace TrueSync {
5 
9  [RequireComponent(typeof(TSCollider))]
10  [AddComponentMenu("TrueSync/Physics/TSRigidBody", 11)]
11  public class TSRigidBody : MonoBehaviour {
12 
13  public enum InterpolateMode { None, Interpolate, Extrapolate };
14 
15  [FormerlySerializedAs("mass")]
16  [SerializeField]
17  private FP _mass = 1;
18 
22  public FP mass {
23  get {
24  if (tsCollider._body != null) {
25  return tsCollider._body.Mass;
26  }
27 
28  return _mass;
29  }
30 
31  set {
32  _mass = value;
33 
34  if (tsCollider._body != null) {
35  tsCollider._body.Mass = value;
36  }
37  }
38  }
39 
40  [SerializeField]
41  private bool _useGravity = true;
42 
46  public bool useGravity {
47  get {
50  }
51 
52  return _useGravity;
53  }
54 
55  set {
56  _useGravity = value;
57 
59  tsCollider.Body.TSAffectedByGravity = _useGravity;
60  }
61  }
62  }
63 
64  [SerializeField]
65  private bool _isKinematic;
66 
70  public bool isKinematic {
71  get {
74  }
75 
76  return _isKinematic;
77  }
78 
79  set {
80  _isKinematic = value;
81 
83  tsCollider.Body.TSIsKinematic = _isKinematic;
84  }
85  }
86  }
87 
91  public InterpolateMode interpolation;
92 
96  [HideInInspector]
97  public bool freezeZAxis;
98 
99  private TSCollider _tsCollider;
100 
105  get {
106  if (_tsCollider == null) {
107  _tsCollider = GetComponent<TSCollider>();
108  }
109 
110  return _tsCollider;
111  }
112  }
113 
114  private TSTransform _tsTransform;
115 
120  get {
121  if (_tsTransform == null) {
122  _tsTransform = GetComponent<TSTransform>();
123  }
124 
125  return _tsTransform;
126  }
127  }
128 
134  public void AddForce(TSVector force) {
135  AddForce(force, ForceMode.Force);
136  }
137 
144  public void AddForce(TSVector force, ForceMode mode) {
145  if (mode == ForceMode.Force) {
147  } else if (mode == ForceMode.Impulse) {
149  }
150  }
151 
158  public void AddForce(TSVector force, TSVector position) {
159  AddForce(force, position, ForceMode.Impulse);
160  }
161 
168  public void AddForce(TSVector force, TSVector position, ForceMode mode) {
169  if (mode == ForceMode.Force) {
170  tsCollider.Body.TSApplyForce(force, position);
171  } else if (mode == ForceMode.Impulse) {
172  tsCollider.Body.TSApplyImpulse(force, position);
173  }
174  }
175 
181  public void AddTorque(TSVector torque) {
182  tsCollider.Body.TSApplyTorque(torque);
183  }
184 
185 
191  public void LookAt(TSVector target) {
193  tsCollider.Body.TSUpdate();
194  }
195 
200  this.position = position;
201  }
202 
206  public void MoveRotation(TSQuaternion rot) {
207  this.rotation = rot;
208  }
209 
214  get {
215  return tsTransform.position;
216  }
217 
218  set {
219  tsTransform.position = value;
220  }
221  }
222 
227  get {
228  return tsTransform.rotation;
229  }
230 
231  set {
232  tsTransform.rotation = value;
233  }
234  }
235 
240  get {
242  }
243 
244  set {
246  }
247  }
248 
253  get {
255  }
256 
257  set {
259  }
260  }
261 
262  }
263 
264 }
bool useGravity
If true it uses gravity force.
Definition: TSRigidBody.cs:46
void MoveRotation(TSQuaternion rot)
Rotates the body to a provided rotation.
Definition: TSRigidBody.cs:206
FP mass
Mass of the body.
Definition: TSRigidBody.cs:22
void AddForce(TSVector force, TSVector position)
Applies the provided force in the body.
Definition: TSRigidBody.cs:158
void TSApplyForce(TSVector force)
Applies a force to the body&#39;s center.
void MovePosition(TSVector position)
Moves the body to a new position.
Definition: TSRigidBody.cs:199
Represents a physical 3D rigid body.
Definition: TSRigidBody.cs:11
void TSApplyImpulse(TSVector force)
Applies a impulse to the body&#39;s center.
A deterministic version of Unity&#39;s Transform component for 3D physics.
Definition: TSTransform.cs:9
static TSMatrix CreateFromLookAt(TSVector position, TSVector target)
Creates a JMatrix representing an orientation from a quaternion.
Definition: TSMatrix.cs:459
InterpolateMode interpolation
Interpolation mode that should be used.
Definition: TSRigidBody.cs:91
A vector structure.
Definition: TSVector.cs:29
TSQuaternion rotation
Property access to rotation.
Definition: TSTransform.cs:50
void AddForce(TSVector force, TSVector position, ForceMode mode)
Applies the provided force in the body.
Definition: TSRigidBody.cs:168
bool isKinematic
If true it doesn&#39;t get influences from external forces.
Definition: TSRigidBody.cs:70
void TSApplyTorque(TSVector force)
Applies a torque force to the body.
Abstract collider for 3D shapes.
Definition: TSCollider.cs:12
TSTransform tsTransform
Returns the TSTransform attached.
Definition: TSRigidBody.cs:119
static TSQuaternion CreateFromMatrix(TSMatrix matrix)
Creates a quaternion from a matrix.
void LookAt(TSVector target)
Changes orientation to look at target position.
Definition: TSRigidBody.cs:191
void AddForce(TSVector force, ForceMode mode)
Applies the provided force in the body.
Definition: TSRigidBody.cs:144
TSVector position
Property access to position.
Definition: TSTransform.cs:23
bool freezeZAxis
If true it freezes Z rotation of the RigidBody (it only appears when in 2D Physics).
Definition: TSRigidBody.cs:97
void AddForce(TSVector force)
Applies the provided force in the body.
Definition: TSRigidBody.cs:134
TSCollider tsCollider
Returns the TSCollider attached.
Definition: TSRigidBody.cs:104
bool TSIsKinematic
If true the body is managed as kinematic.
Definition: IBody3D.cs:32
3x3 Matrix.
Definition: TSMatrix.cs:26
bool IsBodyInitialized
Returns true if the body was already initialized.
Definition: TSCollider.cs:218
bool TSAffectedByGravity
If true the body is affected by gravity.
Definition: IBody3D.cs:25
IBody3D Body
Returns the body linked to this collider.
Definition: TSCollider.cs:94
A Quaternion representing an orientation.
Definition: TSQuaternion.cs:29
TSVector TSLinearVelocity
Set/get body&#39;s linear velocity.
Definition: IBody3D.cs:39
TSVector TSAngularVelocity
Set/get body&#39;s angular velocity.
Definition: IBody3D.cs:46
TSVector position
Position of the body.
Definition: TSRigidBody.cs:213
TSVector angularVelocity
AngularVelocity of the body.
Definition: TSRigidBody.cs:252
TSVector velocity
LinearVelocity of the body.
Definition: TSRigidBody.cs:239
TSQuaternion rotation
Orientation of the body.
Definition: TSRigidBody.cs:226
void AddTorque(TSVector torque)
Simulates the provided tourque in the body.
Definition: TSRigidBody.cs:181