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

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

Added Base/Attachment Logic, works with Kinematic & Ghost.

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