diff options
Diffstat (limited to 'src/components.cpp')
| -rw-r--r-- | src/components.cpp | 62 |
1 files changed, 34 insertions, 28 deletions
| diff --git a/src/components.cpp b/src/components.cpp index ad0f501..cf82979 100644 --- a/src/components.cpp +++ b/src/components.cpp | |||
| @@ -73,10 +73,10 @@ void PhysicsBodyComponent::receive(Game&, Entity&, const Message& msg) | |||
| 73 | { | 73 | { |
| 74 | if (msg.type == Message::Type::walkLeft) | 74 | if (msg.type == Message::Type::walkLeft) |
| 75 | { | 75 | { |
| 76 | velocity.first = -1.5; | 76 | velocity.first = -90; |
| 77 | } else if (msg.type == Message::Type::walkRight) | 77 | } else if (msg.type == Message::Type::walkRight) |
| 78 | { | 78 | { |
| 79 | velocity.first = 1.5; | 79 | velocity.first = 90; |
| 80 | } else if (msg.type == Message::Type::stopWalking) | 80 | } else if (msg.type == Message::Type::stopWalking) |
| 81 | { | 81 | { |
| 82 | velocity.first = 0.0; | 82 | velocity.first = 0.0; |
| @@ -89,13 +89,30 @@ void PhysicsBodyComponent::receive(Game&, Entity&, const Message& msg) | |||
| 89 | } | 89 | } |
| 90 | } | 90 | } |
| 91 | 91 | ||
| 92 | void PhysicsBodyComponent::tick(Game&, Entity& entity) | 92 | void PhysicsBodyComponent::tick(Game&, Entity& entity, double dt) |
| 93 | { | 93 | { |
| 94 | velocity.first += accel.first; | 94 | // RK4 integration |
| 95 | velocity.second += accel.second; | 95 | auto a = std::make_pair(velocity.first, velocity.second); |
| 96 | auto b = std::make_pair(velocity.first + a.first*dt*0.5, velocity.second + a.second*dt*0.5); | ||
| 97 | auto c = std::make_pair(velocity.first + b.first*dt*0.5, velocity.second + b.second*dt*0.5); | ||
| 98 | auto d = std::make_pair(velocity.first + c.first*dt, velocity.second + c.second*dt); | ||
| 96 | 99 | ||
| 97 | entity.position.first += velocity.first; | 100 | double dxxdt = 1.0 / 6.0 * (a.first + 2.0*(b.first + c.first) + d.first); |
| 98 | entity.position.second += velocity.second; | 101 | double dxydt = 1.0 / 6.0 * (a.second + 2.0*(b.second + c.second) + d.second); |
| 102 | |||
| 103 | entity.position.first += dxxdt * dt; | ||
| 104 | entity.position.second += dxydt * dt; | ||
| 105 | |||
| 106 | velocity.first += accel.first * dt; | ||
| 107 | velocity.second += accel.second * dt; | ||
| 108 | |||
| 109 | // Terminal velocity | ||
| 110 | #define TERMINAL_VELOCITY_X (2 * TILE_WIDTH * FRAMES_PER_SECOND) | ||
| 111 | #define TERMINAL_VELOCITY_Y (2 * TILE_HEIGHT * FRAMES_PER_SECOND) | ||
| 112 | if (velocity.first < -TERMINAL_VELOCITY_X) velocity.first = -TERMINAL_VELOCITY_X; | ||
| 113 | if (velocity.first > TERMINAL_VELOCITY_X) velocity.first = TERMINAL_VELOCITY_X; | ||
| 114 | if (velocity.second < -TERMINAL_VELOCITY_Y) velocity.second = -TERMINAL_VELOCITY_Y; | ||
| 115 | if (velocity.second > TERMINAL_VELOCITY_Y) velocity.second = TERMINAL_VELOCITY_Y; | ||
| 99 | } | 116 | } |
| 100 | 117 | ||
| 101 | void PhysicsBodyComponent::detectCollision(Game& game, Entity& entity, Entity& collider, std::pair<double, double> old_position) | 118 | void PhysicsBodyComponent::detectCollision(Game& game, Entity& entity, Entity& collider, std::pair<double, double> old_position) |
| @@ -192,9 +209,9 @@ void PlayerSpriteComponent::receive(Game&, Entity&, const Message& msg) | |||
| 192 | 209 | ||
| 193 | PlayerPhysicsComponent::PlayerPhysicsComponent() | 210 | PlayerPhysicsComponent::PlayerPhysicsComponent() |
| 194 | { | 211 | { |
| 195 | jump_velocity = JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3*FRAMES_PER_SECOND); | 212 | jump_velocity = JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3); |
| 196 | jump_gravity = JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3*FRAMES_PER_SECOND); | 213 | jump_gravity = JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3); |
| 197 | jump_gravity_short = JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233*FRAMES_PER_SECOND); | 214 | jump_gravity_short = JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233); |
| 198 | 215 | ||
| 199 | accel.second = jump_gravity_short; | 216 | accel.second = jump_gravity_short; |
| 200 | } | 217 | } |
| @@ -203,11 +220,11 @@ void PlayerPhysicsComponent::receive(Game&, Entity& entity, const Message& msg) | |||
| 203 | { | 220 | { |
| 204 | if (msg.type == Message::Type::walkLeft) | 221 | if (msg.type == Message::Type::walkLeft) |
| 205 | { | 222 | { |
| 206 | velocity.first = -1.5; | 223 | velocity.first = -90; |
| 207 | direction = -1; | 224 | direction = -1; |
| 208 | } else if (msg.type == Message::Type::walkRight) | 225 | } else if (msg.type == Message::Type::walkRight) |
| 209 | { | 226 | { |
| 210 | velocity.first = 1.5; | 227 | velocity.first = 90; |
| 211 | direction = 1; | 228 | direction = 1; |
| 212 | } else if (msg.type == Message::Type::stopWalking) | 229 | } else if (msg.type == Message::Type::stopWalking) |
| 213 | { | 230 | { |
| @@ -255,7 +272,7 @@ void PlayerPhysicsComponent::receive(Game&, Entity& entity, const Message& msg) | |||
| 255 | } | 272 | } |
| 256 | } | 273 | } |
| 257 | 274 | ||
| 258 | void PlayerPhysicsComponent::tick(Game& game, Entity& entity) | 275 | void PlayerPhysicsComponent::tick(Game& game, Entity& entity, double dt) |
| 259 | { | 276 | { |
| 260 | // If frozen, do nothing | 277 | // If frozen, do nothing |
| 261 | if (frozen) | 278 | if (frozen) |
| @@ -268,10 +285,10 @@ void PlayerPhysicsComponent::tick(Game& game, Entity& entity) | |||
| 268 | { | 285 | { |
| 269 | if (direction < 0) | 286 | if (direction < 0) |
| 270 | { | 287 | { |
| 271 | velocity.first = -1.5; | 288 | velocity.first = -90; |
| 272 | } else if (direction > 0) | 289 | } else if (direction > 0) |
| 273 | { | 290 | { |
| 274 | velocity.first = 1.5; | 291 | velocity.first = 90; |
| 275 | } | 292 | } |
| 276 | } | 293 | } |
| 277 | 294 | ||
| @@ -281,21 +298,10 @@ void PlayerPhysicsComponent::tick(Game& game, Entity& entity) | |||
| 281 | accel.second = jump_gravity_short; | 298 | accel.second = jump_gravity_short; |
| 282 | } | 299 | } |
| 283 | 300 | ||
| 284 | // Apply acceleration | ||
| 285 | velocity.first += accel.first; | ||
| 286 | velocity.second += accel.second; | ||
| 287 | |||
| 288 | // Terminal velocity | ||
| 289 | if (velocity.first < -16) velocity.first = -16; | ||
| 290 | if (velocity.first > 16) velocity.first = 16; | ||
| 291 | if (velocity.second < -16) velocity.second = -16; | ||
| 292 | if (velocity.second > 16) velocity.second = 16; | ||
| 293 | |||
| 294 | // Do the movement | 301 | // Do the movement |
| 295 | std::pair<double, double> old_position = entity.position; | 302 | std::pair<double, double> old_position = entity.position; |
| 296 | entity.position.first += velocity.first; | 303 | PhysicsBodyComponent::tick(game, entity, dt); |
| 297 | entity.position.second += velocity.second; | 304 | |
| 298 | |||
| 299 | // Check for collisions | 305 | // Check for collisions |
| 300 | game.detectCollision(entity, old_position); | 306 | game.detectCollision(entity, old_position); |
| 301 | } | 307 | } |
