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

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

CIS FIX

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