source: trunk/test/Physics/Src/EasyPhysics.cpp @ 1768

Last change on this file since 1768 was 1768, checked in by touky, 8 years ago

Added Attachment/base logic with working attachment with EasyCharacterController.
Added RayCastResult & a naïve integration for EasyCharacterController.
Enjoy !

File size: 9.5 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
5//            (c) 2009-2012 Cédric Lecacheur <jordx@free.fr>
6//            (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com>
7//   This program is free software; you can redistribute it and/or
8//   modify it under the terms of the Do What The Fuck You Want To
9//   Public License, Version 2, as published by Sam Hocevar. See
10//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
11//
12
13#if defined HAVE_CONFIG_H
14#   include "config.h"
15#endif
16
17#include "../Include/LolBtPhysicsIntegration.h"
18#include "../Include/LolPhysics.h"
19
20namespace lol
21{
22
23namespace phys
24{
25
26#ifdef HAVE_PHYS_USE_BULLET
27
28//-------------------------------------------------------------------------
29//EASY_PHYSIC
30//--
31
32EasyPhysic::EasyPhysic(WorldEntity* NewOwnerEntity) :
33        m_collision_object(NULL),
34        m_ghost_object(NULL),
35        m_rigid_body(NULL),
36        m_local_inertia(btVector3(.0f, .0f, .0f)),
37        m_collision_shape(NULL),
38        m_convex_shape(NULL),
39        m_motion_state(NULL),
40        m_mass(.0f),
41        m_collision_group(1),
42        m_collision_mask(1),
43        m_owner_entity(NewOwnerEntity),
44        m_base_physic(NULL)
45{
46}
47
48EasyPhysic::~EasyPhysic()
49{
50        m_rigid_body = NULL;
51        delete m_collision_object;
52        delete m_collision_shape;
53        delete m_motion_state;
54}
55
56//-------------------------------------------------------------------------
57//Set Shape functions
58//--
59
60void EasyPhysic::SetShapeTo(btCollisionShape* collision_shape)
61{
62        bool bReinitToRigidBody = false;
63        if (m_rigid_body)
64        {
65                bReinitToRigidBody = true;
66                delete m_rigid_body;
67        }
68        if (m_collision_shape)
69                delete m_collision_shape;
70
71        m_collision_shape = collision_shape;
72
73        if (bReinitToRigidBody)
74                InitBodyToRigid();
75}
76
77//Box Shape support
78void EasyPhysic::SetShapeToBox(lol::vec3& box_size)
79{
80        vec3 new_box_size = box_size * LOL2BT_UNIT * LOL2BT_SIZE;
81        m_convex_shape = new btBoxShape(LOL2BT_VEC3(new_box_size));
82        SetShapeTo(m_convex_shape);
83}
84
85void EasyPhysic::SetShapeToSphere(float radius)
86{
87        m_convex_shape = new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE);
88        SetShapeTo(m_convex_shape);
89}
90
91void EasyPhysic::SetShapeToCone(float radius, float height)
92{
93        m_convex_shape = new btConeShape(       radius * LOL2BT_UNIT,
94                                                                                height * LOL2BT_UNIT);
95        SetShapeTo(m_convex_shape);
96}
97
98void EasyPhysic::SetShapeToCylinder(lol::vec3& cyl_size)
99{
100        vec3 new_cyl_size = cyl_size * LOL2BT_UNIT;
101        new_cyl_size.y *= LOL2BT_SIZE;
102        m_convex_shape = new btCylinderShape(LOL2BT_VEC3(new_cyl_size));
103        SetShapeTo(m_convex_shape);
104}
105
106void EasyPhysic::SetShapeToCapsule(float radius, float height)
107{
108        m_convex_shape = new btCapsuleShape(radius * LOL2BT_UNIT * LOL2BT_SIZE,
109                                                                                height * LOL2BT_UNIT * LOL2BT_SIZE);
110        SetShapeTo(m_convex_shape);
111}
112
113//-------------------------------------------------------------------------
114//Base Location/Rotation setup
115//--
116
117//Getter
118mat4 EasyPhysic::GetTransform()
119{
120        m_local_to_world = lol::mat4(1.0f);
121        if (m_rigid_body && m_motion_state)
122        {
123                btTransform CurTransform;
124                m_motion_state->getWorldTransform(CurTransform);
125                CurTransform.getOpenGLMatrix(&m_local_to_world[0][0]);
126        }
127        else if (m_collision_object)
128                m_collision_object->getWorldTransform().getOpenGLMatrix(&m_local_to_world[0][0]);
129        return m_local_to_world;
130}
131
132//Setter
133void EasyPhysic::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
134{
135        lol::mat4 PreviousMatrix = m_local_to_world;
136        m_local_to_world = lol::mat4::translate(base_location) * lol::mat4(base_rotation);
137
138        if (m_ghost_object)
139                m_ghost_object->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
140        else
141        {
142                if (m_motion_state)
143                        m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
144                else
145                        m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
146        }
147
148        for (int i = 0; i < m_based_physic_list.Count(); i++)
149        {
150                if (m_based_physic_list[i])
151                        m_based_physic_list[i]->BaseTransformChanged(PreviousMatrix, m_local_to_world);
152                else
153                        m_based_physic_list.Remove(i--);
154        }
155}
156
157//Internal callback when Base transform has changed.
158void EasyPhysic::BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix)
159{
160        mat4 PreviousMatrixLoc = ((m_base_lock_location)?(PreviousMatrix):(lol::mat4::translate(PreviousMatrix.v3.xyz)));
161        mat4 PreviousMatrixRot = ((m_base_lock_rotation)?(lol::mat4(lol::quat(PreviousMatrix))):(lol::mat4(1.f)));
162        mat4 NewMatrixLoc = ((m_base_lock_location)?(NewMatrix):(lol::mat4::translate(NewMatrix.v3.xyz)));
163        mat4 NewMatrixRot = ((m_base_lock_rotation)?(lol::mat4(lol::quat(NewMatrix))):(lol::mat4(1.f)));
164       
165        if (m_ghost_object || (m_rigid_body->getCollisionFlags() & btCollisionObject::CF_KINEMATIC_OBJECT))
166        {
167                mat4 ThisMatrixLoc = NewMatrixLoc * inverse(PreviousMatrixLoc) * lol::mat4::translate(m_local_to_world.v3.xyz);
168                mat4 ThisMatrixRot = NewMatrixRot * inverse(PreviousMatrixRot) * lol::mat4(lol::quat(m_local_to_world));
169                SetTransform(ThisMatrixLoc.v3.xyz, lol::mat4(lol::quat(ThisMatrixRot)));
170        }
171}
172
173//-------------------------------------------------------------------------
174//Mass related functions
175//--
176//Set Shape functions
177void EasyPhysic::SetMass(float mass)
178{
179        m_mass = mass;
180
181        if (m_rigid_body)
182        {
183                SetLocalInertia(m_mass);
184                m_rigid_body->setMassProps(mass, m_local_inertia);
185        }
186}
187
188//-------------------------------------------------------------------------
189//Final conversion pass functons : Body related
190//--
191
192//Init to rigid body
193void EasyPhysic::InitBodyToRigid(bool SetToKinematic)
194{
195        if (m_collision_object)
196                delete m_collision_object;
197
198        if (!m_motion_state)
199                SetTransform(vec3(.0f));
200
201        btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia);
202        m_rigid_body = new btRigidBody(NewInfos);
203        m_collision_object = m_rigid_body;
204        m_collision_object->setUserPointer(this);
205
206        if (m_mass == .0f)
207        {
208                if (SetToKinematic)
209                {
210                        m_rigid_body->setActivationState(DISABLE_DEACTIVATION);
211                        m_rigid_body->setCollisionFlags(m_rigid_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
212                }
213        }
214        else
215                SetMass(m_mass);
216}
217
218//Return correct Ghost Object
219btGhostObject* EasyPhysic::GetGhostObjectInstance()
220{
221        return new btGhostObject();
222}
223
224//Init to Ghost object, for Overlap/Sweep Test/Touching logic
225void EasyPhysic::InitBodyToGhost()
226{
227        if (m_collision_object)
228                delete m_collision_object;
229
230        m_ghost_object = GetGhostObjectInstance();
231        m_ghost_object->setCollisionShape(m_collision_shape);
232        m_collision_object = m_ghost_object;
233        m_collision_object->setUserPointer(this);
234
235        SetTransform(m_local_to_world.v3.xyz, lol::quat(m_local_to_world));
236
237        m_ghost_object->setCollisionFlags(m_ghost_object->getCollisionFlags());
238}
239
240//-------------
241//Touch logic
242//-------------
243  //    btManifoldArray   manifoldArray;
244  //    btBroadphasePairArray& pairArray = ghostObject->getOverlappingPairCache()->getOverlappingPairArray();
245  //    int numPairs = pairArray.size();
246
247  //    for (int i=0;i<numPairs;i++)
248  //    {
249  //       manifoldArray.clear();
250
251  //       const btBroadphasePair& pair = pairArray[i];
252  //       
253  //       //unless we manually perform collision detection on this pair, the contacts are in the dynamics world paircache:
254  //       btBroadphasePair* collisionPair = dynamicsWorld->getPairCache()->findPair(pair.m_pProxy0,pair.m_pProxy1);
255  //       if (!collisionPair)
256  //          continue;
257
258  //       if (collisionPair->m_algorithm)
259  //          collisionPair->m_algorithm->getAllContactManifolds(manifoldArray);
260
261  //       for (int j=0;j<manifoldArray.size();j++)
262  //       {
263  //          btPersistentManifold* manifold = manifoldArray[j];
264  //          btScalar directionSign = manifold->getBody0() == m_ghostObject ? btScalar(-1.0) : btScalar(1.0);
265  //          for (int p=0;p<manifold->getNumContacts();p++)
266  //          {
267  //            const btManifoldPoint&pt = manifold->getContactPoint(p);
268  //              if (pt.getDistance()<0.f)
269                //{
270                //      const btVector3& ptA = pt.getPositionWorldOnA();
271                //      const btVector3& ptB = pt.getPositionWorldOnB();
272                //      const btVector3& normalOnB = pt.m_normalWorldOnB;
273                //      /// work here
274                //}
275  //          }
276  //       }
277  //    }
278
279
280//Add Physic object to the simulation
281void EasyPhysic::AddToSimulation(class Simulation* current_simulation)
282{
283        btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
284        if (dynamics_world)
285        {
286                if (m_ghost_object)
287                {
288                        dynamics_world->addCollisionObject(m_ghost_object, m_collision_group, m_collision_mask);
289                        current_simulation->AddToGhost(this);
290                }
291                else if (m_rigid_body)
292                {
293                        dynamics_world->addRigidBody(m_rigid_body, m_collision_group, m_collision_mask);
294                        if (m_mass != .0f)
295                                current_simulation->AddToDynamic(this);
296                        else
297                                current_simulation->AddToStatic(this);
298                }
299                else
300                        dynamics_world->addCollisionObject(m_collision_object, m_collision_group, m_collision_mask);
301        }
302}
303
304//Remove Physic object to the simulation
305void EasyPhysic::RemoveFromSimulation(class Simulation* current_simulation)
306{
307        btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
308        if (dynamics_world)
309        {
310                if (m_rigid_body)
311                        dynamics_world->removeRigidBody(m_rigid_body);
312                else if (m_collision_object)
313                        dynamics_world->removeCollisionObject(m_collision_object);
314        }
315}
316
317//Set Local Inertia
318void EasyPhysic::SetLocalInertia(float mass)
319{
320        if (mass != .0f)
321                m_collision_shape->calculateLocalInertia(mass, m_local_inertia);
322        else
323                m_local_inertia = btVector3(.0f, .0f, .0f);
324}
325
326#endif // HAVE_PHYS_USE_BULLET
327
328} /* namespace phys */
329
330} /* namespace lol */
Note: See TracBrowser for help on using the repository browser.