TDME2  1.9.200
Rotation.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <tdme/tdme.h>
4 #include <tdme/engine/fwd-tdme.h>
5 #include <tdme/math/Math.h>
6 #include <tdme/math/Quaternion.h>
7 #include <tdme/math/Vector3.h>
8 
9 using tdme::math::Math;
12 
13 /**
14  * Rotation representation
15  * @author Andreas Drewke
16  */
18 {
19 
20 public:
24 
25 private:
27  float angle;
29 
30 public:
31  /**
32  * Public constructor
33  */
34  inline Rotation(): axis(Vector3(0.0f, 0.0f, 0.0f)), angle(0.0f) {
36  }
37 
38  /**
39  * Public constructor
40  * @param axis axis
41  * @param angle angle
42  */
43  inline Rotation(const Vector3& axis, float angle): axis(axis), angle(angle) {
44  update();
45  }
46 
47  /**
48  * Interpolate from given rotation to target rotation taking time passed in seconds and rotation degrees per second into account
49  * @param currentAngle current angle
50  * @param targetAngle target angle
51  * @param timePassedSeconds time passed in seconds
52  * @param degreesPerSeconds deegrees per seconds
53  * @param interpolatedAngle interpolated angle
54  * @return rotation is finished
55  */
56  inline static bool interpolate(float currentAngle, float targetAngle, float timePassedSeconds, float degreesPerSeconds, float& interpolatedAngle) {
57  currentAngle = Math::absmod(currentAngle, 360.0f);
58  targetAngle = Math::absmod(targetAngle, 360.0f);
59  auto targetRotationAngleA = targetAngle;
60  auto targetRotationAngleB = targetAngle - 360.0f;
61  auto targetRotationAngleC = targetAngle + 360.0f;
62  if (Math::abs(targetRotationAngleA - currentAngle) < Math::abs(targetRotationAngleB - currentAngle) &&
63  Math::abs(targetRotationAngleA - currentAngle) < Math::abs(targetRotationAngleC - currentAngle)) {
64  targetAngle = targetRotationAngleA;
65  } else
66  if (Math::abs(targetRotationAngleB - currentAngle) < Math::abs(targetRotationAngleA - currentAngle) &&
67  Math::abs(targetRotationAngleB - currentAngle) < Math::abs(targetRotationAngleC - currentAngle)) {
68  targetAngle = targetRotationAngleB;
69  } else
70  if (Math::abs(targetRotationAngleC - currentAngle) < Math::abs(targetRotationAngleA - currentAngle) &&
71  Math::abs(targetRotationAngleC - currentAngle) < Math::abs(targetRotationAngleB - currentAngle)) {
72  targetAngle = targetRotationAngleC;
73  }
74  if (Math::abs(Math::absmod(currentAngle, 360.0f) - Math::absmod(targetAngle, 360.0f)) < 0.49f) {
75  interpolatedAngle = targetAngle;
76  return true;
77  } else {
78  auto rotationAdd = timePassedSeconds * degreesPerSeconds * Math::sign(targetAngle - currentAngle);
79  if (currentAngle < targetAngle && currentAngle + rotationAdd > targetAngle) {
80  currentAngle = targetAngle;
81  } else
82  if (currentAngle > targetAngle && currentAngle + rotationAdd < targetAngle) {
83  currentAngle = targetAngle;
84  } else {
85  currentAngle+= rotationAdd;
86  }
87  interpolatedAngle = currentAngle;
88  return false;
89  }
90  }
91 
92  /**
93  * @return angle
94  */
95  inline const float getAngle() const {
96  return angle;
97  }
98 
99  /**
100  * @param angle angle
101  */
102  inline void setAngle(const float angle) {
103  this->angle = angle;
104  }
105 
106  /**
107  * @return axis
108  */
109  inline const Vector3& getAxis() const {
110  return axis;
111  }
112 
113  /**
114  * Set axis
115  * @param axis axis
116  */
117  inline void setAxis(const Vector3& axis) {
118  this->axis.set(axis);
119  }
120 
121  /**
122  * @return quaternion
123  */
124  inline const Quaternion& getQuaternion() const {
125  return quaternion;
126  }
127 
128  /**
129  * From rotation
130  */
131  inline void fromRotation(const Rotation& rotation) {
132  this->axis = rotation.axis;
133  this->angle = rotation.angle;
134  this->quaternion = rotation.quaternion;
135  }
136 
137  /**
138  * Returns rotation from a quaternion
139  * @param quaternion quaternion
140  * @return rotation
141  */
142  inline static const Rotation fromQuaternion(const Quaternion& quaternion) {
143  auto q = quaternion.clone().normalize();
144  // compute angle
145  auto angle = 2.0f * Math::acos(q[3]) / 3.1415927f * 180.0f;
146  // compute axis
147  Vector3 axis;
148  auto s = Math::sqrt(1.0f - q[3] * q[3]);
149  if (s < 0.0010f) {
150  axis.set(q[0], q[1], q[2]);
151  } else {
152  axis.set(q[0] / s, q[1] / s, q[2] / s);
153  }
154  //
155  return Rotation(axis, angle);
156  }
157 
158  /**
159  * Computes rotation matrix
160  */
161  inline void update() {
163  }
164 
165 };
Rotation representation.
Definition: Rotation.h:18
static const Rotation fromQuaternion(const Quaternion &quaternion)
Returns rotation from a quaternion.
Definition: Rotation.h:142
void setAngle(const float angle)
Definition: Rotation.h:102
const Quaternion & getQuaternion() const
Definition: Rotation.h:124
void fromRotation(const Rotation &rotation)
From rotation.
Definition: Rotation.h:131
static STATIC_DLL_IMPEXT Vector3 Y_AXIS
Definition: Rotation.h:22
Rotation(const Vector3 &axis, float angle)
Public constructor.
Definition: Rotation.h:43
static bool interpolate(float currentAngle, float targetAngle, float timePassedSeconds, float degreesPerSeconds, float &interpolatedAngle)
Interpolate from given rotation to target rotation taking time passed in seconds and rotation degrees...
Definition: Rotation.h:56
Rotation()
Public constructor.
Definition: Rotation.h:34
void setAxis(const Vector3 &axis)
Set axis.
Definition: Rotation.h:117
const Vector3 & getAxis() const
Definition: Rotation.h:109
void update()
Computes rotation matrix.
Definition: Rotation.h:161
const float getAngle() const
Definition: Rotation.h:95
static STATIC_DLL_IMPEXT Vector3 X_AXIS
Definition: Rotation.h:21
static STATIC_DLL_IMPEXT Vector3 Z_AXIS
Definition: Rotation.h:23
Quaternion quaternion
Definition: Rotation.h:28
Standard math functions.
Definition: Math.h:19
Quaternion class representing quaternion mathematical structure and operations with x,...
Definition: Quaternion.h:24
Quaternion & identity()
Creates identity quaternion.
Definition: Quaternion.h:226
Quaternion & rotate(const Vector3 &axis, float angle)
Creates rotation quaternion.
Definition: Quaternion.h:361
Quaternion clone() const
Clones this quaternion.
Definition: Quaternion.h:427
Quaternion & normalize()
Normalizes this quaternion.
Definition: Quaternion.h:346
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
Vector3 & set(float x, float y, float z)
Sets this vector3 by its components.
Definition: Vector3.h:70
#define STATIC_DLL_IMPEXT
Definition: tdme.h:15