source: trunk/test/physics/easyphysics.cpp @ 2910

Last change on this file since 2910 was 2910, checked in by sam, 6 years ago

physics: always activate Bullet and remove dead code.

  • Property svn:eol-style set to LF
File size: 12.9 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5//            (c) 2009-2013 Cédric Lecacheur <jordx@free.fr>
6//            (c) 2009-2013 Benjamin "Touky" 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://www.wtfpl.net/ for more details.
11//
12
13#if defined HAVE_CONFIG_H
14#   include "config.h"
15#endif
16
17#include "lolbtphysicsintegration.h"
18#include "lolphysics.h"
19
20namespace lol
21{
22
23namespace phys
24{
25
26//-------------------------------------------------------------------------
27//EASY_PHYSIC
28//--
29
30EasyPhysic::EasyPhysic(WorldEntity* NewOwnerEntity) :
31    m_collision_object(NULL),
32    m_ghost_object(NULL),
33    m_rigid_body(NULL),
34    m_local_inertia(btVector3(.0f, .0f, .0f)),
35    m_collision_shape(NULL),
36    m_convex_shape(NULL),
37    m_motion_state(NULL),
38    m_mass(.0f),
39    m_hit_restitution(.0f),
40    m_collision_group(1),
41    m_collision_mask(1),
42    m_owner_entity(NewOwnerEntity),
43    m_owner_simulation(NULL),
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 Mass 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(m_mass, m_local_inertia);
185    }
186}
187
188//-------------------------------------------------------------------------
189//Hit restitution functions
190//--
191//Set Hit Restitution functions
192void EasyPhysic::SetHitRestitution(float hit_restitution)
193{
194    m_hit_restitution = hit_restitution;
195
196    if (m_rigid_body)
197    {
198        m_rigid_body->setRestitution(m_hit_restitution);
199    }
200}
201
202//-------------------------------------------------------------------------
203//Final conversion pass functions : Body related
204//--
205
206//Init to rigid body
207void EasyPhysic::InitBodyToRigid(bool SetToKinematic)
208{
209    if (m_collision_object)
210        delete m_collision_object;
211
212    if (!m_motion_state)
213        SetTransform(vec3(.0f));
214
215    btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia);
216    NewInfos.m_restitution = m_hit_restitution;
217    m_rigid_body = new btRigidBody(NewInfos);
218    m_collision_object = m_rigid_body;
219    m_collision_object->setUserPointer(this);
220
221    if (m_mass == .0f)
222    {
223        if (SetToKinematic)
224        {
225            m_rigid_body->setActivationState(DISABLE_DEACTIVATION);
226            m_rigid_body->setCollisionFlags(m_rigid_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
227        }
228    }
229    else
230        SetMass(m_mass);
231}
232
233//Return correct Ghost Object
234btGhostObject* EasyPhysic::GetGhostObjectInstance()
235{
236    return new btGhostObject();
237}
238
239//Init to Ghost object, for Overlap/Sweep Test/Touching logic
240void EasyPhysic::InitBodyToGhost()
241{
242    if (m_collision_object)
243        delete m_collision_object;
244
245    m_ghost_object = GetGhostObjectInstance();
246    m_ghost_object->setCollisionShape(m_collision_shape);
247    m_collision_object = m_ghost_object;
248    m_collision_object->setUserPointer(this);
249
250    SetTransform(m_local_to_world.v3.xyz, lol::quat(m_local_to_world));
251
252    m_ghost_object->setCollisionFlags(m_ghost_object->getCollisionFlags());
253}
254
255//-------------
256//Touch logic
257//-------------
258  //    btManifoldArray   manifoldArray;
259  //    btBroadphasePairArray& pairArray = ghostObject->getOverlappingPairCache()->getOverlappingPairArray();
260  //    int numPairs = pairArray.size();
261
262  //    for (int i=0;i<numPairs;i++)
263  //    {
264  //       manifoldArray.clear();
265
266  //       const btBroadphasePair& pair = pairArray[i];
267  //
268  //       //unless we manually perform collision detection on this pair, the contacts are in the dynamics world paircache:
269  //       btBroadphasePair* collisionPair = dynamicsWorld->getPairCache()->findPair(pair.m_pProxy0,pair.m_pProxy1);
270  //       if (!collisionPair)
271  //          continue;
272
273  //       if (collisionPair->m_algorithm)
274  //          collisionPair->m_algorithm->getAllContactManifolds(manifoldArray);
275
276  //       for (int j=0;j<manifoldArray.size();j++)
277  //       {
278  //          btPersistentManifold* manifold = manifoldArray[j];
279  //          btScalar directionSign = manifold->getBody0() == m_ghostObject ? btScalar(-1.0) : btScalar(1.0);
280  //          for (int p=0;p<manifold->getNumContacts();p++)
281  //          {
282  //               const btManifoldPoint&pt = manifold->getContactPoint(p);
283  //              if (pt.getDistance()<0.f)
284        //{
285        //    const btVector3& ptA = pt.getPositionWorldOnA();
286        //    const btVector3& ptB = pt.getPositionWorldOnB();
287        //    const btVector3& normalOnB = pt.m_normalWorldOnB;
288        //    /// work here
289        //}
290  //          }
291  //       }
292  //    }
293
294
295//Add Physic object to the simulation
296void EasyPhysic::AddToSimulation(class Simulation* current_simulation)
297{
298    btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
299    if (dynamics_world)
300    {
301        if (m_ghost_object)
302        {
303            dynamics_world->addCollisionObject(m_ghost_object, m_collision_group, m_collision_mask);
304            current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Ghost);
305        }
306        else if (m_rigid_body)
307        {
308            dynamics_world->addRigidBody(m_rigid_body, m_collision_group, m_collision_mask);
309            if (m_mass != .0f)
310                current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Dynamic);
311            else
312                current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Static);
313        }
314        else
315        {
316            dynamics_world->addCollisionObject(m_collision_object, m_collision_group, m_collision_mask);
317            current_simulation->ObjectRegistration(true, this, Simulation::EEPT_CollisionObject);
318        }
319    }
320}
321
322//Remove Physic object to the simulation
323void EasyPhysic::RemoveFromSimulation(class Simulation* current_simulation)
324{
325    btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
326    if (dynamics_world)
327    {
328        if (m_rigid_body)
329        {
330            dynamics_world->removeRigidBody(m_rigid_body);
331            if (m_mass != .0f)
332                current_simulation->ObjectRegistration(false, this, Simulation::EEPT_Dynamic);
333            else
334                current_simulation->ObjectRegistration(false, this, Simulation::EEPT_Static);
335        }
336        else
337        {
338            dynamics_world->removeCollisionObject(m_collision_object);
339            if (m_ghost_object)
340                current_simulation->ObjectRegistration(false, this, Simulation::EEPT_Ghost);
341            current_simulation->ObjectRegistration(false, this, Simulation::EEPT_CollisionObject);
342        }
343    }
344}
345
346//-------------------------------------------------------------------------
347//Force/Impulse functions
348//--
349void EasyPhysic::AddImpulse(const lol::vec3& impulse)
350{
351    if (m_rigid_body)
352        m_rigid_body->applyCentralImpulse(LOL2BT_VEC3(impulse));
353}
354
355void EasyPhysic::AddImpulse(const lol::vec3& impulse, const lol::vec3& rel_pos)
356{
357    if (m_rigid_body)
358        m_rigid_body->applyImpulse(LOL2BT_VEC3(impulse), LOL2BTU_VEC3(rel_pos));
359}
360
361void EasyPhysic::AddImpulseTorque(const lol::vec3& torque)
362{
363    if (m_rigid_body)
364        m_rigid_body->applyTorqueImpulse(LOL2BT_VEC3(torque));
365}
366
367//--
368void EasyPhysic::AddForce(const lol::vec3& force)
369{
370    if (m_rigid_body)
371        m_rigid_body->applyCentralForce(LOL2BT_VEC3(force));
372}
373
374void EasyPhysic::AddForce(const lol::vec3& force, const lol::vec3& rel_pos)
375{
376    if (m_rigid_body)
377        m_rigid_body->applyForce(LOL2BT_VEC3(force), LOL2BTU_VEC3(rel_pos));
378}
379
380void EasyPhysic::AddForceTorque(const lol::vec3& torque)
381{
382    if (m_rigid_body)
383        m_rigid_body->applyTorque(LOL2BT_VEC3(torque));
384}
385
386//-------------------------------------------------------------------------
387//Movements getter
388//--
389lol::vec3 EasyPhysic::GetLinearVelocity() const
390{
391    if (m_rigid_body)
392        return BT2LOL_VEC3(m_rigid_body->getLinearVelocity());
393    return lol::vec3(.0f);
394}
395
396lol::vec3 EasyPhysic::GetLinearForce() const
397{
398    if (m_rigid_body)
399        return BT2LOL_VEC3(m_rigid_body->getTotalForce());
400    return lol::vec3(.0f);
401}
402
403lol::vec3 EasyPhysic::GetAngularVelocity() const
404{
405    if (m_rigid_body)
406        return BT2LOL_VEC3(m_rigid_body->getAngularVelocity());
407    return lol::vec3(.0f);
408}
409
410lol::vec3 EasyPhysic::GetAngularForce() const
411{
412    if (m_rigid_body)
413        return BT2LOL_VEC3(m_rigid_body->getTotalTorque());
414    return lol::vec3(.0f);
415}
416
417//Set Local Inertia
418void EasyPhysic::SetLocalInertia(float mass)
419{
420    if (mass != .0f)
421        m_collision_shape->calculateLocalInertia(mass, m_local_inertia);
422    else
423        m_local_inertia = btVector3(.0f, .0f, .0f);
424}
425
426} /* namespace phys */
427
428} /* namespace lol */
Note: See TracBrowser for help on using the repository browser.