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

Last change on this file since 1888 was 1888, checked in by lolbot, 7 years ago

fixed 23 files out of 277:

  • fixed 1270 CR characters
  • fixed 56 trailing spaces
  • fixed 5085 tabs
File size: 4.6 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        m_character = new BulletKinematicCharacterController(m_pair_caching_object, m_convex_shape, m_step_height, m_up_axis);
66
67        //Deactivate Character controller basic behaviour.
68        //m_character->setGravity(.0f);
69        //m_character->setFallSpeed(.0f);
70
71        dynamics_world->addAction(m_character);
72        current_simulation->ObjectRegistration(true, this, Simulation::EEPT_CharacterController);
73        Ticker::Ref(this);
74    }
75}
76
77//Remove Physic object to the simulation
78void EasyCharacterController::RemoveFromSimulation(class Simulation* current_simulation)
79{
80    EasyPhysic::RemoveFromSimulation(current_simulation);
81
82    btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
83    if (dynamics_world)
84    {
85        if (m_character)
86        {
87            dynamics_world->removeAction(m_character);
88            current_simulation->ObjectRegistration(false, this, Simulation::EEPT_CharacterController);
89            Ticker::Unref(this);
90        }
91    }
92}
93
94void EasyCharacterController::Jump()
95{
96    m_character->Jump();
97}
98
99//Set movement for this frame
100void EasyCharacterController::SetMovementForFrame(vec3 const &MoveQuantity)
101{
102    m_frame_cached_movement = MoveQuantity;
103}
104
105//-------------------------------------------------------------------------
106//Base Location/Rotation setup
107//--
108void EasyCharacterController::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
109{
110    if (m_base_is_updating)
111    {
112        m_base_cached_movement = base_location - m_local_to_world.v3.xyz;
113        m_local_to_world = lol::mat4::translate(m_local_to_world.v3.xyz) * lol::mat4(base_rotation);
114        if (m_ghost_object)
115            m_ghost_object->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(LOL2BT_UNIT * m_local_to_world.v3.xyz)));
116    }
117    else
118        EasyPhysic::SetTransform(base_location, base_rotation);
119}
120
121//Internal callback when Base transform has changed.
122void EasyCharacterController::BaseTransformChanged(const lol::mat4& PreviousMatrix, const lol::mat4& NewMatrix)
123{
124    m_base_is_updating = true;
125    EasyPhysic::BaseTransformChanged(PreviousMatrix, NewMatrix);
126    m_base_is_updating = false;
127}
128
129//---
130char const *EasyCharacterController::GetName()
131{
132    return "<EasyCharacterController>";
133}
134
135//Physic Tick
136void EasyCharacterController::TickGame(float seconds)
137{
138    Entity::TickGame(seconds);
139
140    //Send final velocity in Bullet
141    {
142        int IterationsNb = (int)(seconds / m_owner_simulation->m_timestep);
143        float NewSeconds = IterationsNb * m_owner_simulation->m_timestep;
144        m_character->SetVelocityForTimeInterval((m_base_cached_movement + m_frame_cached_movement) / NewSeconds, NewSeconds);
145        m_base_cached_movement = vec3(.0f);
146    }
147}
148
149#endif // HAVE_PHYS_USE_BULLET
150
151} /* namespace phys */
152
153} /* namespace lol */
Note: See TracBrowser for help on using the repository browser.