Cafu Engine
CompPhysics.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_PHYSICS_HPP_INCLUDED
8 #define CAFU_GAMESYS_COMPONENT_PHYSICS_HPP_INCLUDED
9 
10 #include "CompBase.hpp"
11 #include "btBulletDynamicsCommon.h"
12 
13 
14 namespace cf
15 {
16  namespace GameSys
17  {
18  /// This component includes the body of this entity in the dynamic simulation of physics.
19  ///
20  /// Without this component, the entity is either *static* (it doesn't move at all), *kinematic*
21  /// (it is moved by script or program code), or it doesn't participate in physics computations
22  /// at all.
23  ///
24  /// With this component, the entity's body is subject to gravity, impulses, and generally to
25  /// the dynamic simulation of physics effects in the game world.
26  class ComponentPhysicsT : public ComponentBaseT, public btMotionState
27  {
28  public:
29 
30  /// The constructor.
32 
33  /// The copy constructor.
34  /// @param Comp The component to create a copy of.
36 
37  /// The destructor.
39 
40  float GetMass() const { return m_Mass.Get(); }
41  const btRigidBody* GetRigidBody() const { return m_RigidBody; }
42 
43 
44  // Base class overrides.
45  ComponentPhysicsT* Clone() const;
46  const char* GetName() const { return "Physics"; }
47  void UpdateDependencies(EntityT* Entity);
48 
49 
50  // The TypeSys related declarations for this class.
51  const cf::TypeSys::TypeInfoT* GetType() const { return &TypeInfo; }
52  static void* CreateInstance(const cf::TypeSys::CreateParamsT& Params);
53  static const cf::TypeSys::TypeInfoT TypeInfo;
54 
55 
56  protected:
57 
58  // The Lua API methods of this class.
59  static int ApplyImpulse(lua_State* LuaState);
60  static int SetGravity(lua_State* LuaState);
61  static int toString(lua_State* LuaState);
62 
63  static const luaL_Reg MethodsList[]; ///< The list of Lua methods for this class.
64  static const char* DocClass;
65  static const cf::TypeSys::MethsDocT DocMethods[];
66  static const cf::TypeSys::VarsDocT DocVars[];
67 
68 
69  private:
70 
71  // Implement the btMotionState interface.
72  void getWorldTransform(btTransform& worldTrans) const;
73  void setWorldTransform(const btTransform& worldTrans);
74 
75  TypeSys::VarT<float> m_Mass;
76 
77  btCollisionShape* m_CollisionShape; ///< The collision shape for use with the rigid body.
78  btRigidBody* m_RigidBody; ///< The rigid body for use in the physics world.
79  };
80  }
81 }
82 
83 #endif
ComponentPhysicsT * Clone() const
The virtual copy constructor.
Definition: CompPhysics.cpp:80
void UpdateDependencies(EntityT *Entity)
This method is called whenever something "external" to this component has changed: ...
Definition: CompPhysics.cpp:101
This class represents game entities, which are the basic elements of a world.
Definition: Entity.hpp:53
~ComponentPhysicsT()
The destructor.
Definition: CompPhysics.cpp:64
ComponentPhysicsT()
The constructor.
Definition: CompPhysics.cpp:44
This component includes the body of this entity in the dynamic simulation of physics.
Definition: CompPhysics.hpp:26
static const luaL_Reg MethodsList[]
The list of Lua methods for this class.
Definition: CompPhysics.hpp:63
Definition: TypeSys.hpp:52
Definition: TypeSys.hpp:57
const char * GetName() const
Returns the name of this component.
Definition: CompPhysics.hpp:46
This class keeps type information (about an entity class that occurs in the game).
Definition: TypeSys.hpp:79
const T & Get() const
Returns the value of this variable.
Definition: Variables.hpp:182
Definition: TypeSys.hpp:68
This is the base class for the components that an entity is composed/aggregated of.
Definition: CompBase.hpp:54