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

Last change on this file since 1633 was 1633, checked in by touky, 10 years ago

Correct integration of EasyConstraint + TestDemo.

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