source: trunk/test/physics/easyphysics.h @ 2879

Last change on this file since 2879 was 2879, checked in by touky, 6 years ago

btPhysTest : Added a new test mode called "CAT_MODE". Try it out at your own risks.

  • Property svn:eol-style set to LF
File size: 7.9 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5//            (c) 2009-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
6//   This program is free software; you can redistribute it and/or
7//   modify it under the terms of the Do What The Fuck You Want To
8//   Public License, Version 2, as published by Sam Hocevar. See
9//   http://www.wtfpl.net/ for more details.
10//
11
12//
13// The EasyPhysic class
14// ------------------
15//
16
17#if !defined __EASYPHYSICS_EASYPHYSICS_H__
18#define __EASYPHYSICS_EASYPHYSICS_H__
19
20#ifdef HAVE_PHYS_USE_BULLET
21#include "core.h"
22#include <bullet/btBulletDynamicsCommon.h>
23#include <bullet/btBulletCollisionCommon.h>
24#include <bullet/BulletCollision/CollisionDispatch/btGhostObject.h>
25#endif //HAVE_PHYS_USE_BULLET
26
27namespace lol
28{
29
30namespace phys
31{
32
33class EasyPhysic
34{
35
36    friend class Simulation;
37    friend class EasyConstraint;
38
39#ifdef HAVE_PHYS_USE_BULLET
40
41public:
42    EasyPhysic(WorldEntity* NewOwnerEntity);
43    ~EasyPhysic();
44
45    virtual void SetShapeToBox(lol::vec3& box_size);
46    virtual void SetShapeToSphere(float radius);
47    virtual void SetShapeToCone(float radius, float height);
48    virtual void SetShapeToCylinder(lol::vec3& cyl_size);
49    virtual void SetShapeToCapsule(float radius, float height);
50
51    virtual bool CanChangeCollisionChannel() { return (m_rigid_body == NULL); }
52    virtual mat4 GetTransform();
53    virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f)));
54protected:
55    virtual void BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix);
56public:
57    virtual void SetMass(float mass);
58    virtual float GetMass() { return m_mass; }
59    virtual void SetHitRestitution(float hit_restitution);
60    virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false);
61    virtual void InitBodyToGhost();
62    virtual void AddToSimulation(class Simulation* current_simulation);
63    virtual void RemoveFromSimulation(class Simulation* current_simulation);
64
65    //Force/Impulse functions
66        virtual void AddImpulse(const lol::vec3& impulse);
67    virtual void AddImpulse(const lol::vec3& impulse, const lol::vec3& rel_pos);
68        virtual void AddImpulseTorque(const lol::vec3& torque);
69        virtual void AddForce(const lol::vec3& force);
70        virtual void AddForce(const lol::vec3& force, const lol::vec3& rel_pos);
71    virtual void AddForceTorque(const lol::vec3& torque);
72
73    //Movements getter
74        lol::vec3 GetLinearVelocity() const;
75        lol::vec3 GetLinearForce() const;
76        lol::vec3 GetAngularVelocity() const;
77        lol::vec3 GetAngularForce() const;
78
79protected:
80    virtual void SetLocalInertia(float mass);
81    virtual void SetShapeTo(btCollisionShape* collision_shape);
82
83    virtual btGhostObject* GetGhostObjectInstance();
84
85    btCollisionObject*                          m_collision_object;
86
87    btGhostObject*                              m_ghost_object;
88
89    btRigidBody*                                m_rigid_body;
90    btVector3                                   m_local_inertia;
91
92    btCollisionShape*                           m_collision_shape;
93    btConvexShape*                              m_convex_shape;
94    btMotionState*                              m_motion_state;
95
96#else  // NO PHYSIC IMPLEMENTATION
97
98public:
99    EasyPhysic(WorldEntity* NewOwnerEntity) { m_owner_entity = NewOwnerEntity; }
100
101    virtual void SetShapeToBox(lol::vec3& BoxSize) { }
102    virtual void SetShapeToSphere(float radius) { }
103    virtual void SetShapeToCone(float radius, float height) { }
104    virtual void SetShapeToCylinder(lol::vec3& cyl_size) { }
105    virtual void SetShapeToCapsule(float radius, float height) { }
106
107    virtual bool CanChangeCollisionChannel() { return true; }
108    virtual mat4 GetTransform() { return mat4(1.0f); }
109    virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))) { }
110private:
111    virtual void BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix) { }
112public:
113    virtual void SetMass(float mass) { }
114    virtual float GetMass() { return .0f; }
115    virtual void SetHitRestitution(float hit_restitution) { }
116    virtual void InitBodyToRigid() { }
117    virtual void InitBodyToGhost() { }
118    virtual void AddToSimulation(class Simulation* current_simulation) { }
119    virtual void RemoveFromSimulation(class Simulation* current_simulation) { }
120
121    //Force/Impulse functions
122        virtual void AddImpulse(const lol::vec3& impulse) { }
123    virtual void AddImpulse(const lol::vec3& impulse, const lol::vec3& rel_pos) { }
124        virtual void AddImpulseTorque(const lol::vec3& torque) { }
125        virtual void AddForce(const lol::vec3& force) { }
126        virtual void AddForce(const lol::vec3& force, const lol::vec3& rel_pos) { }
127    virtual void AddForceTorque(const lol::vec3& torque) { }
128
129    //Movements getter
130    lol::vec3 GetLinearVelocity()    const { return lol::vec3(.0f); }
131        lol::vec3 GetLinearForce()       const { return lol::vec3(.0f); }
132        lol::vec3 GetAngularVelocity()   const { return lol::vec3(.0f); }
133        lol::vec3 GetAngularForce()      const { return lol::vec3(.0f); }
134
135    virtual void InitBodyToGhost() { }
136
137#endif // PHYSIC IMPLEMENTATION
138
139public:
140    //Sets the collision Group & Mask.
141    //Mask can change at runtime, not group !
142    virtual bool SetCollisionChannel(int NewGroup, int NewMask)
143    {
144        if (CanChangeCollisionChannel())
145        {
146            m_collision_group = (1 << NewGroup);
147            m_collision_mask = NewMask;
148            return true;
149        }
150        return false;
151    }
152    int GetCollisionGroup() { return m_collision_group; }
153    int GetCollisionMask()    { return m_collision_mask; }
154
155    //Base/Attachment logic
156    virtual void AttachTo(EasyPhysic* NewBase, bool NewBaseLockLocation = true, bool NewBaseLockRotation = true)
157    {
158        if (NewBase == this || (NewBase && NewBase->m_base_physic == this))
159            return;
160
161        if (NewBase)
162        {
163            bool bAlreadyExists = false;
164            for (int i = 0; i < NewBase->m_based_physic_list.Count(); ++i)
165                if (NewBase->m_based_physic_list[i] == this)
166                    bAlreadyExists = true;
167            if (!bAlreadyExists)
168                NewBase->m_based_physic_list << this;
169            m_base_physic = NewBase;
170            m_base_lock_location = NewBaseLockLocation;
171            m_base_lock_rotation = NewBaseLockRotation;
172        }
173        else if (m_base_physic)
174        {
175            for (int i = 0; i < m_base_physic->m_based_physic_list.Count(); ++i)
176                if (m_base_physic->m_based_physic_list[i] == this)
177                    m_base_physic->m_based_physic_list.Remove(i--);
178            m_base_physic = NULL;
179        }
180    }
181
182protected:
183    lol::mat4                                       m_local_to_world;
184    float                                           m_mass;
185    float                                           m_hit_restitution;
186    int                                             m_collision_group;
187    int                                             m_collision_mask;
188    WorldEntity*                                    m_owner_entity;
189    Simulation*                                     m_owner_simulation;
190
191    //Base/Attachment logic
192    Array<EasyPhysic*>                              m_based_physic_list;     //List of objects based on this : this object moves, its based object MoveStep with it.
193    EasyPhysic*                                     m_base_physic;           //Base for this object : The base moves, the object moves with it.
194    bool                                            m_base_lock_location;    //when this is TRUE, location moves with rotation change.
195    bool                                            m_base_lock_rotation;    //when this is TRUE, rotation moves with rotation change.
196
197    //Touch logic
198    Array<EasyPhysic*>                              m_touching_physic;        //Maintained by ghost objects
199};
200
201} /* namespace phys */
202
203} /* namespace lol */
204
205#endif /* __EASYPHYSICS_EASYPHYSICS_H__ */
206
Note: See TracBrowser for help on using the repository browser.