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

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

Added Collision group & mask support.
+ weird racist test : the primitives only collide with their same type.

File size: 5.3 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        m_collision_group(1),
41        m_collision_mask(1)
42{
43}
44
45EasyPhysics::~EasyPhysics()
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 EasyPhysics::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 EasyPhysics::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 EasyPhysics::SetShapeToSphere(float radius)
82{
83        SetShapeTo(new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE));
84}
85
86void EasyPhysics::SetShapeToCone(float radius, float height)
87{
88        SetShapeTo(new btConeShape(     radius * LOL2BT_UNIT,
89                                                                height * LOL2BT_UNIT));
90}
91
92void EasyPhysics::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 EasyPhysics::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//Bullet collision channel setup
107//--
108void EasyPhysics::CustomSetCollisionChannel(int NewGroup, int NewMask)
109{
110        if (m_rigid_body)
111                m_rigid_body->setCollisionFlags(m_collision_mask);
112}
113
114//-------------------------------------------------------------------------
115//Base Location/Rotation setup
116//--
117void EasyPhysics::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
118{
119        if (m_motion_state)
120        {
121                if (m_mass != .0f)
122                        m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
123                else
124                {
125                        m_rigid_body->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
126                        m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
127                }
128        }
129        else
130                m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
131}
132
133//-------------------------------------------------------------------------
134//Mass related functions
135//--
136//Set Shape functions
137void EasyPhysics::SetMass(float mass)
138{
139        m_mass = mass;
140
141        if (m_rigid_body)
142        {
143                SetLocalInertia(m_mass);
144                m_rigid_body->setMassProps(mass, m_local_inertia);
145        }
146}
147
148//-------------------------------------------------------------------------
149//Final conversion pass functons : Body related
150//--
151
152//Init to rigid body
153void EasyPhysics::InitBodyToRigid()
154{
155        if (m_collision_object)
156                delete m_collision_object;
157
158        SetLocalInertia(m_mass);
159        if (!m_motion_state)
160                SetTransform(vec3(.0f));
161        btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia);
162        m_rigid_body = new btRigidBody(NewInfos);
163        m_collision_object = m_rigid_body;
164}
165
166void EasyPhysics::AddToSimulation(class Simulation* current_simulation)
167{
168        btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
169        if (m_rigid_body)
170        {
171                dynamics_world->addRigidBody(m_rigid_body, m_collision_group, m_collision_mask);
172                if (m_mass != .0f)
173                        current_simulation->AddToDynamic(this);
174                else
175                        current_simulation->AddToStatic(this);
176        }
177        else
178                dynamics_world->addCollisionObject(m_collision_object, m_collision_group, m_collision_mask);
179}
180
181//-------------------------------------------------------------------------
182//Getter functons
183//--
184
185mat4 EasyPhysics::GetTransform()
186{
187        m_local_to_world = lol::mat4(1.0f);
188        if (m_rigid_body && m_motion_state)
189        {
190                btTransform CurTransform;
191                m_motion_state->getWorldTransform(CurTransform);
192                CurTransform.getOpenGLMatrix(&m_local_to_world[0][0]);
193        }
194        else if (m_collision_object)
195                m_collision_object->getWorldTransform().getOpenGLMatrix(&m_local_to_world[0][0]);
196        return m_local_to_world;
197}
198
199//Set Local Inertia
200void EasyPhysics::SetLocalInertia(float mass)
201{
202        if (mass != .0f)
203                m_collision_shape->calculateLocalInertia(mass, m_local_inertia);
204        else
205                m_local_inertia = btVector3(.0f, .0f, .0f);
206}
207#endif
208
209} /* namespace phys */
210
211} /* namespace lol */
Note: See TracBrowser for help on using the repository browser.