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

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

Small name refactor & correct Kinematic integration.
+ Constraint hollow class.

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