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

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

BtPhysTest : Container box now moves.

File size: 4.9 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//
14// The EasyMesh class
15// ------------------
16//
17
18#if defined HAVE_CONFIG_H
19#   include "config.h"
20#endif
21
22#include "LolBtPhysicsIntegration.h"
23#include "LolPhysics.h"
24
25namespace lol
26{
27
28namespace phys
29{
30
31#ifdef HAVE_PHYS_USE_BULLET
32
33EasyPhysics::EasyPhysics() :
34        m_collision_object(NULL),
35        m_rigid_body(NULL),
36        m_collision_shape(NULL),
37        m_motion_state(NULL),
38        m_mass(.0f),
39        m_local_inertia(btVector3(.0f, .0f, .0f))
40{
41}
42
43EasyPhysics::~EasyPhysics()
44{
45        delete m_collision_object;
46        delete m_collision_shape;
47        delete m_motion_state;
48}
49
50//-------------------------------------------------------------------------
51//Set Shape functions
52//--
53
54void EasyPhysics::SetShapeTo(btCollisionShape* collision_shape)
55{
56        bool bReinitToRigidBody = false;
57        if (m_rigid_body)
58        {
59                bReinitToRigidBody = true;
60                delete m_rigid_body;
61        }
62        if (m_collision_shape)
63                delete m_collision_shape;
64
65        m_collision_shape = collision_shape;
66
67        if (bReinitToRigidBody)
68                InitBodyToRigid();
69}
70
71//Box Shape support
72void EasyPhysics::SetShapeToBox(lol::vec3& box_size)
73{
74        vec3 new_box_size = box_size * LOL2BT_UNIT * LOL2BT_SIZE;
75        SetShapeTo(new btBoxShape(LOL2BT_VEC3(new_box_size)));
76}
77
78void EasyPhysics::SetShapeToSphere(float radius)
79{
80        SetShapeTo(new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE));
81}
82
83void EasyPhysics::SetShapeToCone(float radius, float height)
84{
85        SetShapeTo(new btConeShape(     radius * LOL2BT_UNIT,
86                                                                height * LOL2BT_UNIT));
87}
88
89void EasyPhysics::SetShapeToCylinder(lol::vec3& cyl_size)
90{
91        vec3 new_cyl_size = cyl_size * LOL2BT_UNIT;
92        new_cyl_size.y *= LOL2BT_SIZE;
93        SetShapeTo(new btCylinderShape(LOL2BT_VEC3(new_cyl_size)));
94}
95
96void EasyPhysics::SetShapeToCapsule(float radius, float height)
97{
98        SetShapeTo(new btCapsuleShape(  radius * LOL2BT_UNIT * LOL2BT_SIZE,
99                                                                        height * LOL2BT_UNIT * LOL2BT_SIZE));
100}
101
102//-------------------------------------------------------------------------
103//Base Location/Rotation setup
104//--
105void EasyPhysics::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
106{
107        if (m_motion_state)
108        {
109                if (m_mass != .0f)
110                        m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
111                else
112                {
113                        m_rigid_body->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
114                        m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
115                }
116        }
117        else
118                m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
119}
120
121//-------------------------------------------------------------------------
122//Mass related functions
123//--
124//Set Shape functions
125void EasyPhysics::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, LOL2BT_VEC3(m_local_inertia));
133        }
134}
135
136//-------------------------------------------------------------------------
137//Final conversion pass functons : Body related
138//--
139
140//Init to rigid body
141void EasyPhysics::InitBodyToRigid()
142{
143        if (m_collision_object)
144                delete m_collision_object;
145
146        SetLocalInertia(m_mass);
147        if (!m_motion_state)
148                SetTransform(vec3(.0f));
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
154void EasyPhysics::AddToSimulation(class Simulation* current_simulation)
155{
156        btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
157        if (m_rigid_body)
158        {
159                dynamics_world->addRigidBody(m_rigid_body);
160                if (m_mass != .0f)
161                        current_simulation->AddToDynamic(this);
162                else
163                        current_simulation->AddToStatic(this);
164        }
165        else
166                dynamics_world->addCollisionObject(m_collision_object);
167}
168
169//-------------------------------------------------------------------------
170//Getter functons
171//--
172
173mat4 EasyPhysics::GetTransform()
174{
175        m_local_to_world = lol::mat4(1.0f);
176        if (m_rigid_body && m_motion_state)
177        {
178                btTransform CurTransform;
179                m_motion_state->getWorldTransform(CurTransform);
180                CurTransform.getOpenGLMatrix(&m_local_to_world[0][0]);
181        }
182        else if (m_collision_object)
183                m_collision_object->getWorldTransform().getOpenGLMatrix(&m_local_to_world[0][0]);
184        return m_local_to_world;
185}
186
187//Set Local Inertia
188void EasyPhysics::SetLocalInertia(float mass)
189{
190        if (mass != .0f)
191                m_collision_shape->calculateLocalInertia(mass, m_local_inertia);
192        else
193                m_local_inertia = btVector3(.0f, .0f, .0f);
194}
195#endif
196
197} /* namespace phys */
198
199} /* namespace lol */
Note: See TracBrowser for help on using the repository browser.