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

Last change on this file since 2816 was 2816, checked in by lolbot, 6 years ago

fixed 542 files out of 2754:

  • removed 0 CR characters
  • removed 0 trailing whitespaces
  • replaced 0 tabs with spaces
  • fixed 542 svn:eol-style properties
  • Property svn:eol-style set to LF
File size: 6.3 KB
RevLine 
[2816]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 void InitBodyToRigid(bool ZeroMassIsKinematic=false);
59    virtual void InitBodyToGhost();
60    virtual void AddToSimulation(class Simulation* current_simulation);
61    virtual void RemoveFromSimulation(class Simulation* current_simulation);
62
63protected:
64    virtual void SetLocalInertia(float mass);
65    virtual void SetShapeTo(btCollisionShape* collision_shape);
66
67    virtual btGhostObject* GetGhostObjectInstance();
68
69    btCollisionObject*                            m_collision_object;
70
71    btGhostObject*                                m_ghost_object;
72
73    btRigidBody*                                m_rigid_body;
74    btVector3                                    m_local_inertia;
75
76    btCollisionShape*                            m_collision_shape;
77    btConvexShape*                                m_convex_shape;
78    btMotionState*                                m_motion_state;
79
80#else  // NO PHYSIC IMPLEMENTATION
81
82public:
83    EasyPhysic(WorldEntity* NewOwnerEntity) { m_owner_entity = NewOwnerEntity; }
84
85    virtual void SetShapeToBox(lol::vec3& BoxSize) { }
86    virtual void SetShapeToSphere(float radius) { }
87    virtual void SetShapeToCone(float radius, float height) { }
88    virtual void SetShapeToCylinder(lol::vec3& cyl_size) { }
89    virtual void SetShapeToCapsule(float radius, float height) { }
90
91    virtual bool CanChangeCollisionChannel() { return true; }
92    virtual mat4 GetTransform() { return mat4(1.0f); }
93    virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))) { }
94private:
95    virtual void BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix) { }
96public:
97    virtual void SetMass(float mass) { }
98    virtual void InitBodyToRigid() { }
99    virtual void InitBodyToGhost() { }
100    virtual void AddToSimulation(class Simulation* current_simulation) { }
101    virtual void RemoveFromSimulation(class Simulation* current_simulation) { }
102
103    virtual void InitBodyToGhost() { }
104
105#endif // PHYSIC IMPLEMENTATION
106
107public:
108    //Sets the collision Group & Mask.
109    //Mask can change at runtime, not group !
110    virtual bool SetCollisionChannel(int NewGroup, int NewMask)
111    {
112        if (CanChangeCollisionChannel())
113        {
114            m_collision_group = (1 << NewGroup);
115            m_collision_mask = NewMask;
116            return true;
117        }
118        return false;
119    }
120    int GetCollisionGroup() { return m_collision_group; }
121    int GetCollisionMask()    { return m_collision_mask; }
122
123    //Base/Attachment logic
124    virtual void AttachTo(EasyPhysic* NewBase, bool NewBaseLockLocation = true, bool NewBaseLockRotation = true)
125    {
126        if (NewBase == this || (NewBase && NewBase->m_base_physic == this))
127            return;
128
129        if (NewBase)
130        {
131            bool bAlreadyExists = false;
132            for (int i = 0; i < NewBase->m_based_physic_list.Count(); ++i)
133                if (NewBase->m_based_physic_list[i] == this)
134                    bAlreadyExists = true;
135            if (!bAlreadyExists)
136                NewBase->m_based_physic_list << this;
137            m_base_physic = NewBase;
138            m_base_lock_location = NewBaseLockLocation;
139            m_base_lock_rotation = NewBaseLockRotation;
140        }
141        else if (m_base_physic)
142        {
143            for (int i = 0; i < m_base_physic->m_based_physic_list.Count(); ++i)
144                if (m_base_physic->m_based_physic_list[i] == this)
145                    m_base_physic->m_based_physic_list.Remove(i--);
146            m_base_physic = NULL;
147        }
148    }
149
150protected:
151    lol::mat4                                    m_local_to_world;
152    float                                        m_mass;
153    int                                            m_collision_group;
154    int                                            m_collision_mask;
155    WorldEntity*                                m_owner_entity;
156    Simulation*                                    m_owner_simulation;
157
158    //Base/Attachment logic
159    Array<EasyPhysic*>                            m_based_physic_list;    //List of objects based on this : this object moves, its based object MoveStep with it.
160    EasyPhysic*                                    m_base_physic;            //Base for this object : The base moves, the object moves with it.
161    bool                                        m_base_lock_location;    //when this is TRUE, location moves with rotation change.
162    bool                                        m_base_lock_rotation;    //when this is TRUE, rotation moves with rotation change.
163
164    //Touch logic
165    Array<EasyPhysic*>                            m_touching_physic;        //Maintained by ghost objects
166};
167
168} /* namespace phys */
169
170} /* namespace lol */
171
172#endif /* __EASYPHYSICS_EASYPHYSICS_H__ */
173
Note: See TracBrowser for help on using the repository browser.