1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net> |
---|
5 | // (c) 2009-2013 Cédric Lecacheur <jordx@free.fr> |
---|
6 | // (c) 2009-2013 Benjamin "Touky" 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://www.wtfpl.net/ 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 | |
---|
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 |
---|
39 | btGhostObject* EasyCharacterController::GetGhostObjectInstance() |
---|
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 | 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 |
---|
78 | void 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 | |
---|
94 | void EasyCharacterController::Jump() |
---|
95 | { |
---|
96 | m_character->Jump(); |
---|
97 | } |
---|
98 | |
---|
99 | //Set movement for this frame |
---|
100 | void EasyCharacterController::SetMovementForFrame(vec3 const &MoveQuantity) |
---|
101 | { |
---|
102 | m_frame_cached_movement = MoveQuantity; |
---|
103 | } |
---|
104 | |
---|
105 | //------------------------------------------------------------------------- |
---|
106 | //Base Location/Rotation setup |
---|
107 | //-- |
---|
108 | void 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. |
---|
122 | void 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 | //--- |
---|
130 | char const *EasyCharacterController::GetName() |
---|
131 | { |
---|
132 | return "<EasyCharacterController>"; |
---|
133 | } |
---|
134 | |
---|
135 | //Physic Tick |
---|
136 | void 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 */ |
---|