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

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

Small .h & .cpp refactor

File size: 6.6 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#ifdef HAVE_PHYS_USE_BULLET
18#include "../Include/EasyPhysics.h"
19#endif // HAVE_PHYS_USE_BULLET
20
21namespace lol
22{
23
24namespace phys
25{
26
27#ifdef HAVE_PHYS_USE_BULLET
28
29//-------------------------------------------------------------------------
30//EASY_PHYSIC
31//--
32
33EasyPhysic::EasyPhysic() :
34        m_collision_object(NULL),
35        m_rigid_body(NULL),
36        m_ghost_object(NULL),
37        m_collision_shape(NULL),
38        m_motion_state(NULL),
39        m_mass(.0f),
40        m_local_inertia(btVector3(.0f, .0f, .0f)),
41        m_collision_group(1),
42        m_collision_mask(1)
43{
44}
45
46EasyPhysic::~EasyPhysic()
47{
48        m_rigid_body = NULL;
49        delete m_collision_object;
50        delete m_collision_shape;
51        delete m_motion_state;
52}
53
54//-------------------------------------------------------------------------
55//Set Shape functions
56//--
57
58void EasyPhysic::SetShapeTo(btCollisionShape* collision_shape)
59{
60        bool bReinitToRigidBody = false;
61        if (m_rigid_body)
62        {
63                bReinitToRigidBody = true;
64                delete m_rigid_body;
65        }
66        if (m_collision_shape)
67                delete m_collision_shape;
68
69        m_collision_shape = collision_shape;
70
71        if (bReinitToRigidBody)
72                InitBodyToRigid();
73}
74
75//Box Shape support
76void EasyPhysic::SetShapeToBox(lol::vec3& box_size)
77{
78        vec3 new_box_size = box_size * LOL2BT_UNIT * LOL2BT_SIZE;
79        m_convex_shape = new btBoxShape(LOL2BT_VEC3(new_box_size));
80        SetShapeTo(m_convex_shape);
81}
82
83void EasyPhysic::SetShapeToSphere(float radius)
84{
85        m_convex_shape = new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE);
86        SetShapeTo(m_convex_shape);
87}
88
89void EasyPhysic::SetShapeToCone(float radius, float height)
90{
91        m_convex_shape = new btConeShape(       radius * LOL2BT_UNIT,
92                                                                                height * LOL2BT_UNIT);
93        SetShapeTo(m_convex_shape);
94}
95
96void EasyPhysic::SetShapeToCylinder(lol::vec3& cyl_size)
97{
98        vec3 new_cyl_size = cyl_size * LOL2BT_UNIT;
99        new_cyl_size.y *= LOL2BT_SIZE;
100        m_convex_shape = new btCylinderShape(LOL2BT_VEC3(new_cyl_size));
101        SetShapeTo(m_convex_shape);
102}
103
104void EasyPhysic::SetShapeToCapsule(float radius, float height)
105{
106        m_convex_shape = new btCapsuleShape(radius * LOL2BT_UNIT * LOL2BT_SIZE,
107                                                                                height * LOL2BT_UNIT * LOL2BT_SIZE);
108        SetShapeTo(m_convex_shape);
109}
110
111//-------------------------------------------------------------------------
112//Base Location/Rotation setup
113//--
114void EasyPhysic::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
115{
116        m_local_to_world = lol::mat4::translate(base_location) * mat4(base_rotation);
117
118        if (m_ghost_object)
119                m_ghost_object->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location * LOL2BT_UNIT)));
120        else
121        {
122                if (m_motion_state)
123                        m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location * LOL2BT_UNIT)));
124                else
125                        m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location * LOL2BT_UNIT)));
126        }
127}
128
129//-------------------------------------------------------------------------
130//Mass related functions
131//--
132//Set Shape functions
133void EasyPhysic::SetMass(float mass)
134{
135        m_mass = mass;
136
137        if (m_rigid_body)
138        {
139                SetLocalInertia(m_mass);
140                m_rigid_body->setMassProps(mass, m_local_inertia);
141        }
142}
143
144//-------------------------------------------------------------------------
145//Final conversion pass functons : Body related
146//--
147
148//Init to rigid body
149void EasyPhysic::InitBodyToRigid(bool SetToKinematic)
150{
151        if (m_collision_object)
152                delete m_collision_object;
153
154        if (!m_motion_state)
155                SetTransform(vec3(.0f));
156
157        btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia);
158        m_rigid_body = new btRigidBody(NewInfos);
159        m_collision_object = m_rigid_body;
160
161        if (m_mass == .0f)
162        {
163                if (SetToKinematic)
164                {
165                        m_rigid_body->setActivationState(DISABLE_DEACTIVATION);
166                        m_rigid_body->setCollisionFlags(m_rigid_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
167                }
168        }
169        else
170                SetMass(m_mass);
171}
172
173//Return correct Ghost Object
174btGhostObject* EasyPhysic::GetGhostObject()
175{
176        return new btGhostObject();
177}
178
179//Init to Ghost object, for Overlap/Sweep Test/Touching logic
180void EasyPhysic::InitBodyToGhost()
181{
182        if (m_collision_object)
183                delete m_collision_object;
184
185        m_ghost_object = GetGhostObject();
186        m_ghost_object->setCollisionShape(m_collision_shape);
187        m_collision_object = m_ghost_object;
188
189        SetTransform(m_local_to_world.v3.xyz, lol::quat(m_local_to_world));
190
191        m_ghost_object->setCollisionFlags(m_ghost_object->getCollisionFlags());
192}
193
194//Add Physic object to the simulation
195void EasyPhysic::AddToSimulation(class Simulation* current_simulation)
196{
197        btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
198        if (dynamics_world)
199        {
200                if (m_ghost_object)
201                {
202                        dynamics_world->addCollisionObject(m_ghost_object, m_collision_group, m_collision_mask);
203                        current_simulation->AddToGhost(this);
204                }
205                else if (m_rigid_body)
206                {
207                        dynamics_world->addRigidBody(m_rigid_body, m_collision_group, m_collision_mask);
208                        if (m_mass != .0f)
209                                current_simulation->AddToDynamic(this);
210                        else
211                                current_simulation->AddToStatic(this);
212                }
213                else
214                        dynamics_world->addCollisionObject(m_collision_object, m_collision_group, m_collision_mask);
215        }
216}
217
218//Remove Physic object to the simulation
219void EasyPhysic::RemoveFromSimulation(class Simulation* current_simulation)
220{
221        btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
222        if (dynamics_world)
223        {
224                if (m_rigid_body)
225                        dynamics_world->removeRigidBody(m_rigid_body);
226                else if (m_collision_object)
227                        dynamics_world->removeCollisionObject(m_collision_object);
228        }
229}
230
231//-------------------------------------------------------------------------
232//Getter functons
233//--
234
235mat4 EasyPhysic::GetTransform()
236{
237        m_local_to_world = lol::mat4(1.0f);
238        if (m_rigid_body && m_motion_state)
239        {
240                btTransform CurTransform;
241                m_motion_state->getWorldTransform(CurTransform);
242                CurTransform.getOpenGLMatrix(&m_local_to_world[0][0]);
243        }
244        else if (m_collision_object)
245                m_collision_object->getWorldTransform().getOpenGLMatrix(&m_local_to_world[0][0]);
246        return m_local_to_world;
247}
248
249//Set Local Inertia
250void EasyPhysic::SetLocalInertia(float mass)
251{
252        if (mass != .0f)
253                m_collision_shape->calculateLocalInertia(mass, m_local_inertia);
254        else
255                m_local_inertia = btVector3(.0f, .0f, .0f);
256}
257
258#endif // HAVE_PHYS_USE_BULLET
259
260} /* namespace phys */
261
262} /* namespace lol */
Note: See TracBrowser for help on using the repository browser.