source: trunk/test/Physics/EasyPhysics.cpp @ 1708

Last change on this file since 1708 was 1708, checked in by Jnat, 9 years ago

added return

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