TrueSync
TSRigidBody2D.cs
1 using UnityEngine;
2 using UnityEngine.Serialization;
3 
4 namespace TrueSync {
8  [RequireComponent(typeof(TSCollider2D))]
9  [AddComponentMenu("TrueSync/Physics/TSRigidBody2D", 11)]
10  public class TSRigidBody2D : MonoBehaviour {
11 
12  public enum InterpolateMode { None, Interpolate, Extrapolate };
13 
14  [FormerlySerializedAs("mass")]
15  [SerializeField]
16  private FP _mass = 1;
17 
21  public FP mass {
22  get {
23  if (tsCollider._body != null) {
24  return tsCollider._body.Mass;
25  }
26 
27  return _mass;
28  }
29 
30  set {
31  _mass = value;
32 
33  if (tsCollider._body != null) {
34  tsCollider._body.Mass = value;
35  }
36  }
37  }
38 
39  [SerializeField]
40  private bool _useGravity = true;
41 
45  public bool useGravity {
46  get {
47  if (tsCollider.IsBodyInitialized) {
48  return tsCollider.Body.TSAffectedByGravity;
49  }
50 
51  return _useGravity;
52  }
53 
54  set {
55  _useGravity = value;
56 
57  if (tsCollider.IsBodyInitialized) {
58  tsCollider.Body.TSAffectedByGravity = _useGravity;
59  }
60  }
61  }
62 
63  [SerializeField]
64  private bool _isKinematic;
65 
69  public bool isKinematic {
70  get {
71  if (tsCollider.IsBodyInitialized) {
72  return tsCollider.Body.TSIsKinematic;
73  }
74 
75  return _isKinematic;
76  }
77 
78  set {
79  _isKinematic = value;
80 
81  if (tsCollider.IsBodyInitialized) {
82  tsCollider.Body.TSIsKinematic = _isKinematic;
83  }
84  }
85  }
86 
90  public InterpolateMode interpolation;
91 
95  public bool freezeZAxis;
96 
97  private TSCollider2D _tsCollider;
98 
99  public TSCollider2D tsCollider {
100  get {
101  if (_tsCollider == null) {
102  _tsCollider = GetComponent<TSCollider2D>();
103  }
104 
105  return _tsCollider;
106  }
107  }
108 
109  private TSTransform2D _tsTransform;
110 
111  private TSTransform2D tsTransform {
112  get {
113  if (_tsTransform == null) {
114  _tsTransform = GetComponent<TSTransform2D>();
115  }
116 
117  return _tsTransform;
118  }
119  }
120 
126  public void AddForce(TSVector2 force) {
127  AddForce(force, ForceMode.Force);
128  }
129 
136  public void AddForce(TSVector2 force, ForceMode mode) {
137  if (mode == ForceMode.Force) {
138  tsCollider.Body.TSApplyForce(force);
139  } else if (mode == ForceMode.Impulse) {
140  tsCollider.Body.TSApplyImpulse(force);
141  }
142  }
143 
150  public void AddForce(TSVector2 force, TSVector2 position) {
151  AddForce(force, position, ForceMode.Impulse);
152  }
153 
160  public void AddForce(TSVector2 force, TSVector2 position, ForceMode mode) {
161  if (mode == ForceMode.Force) {
162  tsCollider.Body.TSApplyForce(force, position);
163  } else if (mode == ForceMode.Impulse) {
164  tsCollider.Body.TSApplyImpulse(force, position);
165  }
166  }
167 
173  public void AddTorque(TSVector2 torque) {
174  tsCollider.Body.TSApplyTorque(torque);
175  }
176 
180  public void MovePosition(TSVector2 position) {
181  this.position = position;
182  }
183 
187  public void MoveRotation(FP rot) {
188  this.rotation = rot;
189  }
190 
194  public TSVector2 position {
195  get {
196  return tsTransform.position;
197  }
198 
199  set {
200  tsTransform.position = value;
201  }
202  }
203 
207  public FP rotation {
208  get {
209  return tsTransform.rotation;
210  }
211 
212  set {
213  tsTransform.rotation = value;
214  }
215  }
216 
220  public TSVector2 velocity {
221  get {
222  return tsCollider.Body.TSLinearVelocity;
223  }
224 
225  set {
226  tsCollider.Body.TSLinearVelocity = value;
227  }
228  }
229 
233  public FP angularVelocity {
234  get {
235  return tsCollider.Body.TSAngularVelocity;
236  }
237 
238  set {
239  tsCollider.Body.TSAngularVelocity = value;
240  }
241  }
242 
243  }
244 
245 }
TSVector2 position
Position of the body.
void MovePosition(TSVector2 position)
Moves the body to a new position.
FP TSAngularVelocity
Set/get body&#39;s angular velocity. (radians/s).
Definition: IBody2D.cs:46
void AddForce(TSVector2 force, TSVector2 position, ForceMode mode)
Applies the provided force in the body.
FP mass
Mass of the body.
InterpolateMode interpolation
Interpolation mode that should be used.
void MoveRotation(FP rot)
Rotates the body to a provided rotation.
void AddTorque(TSVector2 torque)
Simulates the provided tourque in the body.
void TSApplyTorque(TSVector2 force)
Applies a torque force to the body.
void AddForce(TSVector2 force)
Applies the provided force in the body.
A deterministic version of Unity&#39;s Transform component for 2D physics.
Definition: TSTransform2D.cs:9
bool IsBodyInitialized
Returns true if the body was already initialized.
TSVector2 velocity
LinearVelocity of the body.
TSVector2 position
Property access to position.
bool TSAffectedByGravity
If true the body is affected by gravity.
Definition: IBody2D.cs:25
void AddForce(TSVector2 force, TSVector2 position)
Applies the provided force in the body.
FP rotation
Property access to rotation.
bool useGravity
If true it uses gravity force.
FP angularVelocity
AngularVelocity of the body (radians/s).
void AddForce(TSVector2 force, ForceMode mode)
Applies the provided force in the body.
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.
bool TSIsKinematic
If true the body is managed as kinematic.
Definition: IBody2D.cs:32
Represents a physical 2D rigid body.
Abstract collider for 2D shapes.
Definition: TSCollider2D.cs:11
IBody2D Body
Returns RigidBody associated to this TSRigidBody.
Definition: TSCollider2D.cs:47
FP rotation
Orientation of the body.
TSVector2 TSLinearVelocity
Set/get body&#39;s linear velocity.
Definition: IBody2D.cs:39
void TSApplyForce(TSVector2 force)
Applies a force to the body&#39;s center.
void TSApplyImpulse(TSVector2 force)
Applies a impulse to the body&#39;s center.