source: trunk/test/Physics/Src/EasyCharacterController.cpp @ 1779

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

Fixed stupid base_movement which was cached for ever.

File size: 3.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#if defined HAVE_CONFIG_H
14#   include "config.h"
15#endif
16
17#include "../Include/LolBtPhysicsIntegration.h"
18#include "../Include/LolPhysics.h"
19#include "../Include/EasyCharacterController.h"
20
21namespace lol
22{
23
24namespace phys
25{
26
27#ifdef HAVE_PHYS_USE_BULLET
28
29//-------------------------------------------------------------------------
30//EASY_CHARACTER_CONTROLLER
31//--
32
33//Deactivated for Character controller
34void EasyCharacterController::InitBodyToRigid(bool ZeroMassIsKinematic)
35{
36}
37
38//Return correct Ghost Object
39btGhostObject* EasyCharacterController::GetGhostObjectInstance()
40{
41        return new btPairCachingGhostObject();
42}
43
44//Init to Pair caching ghost object, since Character uses that one.
45void EasyCharacterController::InitBodyToGhost()
46{
47        EasyPhysic::InitBodyToGhost();
48
49        m_pair_caching_object = (btPairCachingGhostObject*)m_ghost_object;
50        m_ghost_object->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT | m_ghost_object->getCollisionFlags());
51}
52
53//Add Physic object to the simulation
54void EasyCharacterController::AddToSimulation(class Simulation* current_simulation)
55{
56        EasyPhysic::AddToSimulation(current_simulation);
57
58        btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
59        if (dynamics_world)
60        {
61                if (m_character)
62                        delete m_character;
63
64                m_character = new btKinematicCharacterController(m_pair_caching_object, m_convex_shape, m_step_height, m_up_axis);
65                dynamics_world->addAction(m_character);
66                Ticker::Ref(this);
67        }
68}
69
70//Remove Physic object to the simulation
71void EasyCharacterController::RemoveFromSimulation(class Simulation* current_simulation)
72{
73        EasyPhysic::RemoveFromSimulation(current_simulation);
74
75        btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
76        if (dynamics_world)
77        {
78                if (m_character)
79                {
80                        dynamics_world->removeAction(m_character);
81                        Ticker::Unref(this);
82                }
83        }
84}
85
86//Set movement for this frame
87void EasyCharacterController::SetMovementForFrame(vec3 const &MoveQuantity)
88{
89        m_frame_cached_movement = MoveQuantity;
90}
91
92//-------------------------------------------------------------------------
93//Base Location/Rotation setup
94//--
95void EasyCharacterController::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
96{
97        if (m_base_is_updating)
98        {
99                m_base_cached_movement = base_location - m_local_to_world.v3.xyz;
100                m_local_to_world = lol::mat4::translate(m_local_to_world.v3.xyz) * lol::mat4(base_rotation);
101                if (m_ghost_object)
102                        m_ghost_object->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * m_local_to_world.v3.xyz)));
103        }
104        else
105                EasyPhysic::SetTransform(base_location, base_rotation);
106}
107
108//Internal callback when Base transform has changed.
109void EasyCharacterController::BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix)
110{
111        m_base_is_updating = true;
112        EasyPhysic::BaseTransformChanged(PreviousMatrix, NewMatrix);
113        m_base_is_updating = false;
114}
115
116//Physic Tick
117void EasyCharacterController::TickGame(float seconds)
118{
119        Entity::TickGame(seconds);
120
121        m_character->setVelocityForTimeInterval(LOL2BT_VEC3(LOL2BT_UNIT * (m_base_cached_movement + m_frame_cached_movement)) / seconds, seconds);
122        m_base_cached_movement = vec3(.0f);
123}
124
125#endif // HAVE_PHYS_USE_BULLET
126
127} /* namespace phys */
128
129} /* namespace lol */
Note: See TracBrowser for help on using the repository browser.