TrueSync
TSTransform.cs
1 using UnityEngine;
2 
3 namespace TrueSync {
4 
8  [ExecuteInEditMode]
9  public class TSTransform : MonoBehaviour {
10 
11  private const float DELTA_TIME_FACTOR = 10f;
12 
13  [SerializeField]
14  [HideInInspector]
15  [AddTracking]
16  private TSVector _position;
17 
23  public TSVector position {
24  get {
25  if (tsCollider != null && tsCollider.Body != null) {
26  return tsCollider.Body.TSPosition - scaledCenter;
27  }
28 
29  return _position;
30  }
31  set {
32  _position = value;
33 
34  if (tsCollider != null && tsCollider.Body != null) {
35  tsCollider.Body.TSPosition = _position + scaledCenter;
36  }
37  }
38  }
39 
40  [SerializeField]
41  [HideInInspector]
42  [AddTracking]
43  private TSQuaternion _rotation;
44 
51  get {
52  if (tsCollider != null && tsCollider.Body != null) {
54  }
55 
56  return _rotation;
57  }
58  set {
59  _rotation = value;
60 
61  if (tsCollider != null && tsCollider.Body != null) {
62  tsCollider.Body.TSOrientation = TSMatrix.CreateFromQuaternion(_rotation);
63  }
64  }
65  }
66 
67  [SerializeField]
68  [HideInInspector]
69  [AddTracking]
70  private TSVector _scale;
71 
75  public TSVector scale {
76  get {
77  return _scale;
78  }
79  set {
80  _scale = value;
81  }
82  }
83 
84  [SerializeField]
85  [HideInInspector]
86  private bool _serialized;
87 
88  private TSVector scaledCenter {
89  get {
90  if (tsCollider != null) {
91  return tsCollider.ScaledCenter;
92  }
93 
94  return TSVector.zero;
95  }
96  }
97 
103  public void LookAt(TSTransform other) {
104  LookAt(other.position);
105  }
106 
112  public void LookAt(TSVector target) {
114  }
115 
119  public void Translate(FP x, FP y, FP z) {
120  Translate(x, y, z, Space.Self);
121  }
122 
128  public void Translate(FP x, FP y, FP z, Space relativeTo) {
129  Translate(new TSVector(x, y, z), relativeTo);
130  }
131 
137  public void Translate(FP x, FP y, FP z, TSTransform relativeTo) {
138  Translate(new TSVector(x, y, z), relativeTo);
139  }
140 
144  public void Translate(TSVector translation) {
145  Translate(translation, Space.Self);
146  }
147 
153  public void Translate(TSVector translation, Space relativeTo) {
154  if (relativeTo == Space.Self) {
155  Translate(translation, this);
156  } else {
157  this.position += translation;
158  }
159  }
160 
166  public void Translate(TSVector translation, TSTransform relativeTo) {
167  this.position += TSVector.Transform(translation, TSMatrix.CreateFromQuaternion(relativeTo.rotation));
168  }
169 
173  public void RotateAround(TSVector point, TSVector axis, FP angle) {
174  TSVector vector = this.position;
175  TSVector vector2 = vector - point;
176  vector2 = TSVector.Transform(vector2, TSMatrix.AngleAxis(angle * FP.Deg2Rad, axis));
177  vector = point + vector2;
178  this.position = vector;
179 
180  Rotate(axis, angle);
181  }
182 
186  public void RotateAround(TSVector axis, FP angle) {
187  Rotate(axis, angle);
188  }
189 
193  public void Rotate(FP xAngle, FP yAngle, FP zAngle) {
194  Rotate(new TSVector(xAngle, yAngle, zAngle), Space.Self);
195  }
196 
202  public void Rotate(FP xAngle, FP yAngle, FP zAngle, Space relativeTo) {
203  Rotate(new TSVector(xAngle, yAngle, zAngle), relativeTo);
204  }
205 
209  public void Rotate(TSVector eulerAngles) {
210  Rotate(eulerAngles, Space.Self);
211  }
212 
216  public void Rotate(TSVector axis, FP angle) {
217  Rotate(axis, angle, Space.Self);
218  }
219 
225  public void Rotate(TSVector axis, FP angle, Space relativeTo) {
226  TSQuaternion result = TSQuaternion.identity;
227 
228  if (relativeTo == Space.Self) {
229  result = this.rotation * TSQuaternion.AngleAxis(angle, axis);
230  } else {
231  result = TSQuaternion.AngleAxis(angle, axis) * this.rotation;
232  }
233 
234  result.Normalize();
235  this.rotation = result;
236  }
237 
243  public void Rotate(TSVector eulerAngles, Space relativeTo) {
244  TSQuaternion result = TSQuaternion.identity;
245 
246  if (relativeTo == Space.Self) {
247  result = this.rotation * TSQuaternion.Euler(eulerAngles);
248  } else {
249  result = TSQuaternion.Euler(eulerAngles) * this.rotation;
250  }
251 
252  result.Normalize();
253  this.rotation = result;
254  }
255 
259  public TSVector forward {
260  get {
261  return TSVector.Transform(TSVector.forward, TSMatrix.CreateFromQuaternion(rotation));
262  }
263  }
264 
268  public TSVector right {
269  get {
270  return TSVector.Transform(TSVector.right, TSMatrix.CreateFromQuaternion(rotation));
271  }
272  }
273 
277  public TSVector up {
278  get {
279  return TSVector.Transform(TSVector.up, TSMatrix.CreateFromQuaternion(rotation));
280  }
281  }
282 
287  get {
288  return rotation.eulerAngles;
289  }
290  }
291 
292  [HideInInspector]
293  public TSCollider tsCollider;
294 
295  [HideInInspector]
296  public TSTransform tsParent;
297 
298  private bool initialized = false;
299 
300  private TSRigidBody rb;
301 
302  public void Start() {
303  if (!Application.isPlaying) {
304  return;
305  }
306 
307  Initialize();
308  rb = GetComponent<TSRigidBody> ();
309  }
310 
314  public void Initialize() {
315  if (initialized) {
316  return;
317  }
318 
319  tsCollider = GetComponent<TSCollider>();
320  if (transform.parent != null) {
321  tsParent = transform.parent.GetComponent<TSTransform>();
322  }
323 
324  if (!_serialized) {
325  UpdateEditMode();
326  }
327 
328  if (tsCollider != null) {
329  if (tsCollider.IsBodyInitialized) {
330  tsCollider.Body.TSPosition = _position + scaledCenter;
331  tsCollider.Body.TSOrientation = TSMatrix.CreateFromQuaternion(_rotation);
332  }
333  } else {
334  StateTracker.AddTracking(this);
335  }
336 
337  initialized = true;
338  }
339 
340  public void Update() {
341  if (Application.isPlaying) {
342  if (initialized) {
343  UpdatePlayMode();
344  }
345  } else {
346  UpdateEditMode();
347  }
348  }
349 
350  private void UpdateEditMode() {
351  if (transform.hasChanged) {
352  _position = transform.position.ToTSVector();
353  _rotation = transform.rotation.ToTSQuaternion();
354  _scale = transform.localScale.ToTSVector();
355 
356  _serialized = true;
357  }
358  }
359 
360  private void UpdatePlayMode() {
361  if (rb != null) {
362  if (rb.interpolation == TSRigidBody.InterpolateMode.Interpolate) {
363  transform.position = Vector3.Lerp(transform.position, position.ToVector(), Time.deltaTime * DELTA_TIME_FACTOR);
364  transform.rotation = Quaternion.Lerp(transform.rotation, rotation.ToQuaternion(), Time.deltaTime * DELTA_TIME_FACTOR);
365  transform.localScale = Vector3.Lerp(transform.localScale, scale.ToVector(), Time.deltaTime * DELTA_TIME_FACTOR);
366  return;
367  } else if (rb.interpolation == TSRigidBody.InterpolateMode.Extrapolate) {
368  transform.position = (position + rb.tsCollider.Body.TSLinearVelocity * Time.deltaTime * DELTA_TIME_FACTOR).ToVector();
369  transform.rotation = Quaternion.Lerp(transform.rotation, rotation.ToQuaternion(), Time.deltaTime * DELTA_TIME_FACTOR);
370  transform.localScale = Vector3.Lerp(transform.localScale, scale.ToVector(), Time.deltaTime * DELTA_TIME_FACTOR);
371  return;
372  }
373  }
374 
375  transform.position = position.ToVector();
376  transform.rotation = rotation.ToQuaternion();
377  transform.localScale = scale.ToVector();
378  }
379 
380  }
381 
382 }
TSVector TSPosition
Set/get body&#39;s position.
Definition: IBody3D.cs:11
void Normalize()
Sets the length of the quaternion to one.
void Rotate(FP xAngle, FP yAngle, FP zAngle, Space relativeTo)
Rotates game object based on provided axis angles of rotation and a relative space.
Definition: TSTransform.cs:202
static TSMatrix AngleAxis(FP angle, TSVector axis)
Creates a matrix which rotates around the given axis by the given angle.
Definition: TSMatrix.cs:635
void Translate(FP x, FP y, FP z, Space relativeTo)
Moves game object based on provided axis values and a relative space.
Definition: TSTransform.cs:128
TSMatrix TSOrientation
Set/get body&#39;s orientation.
Definition: IBody3D.cs:18
void Translate(TSVector translation)
Moves game object based on provided translation vector.
Definition: TSTransform.cs:144
void Rotate(TSVector axis, FP angle)
Rotates game object based on provided axis and angle of rotation.
Definition: TSTransform.cs:216
void Translate(FP x, FP y, FP z)
Moves game object based on provided axis values.
Definition: TSTransform.cs:119
void Translate(FP x, FP y, FP z, TSTransform relativeTo)
Moves game object based on provided axis values and a relative TSTransform.
Definition: TSTransform.cs:137
static readonly TSVector up
A vector with components (0,1,0);
Definition: TSVector.cs:59
Represents a physical 3D rigid body.
Definition: TSRigidBody.cs:11
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
void Rotate(TSVector axis, FP angle, Space relativeTo)
Rotates game object based on provided axis, angle of rotation and relative space. ...
Definition: TSTransform.cs:225
TSQuaternion rotation
Property access to rotation.
Definition: TSTransform.cs:50
static readonly TSVector zero
A vector with components (0,0,0);
Definition: TSVector.cs:47
Abstract collider for 3D shapes.
Definition: TSCollider.cs:12
TSVector ScaledCenter
Returns a version of collider&#39;s center scaled by parent&#39;s transform.
Definition: TSCollider.cs:60
static TSQuaternion CreateFromMatrix(TSMatrix matrix)
Creates a quaternion from a matrix.
TSVector forward
Current self forward vector.
Definition: TSTransform.cs:259
static TSVector Transform(TSVector position, TSMatrix matrix)
Transforms a vector by the given matrix.
Definition: TSVector.cs:369
void LookAt(TSTransform other)
Rotates game object to point forward vector to a target position.
Definition: TSTransform.cs:103
void Rotate(TSVector eulerAngles)
Rotates game object based on provided axis angles of rotation.
Definition: TSTransform.cs:209
void Rotate(FP xAngle, FP yAngle, FP zAngle)
Rotates game object based on provided axis angles of rotation.
Definition: TSTransform.cs:193
void Translate(TSVector translation, TSTransform relativeTo)
Moves game object based on provided translation vector and a relative TSTransform.
Definition: TSTransform.cs:166
static readonly TSVector right
A vector with components (1,0,0);
Definition: TSVector.cs:55
static readonly TSVector forward
A vector with components (0,0,1);
Definition: TSVector.cs:71
TSVector position
Property access to position.
Definition: TSTransform.cs:23
TSVector scale
Property access to scale.
Definition: TSTransform.cs:75
TSVector right
Current self right vector.
Definition: TSTransform.cs:268
void Initialize()
Initializes internal properties based on whether there is a TSCollider attached.
Definition: TSTransform.cs:314
void Translate(TSVector translation, Space relativeTo)
Moves game object based on provided translation vector and a relative space.
Definition: TSTransform.cs:153
void RotateAround(TSVector axis, FP angle)
Rotates game object based on provided axis and angle of rotation.
Definition: TSTransform.cs:186
TSCollider tsCollider
Returns the TSCollider attached.
Definition: TSRigidBody.cs:104
void RotateAround(TSVector point, TSVector axis, FP angle)
Rotates game object based on provided axis, point and angle of rotation.
Definition: TSTransform.cs:173
TSVector up
Current self up vector.
Definition: TSTransform.cs:277
3x3 Matrix.
Definition: TSMatrix.cs:26
bool IsBodyInitialized
Returns true if the body was already initialized.
Definition: TSCollider.cs:218
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
void LookAt(TSVector target)
Rotates game object to point forward vector to a target position.
Definition: TSTransform.cs:112
TSVector eulerAngles
Returns Euler angles in degrees.
Definition: TSTransform.cs:286
void Rotate(TSVector eulerAngles, Space relativeTo)
Rotates game object based on provided axis angles and relative space.
Definition: TSTransform.cs:243