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

Last change on this file since 1888 was 1888, checked in by lolbot, 7 years ago

fixed 23 files out of 277:

  • fixed 1270 CR characters
  • fixed 56 trailing spaces
  • fixed 5085 tabs
File size: 10.9 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_owner_simulation(NULL),
45    m_base_physic(NULL)
46{
47}
48
49EasyPhysic::~EasyPhysic()
50{
51    m_rigid_body = NULL;
52    delete m_collision_object;
53    delete m_collision_shape;
54    delete m_motion_state;
55}
56
57//-------------------------------------------------------------------------
58//Set Shape functions
59//--
60
61void EasyPhysic::SetShapeTo(btCollisionShape* collision_shape)
62{
63    bool bReinitToRigidBody = false;
64    if (m_rigid_body)
65    {
66        bReinitToRigidBody = true;
67        delete m_rigid_body;
68    }
69    if (m_collision_shape)
70        delete m_collision_shape;
71
72    m_collision_shape = collision_shape;
73
74    if (bReinitToRigidBody)
75        InitBodyToRigid();
76}
77
78//Box Shape support
79void EasyPhysic::SetShapeToBox(lol::vec3& box_size)
80{
81    vec3 new_box_size = box_size * LOL2BT_UNIT * LOL2BT_SIZE;
82    m_convex_shape = new btBoxShape(LOL2BT_VEC3(new_box_size));
83    SetShapeTo(m_convex_shape);
84}
85
86void EasyPhysic::SetShapeToSphere(float radius)
87{
88    m_convex_shape = new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE);
89    SetShapeTo(m_convex_shape);
90}
91
92void EasyPhysic::SetShapeToCone(float radius, float height)
93{
94    m_convex_shape = new btConeShape(    radius * LOL2BT_UNIT,
95                                        height * LOL2BT_UNIT);
96    SetShapeTo(m_convex_shape);
97}
98
99void EasyPhysic::SetShapeToCylinder(lol::vec3& cyl_size)
100{
101    vec3 new_cyl_size = cyl_size * LOL2BT_UNIT;
102    new_cyl_size.y *= LOL2BT_SIZE;
103    m_convex_shape = new btCylinderShape(LOL2BT_VEC3(new_cyl_size));
104    SetShapeTo(m_convex_shape);
105}
106
107void EasyPhysic::SetShapeToCapsule(float radius, float height)
108{
109    m_convex_shape = new btCapsuleShape(radius * LOL2BT_UNIT * LOL2BT_SIZE,
110                                        height * LOL2BT_UNIT * LOL2BT_SIZE);
111    SetShapeTo(m_convex_shape);
112}
113
114//-------------------------------------------------------------------------
115//Base Location/Rotation setup
116//--
117
118//Getter
119mat4 EasyPhysic::GetTransform()
120{
121    m_local_to_world = lol::mat4(1.0f);
122    if (m_rigid_body && m_motion_state)
123    {
124        btTransform CurTransform;
125        m_motion_state->getWorldTransform(CurTransform);
126        CurTransform.getOpenGLMatrix(&m_local_to_world[0][0]);
127    }
128    else if (m_collision_object)
129        m_collision_object->getWorldTransform().getOpenGLMatrix(&m_local_to_world[0][0]);
130    return m_local_to_world;
131}
132
133//Setter
134void EasyPhysic::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
135{
136    lol::mat4 PreviousMatrix = m_local_to_world;
137    m_local_to_world = lol::mat4::translate(base_location) * lol::mat4(base_rotation);
138
139    if (m_ghost_object)
140        m_ghost_object->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
141    else
142    {
143        if (m_motion_state)
144            m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
145        else
146            m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
147    }
148
149    for (int i = 0; i < m_based_physic_list.Count(); i++)
150    {
151        if (m_based_physic_list[i])
152            m_based_physic_list[i]->BaseTransformChanged(PreviousMatrix, m_local_to_world);
153        else
154            m_based_physic_list.Remove(i--);
155    }
156}
157
158//Internal callback when Base transform has changed.
159void EasyPhysic::BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix)
160{
161    mat4 PreviousMatrixLoc = ((m_base_lock_location)?(PreviousMatrix):(lol::mat4::translate(PreviousMatrix.v3.xyz)));
162    mat4 PreviousMatrixRot = ((m_base_lock_rotation)?(lol::mat4(lol::quat(PreviousMatrix))):(lol::mat4(1.f)));
163    mat4 NewMatrixLoc = ((m_base_lock_location)?(NewMatrix):(lol::mat4::translate(NewMatrix.v3.xyz)));
164    mat4 NewMatrixRot = ((m_base_lock_rotation)?(lol::mat4(lol::quat(NewMatrix))):(lol::mat4(1.f)));
165
166    if (m_ghost_object || (m_rigid_body->getCollisionFlags() & btCollisionObject::CF_KINEMATIC_OBJECT))
167    {
168        mat4 ThisMatrixLoc = NewMatrixLoc * inverse(PreviousMatrixLoc) * lol::mat4::translate(m_local_to_world.v3.xyz);
169        mat4 ThisMatrixRot = NewMatrixRot * inverse(PreviousMatrixRot) * lol::mat4(lol::quat(m_local_to_world));
170        SetTransform(ThisMatrixLoc.v3.xyz, lol::mat4(lol::quat(ThisMatrixRot)));
171    }
172}
173
174//-------------------------------------------------------------------------
175//Mass related functions
176//--
177//Set Shape functions
178void EasyPhysic::SetMass(float mass)
179{
180    m_mass = mass;
181
182    if (m_rigid_body)
183    {
184        SetLocalInertia(m_mass);
185        m_rigid_body->setMassProps(mass, m_local_inertia);
186    }
187}
188
189//-------------------------------------------------------------------------
190//Final conversion pass functons : Body related
191//--
192
193//Init to rigid body
194void EasyPhysic::InitBodyToRigid(bool SetToKinematic)
195{
196    if (m_collision_object)
197        delete m_collision_object;
198
199    if (!m_motion_state)
200        SetTransform(vec3(.0f));
201
202    btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia);
203    m_rigid_body = new btRigidBody(NewInfos);
204    m_collision_object = m_rigid_body;
205    m_collision_object->setUserPointer(this);
206
207    if (m_mass == .0f)
208    {
209        if (SetToKinematic)
210        {
211            m_rigid_body->setActivationState(DISABLE_DEACTIVATION);
212            m_rigid_body->setCollisionFlags(m_rigid_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
213        }
214    }
215    else
216        SetMass(m_mass);
217}
218
219//Return correct Ghost Object
220btGhostObject* EasyPhysic::GetGhostObjectInstance()
221{
222    return new btGhostObject();
223}
224
225//Init to Ghost object, for Overlap/Sweep Test/Touching logic
226void EasyPhysic::InitBodyToGhost()
227{
228    if (m_collision_object)
229        delete m_collision_object;
230
231    m_ghost_object = GetGhostObjectInstance();
232    m_ghost_object->setCollisionShape(m_collision_shape);
233    m_collision_object = m_ghost_object;
234    m_collision_object->setUserPointer(this);
235
236    SetTransform(m_local_to_world.v3.xyz, lol::quat(m_local_to_world));
237
238    m_ghost_object->setCollisionFlags(m_ghost_object->getCollisionFlags());
239}
240
241//-------------
242//Touch logic
243//-------------
244  //    btManifoldArray   manifoldArray;
245  //    btBroadphasePairArray& pairArray = ghostObject->getOverlappingPairCache()->getOverlappingPairArray();
246  //    int numPairs = pairArray.size();
247
248  //    for (int i=0;i<numPairs;i++)
249  //    {
250  //       manifoldArray.clear();
251
252  //       const btBroadphasePair& pair = pairArray[i];
253  //
254  //       //unless we manually perform collision detection on this pair, the contacts are in the dynamics world paircache:
255  //       btBroadphasePair* collisionPair = dynamicsWorld->getPairCache()->findPair(pair.m_pProxy0,pair.m_pProxy1);
256  //       if (!collisionPair)
257  //          continue;
258
259  //       if (collisionPair->m_algorithm)
260  //          collisionPair->m_algorithm->getAllContactManifolds(manifoldArray);
261
262  //       for (int j=0;j<manifoldArray.size();j++)
263  //       {
264  //          btPersistentManifold* manifold = manifoldArray[j];
265  //          btScalar directionSign = manifold->getBody0() == m_ghostObject ? btScalar(-1.0) : btScalar(1.0);
266  //          for (int p=0;p<manifold->getNumContacts();p++)
267  //          {
268  //               const btManifoldPoint&pt = manifold->getContactPoint(p);
269  //              if (pt.getDistance()<0.f)
270        //{
271        //    const btVector3& ptA = pt.getPositionWorldOnA();
272        //    const btVector3& ptB = pt.getPositionWorldOnB();
273        //    const btVector3& normalOnB = pt.m_normalWorldOnB;
274        //    /// work here
275        //}
276  //          }
277  //       }
278  //    }
279
280
281//Add Physic object to the simulation
282void EasyPhysic::AddToSimulation(class Simulation* current_simulation)
283{
284    btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
285    if (dynamics_world)
286    {
287        if (m_ghost_object)
288        {
289            dynamics_world->addCollisionObject(m_ghost_object, m_collision_group, m_collision_mask);
290            current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Ghost);
291        }
292        else if (m_rigid_body)
293        {
294            dynamics_world->addRigidBody(m_rigid_body, m_collision_group, m_collision_mask);
295            if (m_mass != .0f)
296                current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Dynamic);
297            else
298                current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Static);
299        }
300        else
301        {
302            dynamics_world->addCollisionObject(m_collision_object, m_collision_group, m_collision_mask);
303            current_simulation->ObjectRegistration(true, this, Simulation::EEPT_CollisionObject);
304        }
305    }
306}
307
308//Remove Physic object to the simulation
309void EasyPhysic::RemoveFromSimulation(class Simulation* current_simulation)
310{
311    btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
312    if (dynamics_world)
313    {
314        if (m_rigid_body)
315        {
316            dynamics_world->removeRigidBody(m_rigid_body);
317            if (m_mass != .0f)
318                current_simulation->ObjectRegistration(false, this, Simulation::EEPT_Dynamic);
319            else
320                current_simulation->ObjectRegistration(false, this, Simulation::EEPT_Static);
321        }
322        else
323        {
324            dynamics_world->removeCollisionObject(m_collision_object);
325            if (m_ghost_object)
326                current_simulation->ObjectRegistration(false, this, Simulation::EEPT_Ghost);
327            current_simulation->ObjectRegistration(false, this, Simulation::EEPT_CollisionObject);
328        }
329    }
330}
331
332//Set Local Inertia
333void EasyPhysic::SetLocalInertia(float mass)
334{
335    if (mass != .0f)
336        m_collision_shape->calculateLocalInertia(mass, m_local_inertia);
337    else
338        m_local_inertia = btVector3(.0f, .0f, .0f);
339}
340
341#endif // HAVE_PHYS_USE_BULLET
342
343} /* namespace phys */
344
345} /* namespace lol */
Note: See TracBrowser for help on using the repository browser.