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

Last change on this file since 2879 was 2879, checked in by touky, 6 years ago

btPhysTest : Added a new test mode called "CAT_MODE". Try it out at your own risks.

  • Property svn:eol-style set to LF
File size: 13.0 KB
RevLine 
[2816]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#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),
[2879]41    m_hit_restitution(.0f),
[2816]42    m_collision_group(1),
43    m_collision_mask(1),
44    m_owner_entity(NewOwnerEntity),
45    m_owner_simulation(NULL),
46    m_base_physic(NULL)
47{
48}
49
50EasyPhysic::~EasyPhysic()
51{
52    m_rigid_body = NULL;
53    delete m_collision_object;
54    delete m_collision_shape;
55    delete m_motion_state;
56}
57
58//-------------------------------------------------------------------------
59//Set Shape functions
60//--
61
62void EasyPhysic::SetShapeTo(btCollisionShape* collision_shape)
63{
64    bool bReinitToRigidBody = false;
65    if (m_rigid_body)
66    {
67        bReinitToRigidBody = true;
68        delete m_rigid_body;
69    }
70    if (m_collision_shape)
71        delete m_collision_shape;
72
73    m_collision_shape = collision_shape;
74
75    if (bReinitToRigidBody)
76        InitBodyToRigid();
77}
78
79//Box Shape support
80void EasyPhysic::SetShapeToBox(lol::vec3& box_size)
81{
82    vec3 new_box_size = box_size * LOL2BT_UNIT * LOL2BT_SIZE;
83    m_convex_shape = new btBoxShape(LOL2BT_VEC3(new_box_size));
84    SetShapeTo(m_convex_shape);
85}
86
87void EasyPhysic::SetShapeToSphere(float radius)
88{
89    m_convex_shape = new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE);
90    SetShapeTo(m_convex_shape);
91}
92
93void EasyPhysic::SetShapeToCone(float radius, float height)
94{
95    m_convex_shape = new btConeShape(    radius * LOL2BT_UNIT,
96                                        height * LOL2BT_UNIT);
97    SetShapeTo(m_convex_shape);
98}
99
100void EasyPhysic::SetShapeToCylinder(lol::vec3& cyl_size)
101{
102    vec3 new_cyl_size = cyl_size * LOL2BT_UNIT;
103    new_cyl_size.y *= LOL2BT_SIZE;
104    m_convex_shape = new btCylinderShape(LOL2BT_VEC3(new_cyl_size));
105    SetShapeTo(m_convex_shape);
106}
107
108void EasyPhysic::SetShapeToCapsule(float radius, float height)
109{
110    m_convex_shape = new btCapsuleShape(radius * LOL2BT_UNIT * LOL2BT_SIZE,
111                                        height * LOL2BT_UNIT * LOL2BT_SIZE);
112    SetShapeTo(m_convex_shape);
113}
114
115//-------------------------------------------------------------------------
116//Base Location/Rotation setup
117//--
118
119//Getter
120mat4 EasyPhysic::GetTransform()
121{
122    m_local_to_world = lol::mat4(1.0f);
123    if (m_rigid_body && m_motion_state)
124    {
125        btTransform CurTransform;
126        m_motion_state->getWorldTransform(CurTransform);
127        CurTransform.getOpenGLMatrix(&m_local_to_world[0][0]);
128    }
129    else if (m_collision_object)
130        m_collision_object->getWorldTransform().getOpenGLMatrix(&m_local_to_world[0][0]);
131    return m_local_to_world;
132}
133
134//Setter
135void EasyPhysic::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
136{
137    lol::mat4 PreviousMatrix = m_local_to_world;
138    m_local_to_world = lol::mat4::translate(base_location) * lol::mat4(base_rotation);
139
140    if (m_ghost_object)
141        m_ghost_object->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
142    else
143    {
144        if (m_motion_state)
145            m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
146        else
147            m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * base_location)));
148    }
149
150    for (int i = 0; i < m_based_physic_list.Count(); i++)
151    {
152        if (m_based_physic_list[i])
153            m_based_physic_list[i]->BaseTransformChanged(PreviousMatrix, m_local_to_world);
154        else
155            m_based_physic_list.Remove(i--);
156    }
157}
158
159//Internal callback when Base transform has changed.
160void EasyPhysic::BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix)
161{
162    mat4 PreviousMatrixLoc = ((m_base_lock_location)?(PreviousMatrix):(lol::mat4::translate(PreviousMatrix.v3.xyz)));
163    mat4 PreviousMatrixRot = ((m_base_lock_rotation)?(lol::mat4(lol::quat(PreviousMatrix))):(lol::mat4(1.f)));
164    mat4 NewMatrixLoc = ((m_base_lock_location)?(NewMatrix):(lol::mat4::translate(NewMatrix.v3.xyz)));
165    mat4 NewMatrixRot = ((m_base_lock_rotation)?(lol::mat4(lol::quat(NewMatrix))):(lol::mat4(1.f)));
166
167    if (m_ghost_object || (m_rigid_body->getCollisionFlags() & btCollisionObject::CF_KINEMATIC_OBJECT))
168    {
169        mat4 ThisMatrixLoc = NewMatrixLoc * inverse(PreviousMatrixLoc) * lol::mat4::translate(m_local_to_world.v3.xyz);
170        mat4 ThisMatrixRot = NewMatrixRot * inverse(PreviousMatrixRot) * lol::mat4(lol::quat(m_local_to_world));
171        SetTransform(ThisMatrixLoc.v3.xyz, lol::mat4(lol::quat(ThisMatrixRot)));
172    }
173}
174
175//-------------------------------------------------------------------------
176//Mass related functions
177//--
[2879]178//Set Mass functions
[2816]179void EasyPhysic::SetMass(float mass)
180{
181    m_mass = mass;
182
183    if (m_rigid_body)
184    {
185        SetLocalInertia(m_mass);
[2879]186        m_rigid_body->setMassProps(m_mass, m_local_inertia);
[2816]187    }
188}
189
190//-------------------------------------------------------------------------
[2879]191//Hit restitution functions
[2816]192//--
[2879]193//Set Hit Restitution functions
194void EasyPhysic::SetHitRestitution(float hit_restitution)
195{
196    m_hit_restitution = hit_restitution;
[2816]197
[2879]198    if (m_rigid_body)
199    {
200        m_rigid_body->setRestitution(m_hit_restitution);
201    }
202}
203
204//-------------------------------------------------------------------------
205//Final conversion pass functions : Body related
206//--
207
[2816]208//Init to rigid body
209void EasyPhysic::InitBodyToRigid(bool SetToKinematic)
210{
211    if (m_collision_object)
212        delete m_collision_object;
213
214    if (!m_motion_state)
215        SetTransform(vec3(.0f));
216
217    btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia);
[2879]218    NewInfos.m_restitution = m_hit_restitution;
[2816]219    m_rigid_body = new btRigidBody(NewInfos);
220    m_collision_object = m_rigid_body;
221    m_collision_object->setUserPointer(this);
222
223    if (m_mass == .0f)
224    {
225        if (SetToKinematic)
226        {
227            m_rigid_body->setActivationState(DISABLE_DEACTIVATION);
228            m_rigid_body->setCollisionFlags(m_rigid_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
229        }
230    }
231    else
232        SetMass(m_mass);
233}
234
235//Return correct Ghost Object
236btGhostObject* EasyPhysic::GetGhostObjectInstance()
237{
238    return new btGhostObject();
239}
240
241//Init to Ghost object, for Overlap/Sweep Test/Touching logic
242void EasyPhysic::InitBodyToGhost()
243{
244    if (m_collision_object)
245        delete m_collision_object;
246
247    m_ghost_object = GetGhostObjectInstance();
248    m_ghost_object->setCollisionShape(m_collision_shape);
249    m_collision_object = m_ghost_object;
250    m_collision_object->setUserPointer(this);
251
252    SetTransform(m_local_to_world.v3.xyz, lol::quat(m_local_to_world));
253
254    m_ghost_object->setCollisionFlags(m_ghost_object->getCollisionFlags());
255}
256
257//-------------
258//Touch logic
259//-------------
260  //    btManifoldArray   manifoldArray;
261  //    btBroadphasePairArray& pairArray = ghostObject->getOverlappingPairCache()->getOverlappingPairArray();
262  //    int numPairs = pairArray.size();
263
264  //    for (int i=0;i<numPairs;i++)
265  //    {
266  //       manifoldArray.clear();
267
268  //       const btBroadphasePair& pair = pairArray[i];
269  //
270  //       //unless we manually perform collision detection on this pair, the contacts are in the dynamics world paircache:
271  //       btBroadphasePair* collisionPair = dynamicsWorld->getPairCache()->findPair(pair.m_pProxy0,pair.m_pProxy1);
272  //       if (!collisionPair)
273  //          continue;
274
275  //       if (collisionPair->m_algorithm)
276  //          collisionPair->m_algorithm->getAllContactManifolds(manifoldArray);
277
278  //       for (int j=0;j<manifoldArray.size();j++)
279  //       {
280  //          btPersistentManifold* manifold = manifoldArray[j];
281  //          btScalar directionSign = manifold->getBody0() == m_ghostObject ? btScalar(-1.0) : btScalar(1.0);
282  //          for (int p=0;p<manifold->getNumContacts();p++)
283  //          {
284  //               const btManifoldPoint&pt = manifold->getContactPoint(p);
285  //              if (pt.getDistance()<0.f)
286        //{
287        //    const btVector3& ptA = pt.getPositionWorldOnA();
288        //    const btVector3& ptB = pt.getPositionWorldOnB();
289        //    const btVector3& normalOnB = pt.m_normalWorldOnB;
290        //    /// work here
291        //}
292  //          }
293  //       }
294  //    }
295
296
297//Add Physic object to the simulation
298void EasyPhysic::AddToSimulation(class Simulation* current_simulation)
299{
300    btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
301    if (dynamics_world)
302    {
303        if (m_ghost_object)
304        {
305            dynamics_world->addCollisionObject(m_ghost_object, m_collision_group, m_collision_mask);
306            current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Ghost);
307        }
308        else if (m_rigid_body)
309        {
310            dynamics_world->addRigidBody(m_rigid_body, m_collision_group, m_collision_mask);
311            if (m_mass != .0f)
312                current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Dynamic);
313            else
314                current_simulation->ObjectRegistration(true, this, Simulation::EEPT_Static);
315        }
316        else
317        {
318            dynamics_world->addCollisionObject(m_collision_object, m_collision_group, m_collision_mask);
319            current_simulation->ObjectRegistration(true, this, Simulation::EEPT_CollisionObject);
320        }
321    }
322}
323
324//Remove Physic object to the simulation
325void EasyPhysic::RemoveFromSimulation(class Simulation* current_simulation)
326{
327    btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
328    if (dynamics_world)
329    {
330        if (m_rigid_body)
331        {
332            dynamics_world->removeRigidBody(m_rigid_body);
333            if (m_mass != .0f)
334                current_simulation->ObjectRegistration(false, this, Simulation::EEPT_Dynamic);
335            else
336                current_simulation->ObjectRegistration(false, this, Simulation::EEPT_Static);
337        }
338        else
339        {
340            dynamics_world->removeCollisionObject(m_collision_object);
341            if (m_ghost_object)
342                current_simulation->ObjectRegistration(false, this, Simulation::EEPT_Ghost);
343            current_simulation->ObjectRegistration(false, this, Simulation::EEPT_CollisionObject);
344        }
345    }
346}
347
[2879]348//-------------------------------------------------------------------------
349//Force/Impulse functions
350//--
351void EasyPhysic::AddImpulse(const lol::vec3& impulse)
352{
353    if (m_rigid_body)
354        m_rigid_body->applyCentralImpulse(LOL2BT_VEC3(impulse));
355}
356
357void EasyPhysic::AddImpulse(const lol::vec3& impulse, const lol::vec3& rel_pos)
358{
359    if (m_rigid_body)
360        m_rigid_body->applyImpulse(LOL2BT_VEC3(impulse), LOL2BTU_VEC3(rel_pos));
361}
362
363void EasyPhysic::AddImpulseTorque(const lol::vec3& torque)
364{
365    if (m_rigid_body)
366        m_rigid_body->applyTorqueImpulse(LOL2BT_VEC3(torque));
367}
368
369//--
370void EasyPhysic::AddForce(const lol::vec3& force)
371{
372    if (m_rigid_body)
373        m_rigid_body->applyCentralForce(LOL2BT_VEC3(force));
374}
375
376void EasyPhysic::AddForce(const lol::vec3& force, const lol::vec3& rel_pos)
377{
378    if (m_rigid_body)
379        m_rigid_body->applyForce(LOL2BT_VEC3(force), LOL2BTU_VEC3(rel_pos));
380}
381
382void EasyPhysic::AddForceTorque(const lol::vec3& torque)
383{
384    if (m_rigid_body)
385        m_rigid_body->applyTorque(LOL2BT_VEC3(torque));
386}
387
388//-------------------------------------------------------------------------
389//Movements getter
390//--
391lol::vec3 EasyPhysic::GetLinearVelocity() const
392{
393    if (m_rigid_body)
394        return BT2LOL_VEC3(m_rigid_body->getLinearVelocity());
395    return lol::vec3(.0f);
396}
397
398lol::vec3 EasyPhysic::GetLinearForce() const
399{
400    if (m_rigid_body)
401        return BT2LOL_VEC3(m_rigid_body->getTotalForce());
402    return lol::vec3(.0f);
403}
404
405lol::vec3 EasyPhysic::GetAngularVelocity() const
406{
407    if (m_rigid_body)
408        return BT2LOL_VEC3(m_rigid_body->getAngularVelocity());
409    return lol::vec3(.0f);
410}
411
412lol::vec3 EasyPhysic::GetAngularForce() const
413{
414    if (m_rigid_body)
415        return BT2LOL_VEC3(m_rigid_body->getTotalTorque());
416    return lol::vec3(.0f);
417}
418
[2816]419//Set Local Inertia
420void EasyPhysic::SetLocalInertia(float mass)
421{
422    if (mass != .0f)
423        m_collision_shape->calculateLocalInertia(mass, m_local_inertia);
424    else
425        m_local_inertia = btVector3(.0f, .0f, .0f);
426}
427
428#endif // HAVE_PHYS_USE_BULLET
429
430} /* namespace phys */
431
432} /* namespace lol */
Note: See TracBrowser for help on using the repository browser.