TrueSync: TSRigidBody2D.cs Source File

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 
87  [SerializeField]
88  private FP _linearDrag;
89 
93  public FP drag {
94  get {
95  if (tsCollider.IsBodyInitialized) {
96  return tsCollider.Body.TSLinearDrag;
97  }
98 
99  return _linearDrag;
100  }
101 
102  set {
103  _linearDrag = value;
104 
105  if (tsCollider.IsBodyInitialized) {
106  tsCollider.Body.TSLinearDrag = _linearDrag;
107  }
108  }
109  }
110 
111  [SerializeField]
112  private FP _angularDrag = 0.05f;
113 
117  public FP angularDrag {
118  get {
119  if (tsCollider.IsBodyInitialized) {
120  return tsCollider.Body.TSAngularDrag;
121  }
122 
123  return _angularDrag;
124  }
125 
126  set {
127  _angularDrag = value;
128 
129  if (tsCollider.IsBodyInitialized) {
130  tsCollider.Body.TSAngularDrag = _angularDrag;
131  }
132  }
133  }
134 
138  public InterpolateMode interpolation;
139 
143  public bool freezeZAxis;
144 
145  private TSCollider2D _tsCollider;
146 
147  public TSCollider2D tsCollider {
148  get {
149  if (_tsCollider == null) {
150  _tsCollider = GetComponent<TSCollider2D>();
151  }
152 
153  return _tsCollider;
154  }
155  }
156 
157  private TSTransform2D _tsTransform;
158 
159  private TSTransform2D tsTransform {
160  get {
161  if (_tsTransform == null) {
162  _tsTransform = GetComponent<TSTransform2D>();
163  }
164 
165  return _tsTransform;
166  }
167  }
168 
174  public void AddForce(TSVector2 force) {
175  AddForce(force, ForceMode.Force);
176  }
177 
184  public void AddForce(TSVector2 force, ForceMode mode) {
185  if (mode == ForceMode.Force) {
186  tsCollider.Body.TSApplyForce(force);
187  } else if (mode == ForceMode.Impulse) {
188  tsCollider.Body.TSApplyImpulse(force);
189  }
190  }
191 
198  public void AddForceAtPosition(TSVector2 force, TSVector2 position) {
199  AddForceAtPosition(force, position, ForceMode.Impulse);
200  }
201 
208  public void AddForceAtPosition(TSVector2 force, TSVector2 position, ForceMode mode) {
209  if (mode == ForceMode.Force) {
210  tsCollider.Body.TSApplyForce(force, position);
211  } else if (mode == ForceMode.Impulse) {
212  tsCollider.Body.TSApplyImpulse(force, position);
213  }
214  }
215 
219  public TSVector2 GetPointVelocity(TSVector2 worldPoint) {
220  TSVector directionPoint = (position - tsCollider.Body.TSPosition).ToTSVector();
221  return TSVector.Cross(new TSVector(0, 0, tsCollider.Body.TSAngularVelocity), directionPoint).ToTSVector2() + tsCollider.Body.TSLinearVelocity;
222  }
223 
229  public void AddTorque(TSVector2 torque) {
230  tsCollider.Body.TSApplyTorque(torque);
231  }
232 
236  public void MovePosition(TSVector2 position) {
237  this.position = position;
238  }
239 
243  public void MoveRotation(FP rot) {
244  this.rotation = rot;
245  }
246 
250  public TSVector2 position {
251  get {
252  return tsTransform.position;
253  }
254 
255  set {
256  tsTransform.position = value;
257  }
258  }
259 
263  public FP rotation {
264  get {
265  return tsTransform.rotation;
266  }
267 
268  set {
269  tsTransform.rotation = value;
270  }
271  }
272 
276  public TSVector2 velocity {
277  get {
278  return tsCollider.Body.TSLinearVelocity;
279  }
280 
281  set {
282  tsCollider.Body.TSLinearVelocity = value;
283  }
284  }
285 
289  public FP angularVelocity {
290  get {
291  return tsCollider.Body.TSAngularVelocity;
292  }
293 
294  set {
295  tsCollider.Body.TSAngularVelocity = value;
296  }
297  }
298 
299  }
300 
301 }
TSVector2 position
Position of the body.
void MovePosition(TSVector2 position)
Moves the body to a new position.
void AddForceAtPosition(TSVector2 force, TSVector2 position)
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 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
FP angularDrag
Angular drag coeficient.
bool IsBodyInitialized
Returns true if the body was already initialized.
TSVector2 velocity
LinearVelocity of the body.
TSVector2 position
Property access to position.
TSVector2 GetPointVelocity(TSVector2 worldPoint)
Returns the velocity of the body at some position in world space.
FP rotation
Property access to rotation.
bool useGravity
If true it uses gravity force.
void AddForceAtPosition(TSVector2 force, TSVector2 position, ForceMode mode)
Applies the provided force in the body.
FP drag
Linear drag coeficient.
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.
Represents a physical 2D rigid body.
Abstract collider for 2D shapes.
Definition: TSCollider2D.cs:12
IBody2D Body
Returns RigidBody associated to this TSRigidBody.
Definition: TSCollider2D.cs:70
FP rotation
Orientation of the body.
Generated by   doxygen 1.8.11