Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
Transform.cpp
1/*
2Copyright (C) 2014-2019,2021,2025 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
18#include "aeongames/AABB.hpp"
21
22namespace AeonGames
23{
24 Transform::Transform()
25 = default;
26
27 Transform::Transform ( const Vector3 & aScale, const Quaternion & aRotation, const Vector3 & aTranslation ) :
28 mScale{ aScale }, mRotation{ aRotation }, mTranslation{aTranslation}
29 {
30 }
31
32 Transform::Transform ( const Vector3 & aScale, const Vector3 & aRotation, const Vector3 & aTranslation ) :
33 mScale{ aScale }, mRotation{ Quaternion::GetFromEuler ( aRotation ) }, mTranslation{aTranslation}
34 {
35 }
36
37 Transform::Transform ( const float* aData ) :
38 mScale{ aData }, mRotation{ aData + 3 }, mTranslation{aData + 7}
39 {
40 }
41
42 void Transform::Get ( float* aData ) const
43 {
44 mScale.Get ( aData );
45 mRotation.Get ( aData + 3 );
46 mTranslation.Get ( aData + 7 );
47 }
48
50 {
51 return mScale;
52 }
54 {
55 return mRotation;
56 }
58 {
59 return mTranslation;
60 }
61
62 void Transform::SetScale ( const Vector3& aScale )
63 {
64 mScale = aScale;
65 }
66
67 void Transform::SetRotation ( const Quaternion& aRotation )
68 {
69 mRotation = aRotation;
70 }
71
72 void Transform::SetTranslation ( const Vector3& aTranslation )
73 {
74 mTranslation = aTranslation;
75 }
76
77 void Transform::RotateObjectSpace ( float angle, float x, float y, float z )
78 {
79 mRotation *= Quaternion::GetFromAxisAngle ( angle, x, y, z );
80 }
81
82 void Transform::RotateInertialSpace ( float angle, float x, float y, float z )
83 {
85 }
86
87 void Transform::Move ( float x, float y, float z )
88 {
89 mTranslation[0] += x;
90 mTranslation[1] += y;
91 mTranslation[2] += z;
92 }
93
95 {
96 mRotation[0] = 1.0f;
97 mRotation[1] = 0.0f;
98 mRotation[2] = 0.0f;
99 mRotation[3] = 0.0f;
100 }
101
103 {
104 Matrix4x4 rotation = mRotation.GetMatrix4x4();
105 return Matrix4x4
106 {
107 // Simplified 3x3 scale matrix multiplication
108 rotation[0] * mScale[0],
109 rotation[1] * mScale[0],
110 rotation[2] * mScale[0],
111 0,
112
113 rotation[4] * mScale[1],
114 rotation[5] * mScale[1],
115 rotation[6] * mScale[1],
116 0,
117
118 rotation[8] * mScale[2],
119 rotation[9] * mScale[2],
120 rotation[10] * mScale[2],
121 0,
122 // Simplified translation multiplication
123 mTranslation[0],
124 mTranslation[1],
125 mTranslation[2],
126 1,
127 };
128 }
129
131 {
132 Matrix3x3 rotation = mRotation.GetMatrix3x3();
133 return Matrix3x3
134 {
135 // Simplified 3x3 scale matrix multiplication
136 rotation[0] * mScale[0],
137 rotation[1] * mScale[0],
138 rotation[2] * mScale[0],
139
140 rotation[3] * mScale[1],
141 rotation[4] * mScale[1],
142 rotation[5] * mScale[1],
143
144 rotation[6] * mScale[2],
145 rotation[7] * mScale[2],
146 rotation[8] * mScale[2],
147 };
148 }
149
151 {
152 return GetInverted().GetMatrix();
153 }
154
155 void Transform::MoveInObjectSpace ( float x, float y, float z )
156 {
157 mTranslation += mRotation * Vector3 ( x, y, z );
158 }
159
160 Transform& Transform::Invert()
161 {
162 mScale[0] = 1.0f / mScale[0];
163 mScale[1] = 1.0f / mScale[1];
164 mScale[2] = 1.0f / mScale[2];
165 //mRotation[0] = mRotation[0]; // Stays the same
166 mRotation[1] = -mRotation[1];
167 mRotation[2] = -mRotation[2];
168 mRotation[3] = -mRotation[3];
170 return *this;
171 }
172
173 const Transform Transform::GetInverted() const
174 {
175 return Transform ( *this ).Invert();
176 }
177
178 Transform& Transform::operator *= ( const Transform& rhs )
179 {
180 // Item 22 from MEC++
181 // Translation
183 // Rotation
184 mRotation *= rhs.GetRotation();
185 // Scale
186 mScale *= rhs.GetScale();
187 return *this;
188 }
189
190 const Transform operator* ( const Transform& lhs, const Transform& rhs )
191 {
192 /* Here Transform(lhs) *MAY* mean cast away constness
193 rather tan create a temporary object in some compilers,
194 we want the temporary, NOT the cast.*/
195 return Transform ( lhs ) *= rhs;
196 }
197
198 const AABB operator* ( const Transform & lhs, const AABB & rhs )
199 {
201 Matrix3x3 scale_rotation{lhs.GetScaleRotationMatrix() };
202 return AABB
203 {
204 lhs.GetTranslation() + scale_rotation * rhs.GetCenter(),
205 Abs ( scale_rotation ) * rhs.GetRadii()
206 };
207 }
208
209 const bool operator== ( const Transform& lhs, const Transform& rhs )
210 {
211 return
212 lhs.GetScale() == rhs.GetScale() &&
213 lhs.GetRotation() == rhs.GetRotation() &&
214 lhs.GetTranslation() == rhs.GetTranslation();
215 }
216 const Transform Interpolate ( const Transform & aTransform0, const Transform & aTransform1, const Transform & aTransform2, const Transform & aTransform3, double aInterpolation )
217 {
218 return Transform
219 {
220 Spline ( aTransform0.GetScale(), aTransform1.GetScale(), aTransform2.GetScale(), aTransform3.GetScale(), aInterpolation ),
221 NlerpQuats ( aTransform1.GetRotation(), aTransform1.GetRotation(), aInterpolation ),
222 Spline ( aTransform0.GetTranslation(), aTransform1.GetTranslation(), aTransform2.GetTranslation(), aTransform3.GetTranslation(), aInterpolation )
223 };
224 }
225}
Header for the axis aligned bounding box class.
Header for 3x3 matrix class.
Header for 4x4 matrix class.
Header for Transform component class.
Axis Aligned Bounding Box class.
Definition AABB.hpp:34
DLL const Vector3 & GetRadii() const
Get the radii (half-extents) of the AABB.
Definition AABB.cpp:33
DLL const Vector3 & GetCenter() const
Get the center position of the AABB.
Definition AABB.cpp:29
3 by 3 matrix in colum mayor order.
Definition Matrix3x3.hpp:33
4 by 4 matrix in colum mayor order.
Definition Matrix4x4.hpp:35
Quaternion class.
static DLL const Quaternion GetFromAxisAngle(float angle, float x, float y, float z)
Create a quaternion from an axis-angle representation.
Component class for any object that requires space transformations.
Definition Transform.hpp:34
DLL void SetRotation(const Quaternion &aRotation)
Set the rotation quaternion.
Definition Transform.cpp:67
DLL const Vector3 & GetScale() const
Get the scale vector.
Definition Transform.cpp:49
DLL void ResetRotation()
Clear rotation.
Definition Transform.cpp:94
DLL const Vector3 & GetTranslation() const
Get the translation vector.
Definition Transform.cpp:57
DLL const Matrix4x4 GetInvertedMatrix() const
Constructs an inverted transformation matrix from the SRT variables.
DLL Transform & Invert()
Invert this transform in place.
DLL void Move(float x, float y, float z)
Adds the provided vector to the position vector.
Definition Transform.cpp:87
DLL const Matrix4x4 GetMatrix() const
Constructs a transformation matrix from the SRT variables.
Vector3 mTranslation
Translation vector.
DLL const Transform GetInverted() const
Get the inverted transform.
Vector3 mScale
Scale rotation and translation.
DLL void SetScale(const Vector3 &aScale)
Set transform scale vector.
Definition Transform.cpp:62
DLL const Matrix3x3 GetScaleRotationMatrix() const
Constructs a transformation matrix from only the SR variables.
DLL const Quaternion & GetRotation() const
Get the rotation quaternion.
Definition Transform.cpp:53
DLL void Get(float *aData) const
Copy the transform data to a float array.
Definition Transform.cpp:42
DLL void MoveInObjectSpace(float x, float y, float z)
Moves the transfrom relative to its own axes.
Quaternion mRotation
Rotation quaternion.
DLL void RotateObjectSpace(float angle, float x, float y, float z)
Rotate in Object Space.
Definition Transform.cpp:77
DLL void RotateInertialSpace(float angle, float x, float y, float z)
Rotate in Inertial Space.
Definition Transform.cpp:82
DLL Transform & operator*=(const Transform &lhs)
Combine this transform with another transform.
DLL void SetTranslation(const Vector3 &aTranslation)
Set the translation vector.
Definition Transform.cpp:72
3D vector class.
Definition Vector3.hpp:32
<- This is here just for the literals
Definition AABB.hpp:31
DLL const Vector3 Spline(const Vector3 &p0, const Vector3 &p1, const Vector3 &p2, const Vector3 &p3, double interpolation)
Catmull-Rom spline interpolation between four control points.
Definition Vector3.cpp:275
DLL const Matrix3x3 operator*(const Matrix3x3 &lhs, const Matrix3x3 &rhs)
Multiplies two 3x3 matrices. Multiplies two 3x3 matrices.
DLL const Quaternion NlerpQuats(const Quaternion &q1, const Quaternion &q2, double interpolation)
Linearly interpolate between two quaternions return the normalized result.
DLL const Transform Interpolate(const Transform &aTransform0, const Transform &aTransform1, const Transform &aTransform2, const Transform &aTransform3, double aInterpolation)
Interpolate transforms using spline and mlerp methods.
DLL const Matrix3x3 Abs(const Matrix3x3 &aMatrix3x3)
Compute the element-wise absolute value of a matrix.
DLL const bool operator==(const Matrix3x3 &lhs, const Matrix3x3 &rhs)
Compare two 3x3 matrices for equality.