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

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

EasyPhysics : Added Capsules.

File size: 4.7 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::SetBaseTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
106{
107        if (m_motion_state)
108                m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
109        else
110                m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
111}
112
113//-------------------------------------------------------------------------
114//Mass related functions
115//--
116
117//Set Shape functions
118void EasyPhysics::SetMass(float mass)
119{
120        m_mass = mass;
121
122        if (m_rigid_body)
123        {
124                SetLocalInertia(m_mass);
125                m_rigid_body->setMassProps(mass, LOL2BT_VEC3(m_local_inertia));
126        }
127}
128
129//-------------------------------------------------------------------------
130//Final conversion pass functons : Body related
131//--
132
133//Init to rigid body
134void EasyPhysics::InitBodyToRigid()
135{
136        if (m_collision_object)
137                delete m_collision_object;
138
139        SetLocalInertia(m_mass);
140        if (!m_motion_state)
141                SetBaseTransform(vec3(.0f));
142        btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia);
143        m_rigid_body = new btRigidBody(NewInfos);
144        m_collision_object = m_rigid_body;
145}
146
147void EasyPhysics::AddToSimulation(class Simulation* current_simulation)
148{
149        btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
150        if (m_rigid_body)
151        {
152                dynamics_world->addRigidBody(m_rigid_body);
153                if (m_mass != .0f)
154                        current_simulation->AddToDynamic(this);
155                else
156                        current_simulation->AddToStatic(this);
157        }
158        else
159                dynamics_world->addCollisionObject(m_collision_object);
160}
161
162//-------------------------------------------------------------------------
163//Getter functons
164//--
165
166mat4 EasyPhysics::GetTransform()
167{
168        m_local_to_world = lol::mat4(1.0f);
169        if (m_rigid_body && m_motion_state)
170        {
171                btTransform CurTransform;
172                m_motion_state->getWorldTransform(CurTransform);
173                CurTransform.getOpenGLMatrix(&m_local_to_world[0][0]);
174        }
175        else if (m_collision_object)
176                m_collision_object->getWorldTransform().getOpenGLMatrix(&m_local_to_world[0][0]);
177        return m_local_to_world;
178}
179
180//Set Local Inertia
181void EasyPhysics::SetLocalInertia(float mass)
182{
183        if (mass != .0f)
184                m_collision_shape->calculateLocalInertia(mass, m_local_inertia);
185        else
186                m_local_inertia = btVector3(.0f, .0f, .0f);
187}
188#endif
189
190} /* namespace phys */
191
192} /* namespace lol */
Note: See TracBrowser for help on using the repository browser.