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

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

EasyMesh : Fixed normals on disc when one of the radius == 0
EasyPhysics : Added Cone primitive.

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