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

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

Small refactor of the Phys::Simulation.
Phys::Simulation is now an entity.
Phys::Simulation now implements timesteps correctly.

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