Cafu Engine
CompTransform.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_GAMESYS_COMPONENT_TRANSFORM_HPP_INCLUDED
8 #define CAFU_GAMESYS_COMPONENT_TRANSFORM_HPP_INCLUDED
9 
10 #include "CompBase.hpp"
11 #include "Math3D/Matrix.hpp"
12 #include "Math3D/Quaternion.hpp"
13 
14 
15 namespace cf
16 {
17  namespace GameSys
18  {
19  /// This component adds information about the position and orientation of its entity.
20  /// Positions and orientations can be measured relative to several distinct spaces:
21  ///
22  /// world-space
23  /// : The global and "absolute" coordinate space that also exists when nothing else does.
24  ///
25  /// entity-space
26  /// : The local coordinate system of the entity. It is defined by the entity's transform component relative
27  /// to the entity's parent-space. The term "model-space" can be used synonymously with "entity-space".
28  ///
29  /// parent-space
30  /// : The entity-space of an entity's parent.
31  /// If an entity has no parent entity, this is the same as world-space.
32  ///
33  /// Although transform components can theoretically and technically exist without being attached to an entity,
34  /// in practice this distinction is not made. Every entity has exactly one built-in transform component, and
35  /// terms like "the origin of the transform" and "the origin of the entity" are used synonymously.
37  {
38  public:
39 
40  /// The constructor.
42 
43  /// The copy constructor.
44  /// @param Comp The component to create a copy of.
46 
47  /// Returns whether the transformation described by this component is the identity ("no") transform.
48  bool IsIdentity() const { return m_Origin.Get() == Vector3fT() && m_Quat.Get() == Vector3fT(); }
49 
50  /// Returns the origin of the transform (in "parent-space").
51  const Vector3fT& GetOriginPS() const { return m_Origin.Get(); }
52 
53  /// Sets the origin of the transform (in "parent-space").
54  void SetOriginPS(const Vector3fT& Origin) { m_Origin.Set(Origin); }
55 
56  /// Returns the orientation of the transform (in "parent-space").
57  const cf::math::QuaternionfT GetQuatPS() const { return cf::math::QuaternionfT::FromXYZ(m_Quat.Get()); }
58 
59  /// Sets the orientation of the transform (in "parent-space").
60  void SetQuatPS(const cf::math::QuaternionfT& Quat) { m_Quat.Set(Quat.GetXYZ()); }
61 
62  /// Returns the origin of the transform (in world-space).
63  Vector3fT GetOriginWS() const;
64 
65  /// Sets the origin of the transform (in world-space).
66  void SetOriginWS(const Vector3fT& OriginWS);
67 
68  /// Returns the orientation of the transform (in world-space).
69  const cf::math::QuaternionfT GetQuatWS() const;
70 
71  /// Sets the orientation of the transform (in world-space).
72  void SetQuatWS(const cf::math::QuaternionfT& QuatWS);
73 
74  /// Returns the transformation matrix from local entity-space to world-space.
75  MatrixT GetEntityToWorld() const;
76 
77  /// Sets the orientation of the transform so that it "looks at" the given position.
78  /// The new orientation is chosen such that the bank angle is always 0 relative to the xy-plane.
79  /// @param Pos The target position to look at.
80  /// @param AxisNr The "look axis", i.e. the number of the axis to orient towards `Pos`:
81  /// 0 for the x-axis, 1 for the y-axis.
82  /// @param NoPitch If `true`, the pitch angle is kept at 0, and the given axis points towards `Pos`
83  /// only in the XY-Plane and the z-axis points straight up (0, 0, 1).
84  void LookAt(const Vector3fT& Pos, unsigned int AxisNr = 0, bool NoPitch = false);
85 
86 
87  // Base class overrides.
88  ComponentTransformT* Clone() const;
89  const char* GetName() const { return "Transform"; }
90 
91 
92  // The TypeSys related declarations for this class.
93  const cf::TypeSys::TypeInfoT* GetType() const { return &TypeInfo; }
94  static void* CreateInstance(const cf::TypeSys::CreateParamsT& Params);
95  static const cf::TypeSys::TypeInfoT TypeInfo;
96 
97 
98  protected:
99 
100  // The Lua API methods of this class.
101  static int GetOriginWS(lua_State* LuaState);
102  static int SetOriginWS(lua_State* LuaState);
103  static int GetAngles(lua_State* LuaState);
104  static int SetAngles(lua_State* LuaState);
105  static int GetAxisX(lua_State* LuaState);
106  static int GetAxisY(lua_State* LuaState);
107  static int GetAxisZ(lua_State* LuaState);
108  static int LookAt(lua_State* LuaState);
109  static int toString(lua_State* LuaState);
110 
111  static const luaL_Reg MethodsList[]; ///< The list of Lua methods for this class.
112  static const char* DocClass;
113  static const cf::TypeSys::MethsDocT DocMethods[];
114  static const cf::TypeSys::VarsDocT DocVars[];
115 
116 
117  private:
118 
119  TypeSys::VarT<Vector3fT> m_Origin; ///< The origin of the transform (in "parent-space").
120  TypeSys::VarT<Vector3fT> m_Quat; ///< The orientation of the transform (in "parent-space"), kept as the first three components (x, y, z) of a unit quaternion.
121  };
122  }
123 }
124 
125 #endif
static QuaternionT FromXYZ(const Vector3T< float > &Vec)
Constructs a quaternion from the first three components (x, y, z) of a unit quaternion.
Definition: Quaternion.hpp:61
static const luaL_Reg MethodsList[]
The list of Lua methods for this class.
Definition: CompTransform.hpp:111
Vector3fT GetOriginWS() const
Returns the origin of the transform (in world-space).
Definition: CompTransform.cpp:79
const cf::math::QuaternionfT GetQuatWS() const
Returns the orientation of the transform (in world-space).
Definition: CompTransform.cpp:132
ComponentTransformT * Clone() const
The virtual copy constructor.
Definition: CompTransform.cpp:270
void SetOriginPS(const Vector3fT &Origin)
Sets the origin of the transform (in "parent-space").
Definition: CompTransform.hpp:54
This is a "wrapper" around a normal C++ variable.
Definition: SetCompVar.hpp:15
This class represents a polymorphic 3-dimensional vector.
Definition: Misc.hpp:11
bool IsIdentity() const
Returns whether the transformation described by this component is the identity ("no") transform...
Definition: CompTransform.hpp:48
MatrixT GetEntityToWorld() const
Returns the transformation matrix from local entity-space to world-space.
Definition: CompTransform.cpp:179
void SetOriginWS(const Vector3fT &OriginWS)
Sets the origin of the transform (in world-space).
Definition: CompTransform.cpp:102
const cf::math::QuaternionfT GetQuatPS() const
Returns the orientation of the transform (in "parent-space").
Definition: CompTransform.hpp:57
Vector3T< T > GetXYZ() const
Returns the x, y and z components as a Vector3T<T>.
Definition: Quaternion.hpp:81
void LookAt(const Vector3fT &Pos, unsigned int AxisNr=0, bool NoPitch=false)
Sets the orientation of the transform so that it "looks at" the given position.
Definition: CompTransform.cpp:194
This component adds information about the position and orientation of its entity. ...
Definition: CompTransform.hpp:36
void SetQuatPS(const cf::math::QuaternionfT &Quat)
Sets the orientation of the transform (in "parent-space").
Definition: CompTransform.hpp:60
ComponentTransformT()
The constructor.
Definition: CompTransform.cpp:59
void SetQuatWS(const cf::math::QuaternionfT &QuatWS)
Sets the orientation of the transform (in world-space).
Definition: CompTransform.cpp:148
const Vector3fT & GetOriginPS() const
Returns the origin of the transform (in "parent-space").
Definition: CompTransform.hpp:51
Definition: TypeSys.hpp:52
Definition: TypeSys.hpp:57
This class keeps type information (about an entity class that occurs in the game).
Definition: TypeSys.hpp:79
const char * GetName() const
Returns the name of this component.
Definition: CompTransform.hpp:89
Definition: TypeSys.hpp:68
This is the base class for the components that an entity is composed/aggregated of.
Definition: CompBase.hpp:54