[1747] | 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 | |
---|
[1749] | 17 | #include "../Include/LolBtPhysicsIntegration.h" |
---|
| 18 | #include "../Include/LolPhysics.h" |
---|
[1747] | 19 | #include "../Include/EasyCharacterController.h" |
---|
| 20 | |
---|
| 21 | namespace lol |
---|
| 22 | { |
---|
| 23 | |
---|
| 24 | namespace phys |
---|
| 25 | { |
---|
| 26 | |
---|
| 27 | #ifdef HAVE_PHYS_USE_BULLET |
---|
| 28 | |
---|
| 29 | //------------------------------------------------------------------------- |
---|
| 30 | //EASY_CHARACTER_CONTROLLER |
---|
| 31 | //-- |
---|
| 32 | |
---|
| 33 | //Deactivated for Character controller |
---|
| 34 | void EasyCharacterController::InitBodyToRigid(bool ZeroMassIsKinematic) |
---|
| 35 | { |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | //Return correct Ghost Object |
---|
[1768] | 39 | btGhostObject* EasyCharacterController::GetGhostObjectInstance() |
---|
[1747] | 40 | { |
---|
| 41 | return new btPairCachingGhostObject(); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | //Init to Pair caching ghost object, since Character uses that one. |
---|
| 45 | void 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 |
---|
| 54 | void 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);
|
---|
[1768] | 66 | Ticker::Ref(this);
|
---|
[1747] | 67 | }
|
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | //Remove Physic object to the simulation |
---|
| 71 | void 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) |
---|
[1768] | 79 | { |
---|
[1747] | 80 | dynamics_world->removeAction(m_character); |
---|
[1768] | 81 | Ticker::Unref(this);
|
---|
| 82 | } |
---|
[1747] | 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | //Set movement for this frame |
---|
| 87 | void EasyCharacterController::SetMovementForFrame(vec3 const &MoveQuantity)
|
---|
| 88 | {
|
---|
[1768] | 89 | m_frame_cached_movement = MoveQuantity;
|
---|
[1747] | 90 | }
|
---|
| 91 | |
---|
[1768] | 92 | //------------------------------------------------------------------------- |
---|
| 93 | //Base Location/Rotation setup |
---|
| 94 | //-- |
---|
| 95 | void 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. |
---|
| 109 | void 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 |
---|
| 117 | void EasyCharacterController::TickGame(float seconds) |
---|
| 118 | { |
---|
| 119 | Entity::TickGame(seconds); |
---|
| 120 | |
---|
| 121 | m_character->setVelocityForTimeInterval(LOL2BT_VEC3(LOL2BT_UNIT * /*0.1f **/ (m_base_cached_movement + m_frame_cached_movement)) / seconds, seconds);
|
---|
| 122 | //m_character->setWalkDirection();
|
---|
| 123 | } |
---|
| 124 | |
---|
[1747] | 125 | #endif // HAVE_PHYS_USE_BULLET |
---|
| 126 | |
---|
| 127 | } /* namespace phys */ |
---|
| 128 | |
---|
| 129 | } /* namespace lol */ |
---|