summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2015-03-13 11:50:38 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2015-03-13 11:50:38 -0400
commit9cf2d327476d00b696254b170c03dd6b8548e1d0 (patch)
tree0bc00066884c3489321bc568b5aa0b352ef1b3de
parentb8d62ce8d2f7c8f38aa9c52ab8d7dd32dc3aba64 (diff)
downloadtherapy-9cf2d327476d00b696254b170c03dd6b8548e1d0.tar.gz
therapy-9cf2d327476d00b696254b170c03dd6b8548e1d0.tar.bz2
therapy-9cf2d327476d00b696254b170c03dd6b8548e1d0.zip
RK4 integration is not necessary for constant acceleration over a timestep
-rw-r--r--src/components.cpp17
1 files changed, 5 insertions, 12 deletions
diff --git a/src/components.cpp b/src/components.cpp index cf82979..5f67934 100644 --- a/src/components.cpp +++ b/src/components.cpp
@@ -91,18 +91,7 @@ void PhysicsBodyComponent::receive(Game&, Entity&, const Message& msg)
91 91
92void PhysicsBodyComponent::tick(Game&, Entity& entity, double dt) 92void PhysicsBodyComponent::tick(Game&, Entity& entity, double dt)
93{ 93{
94 // RK4 integration 94 // Accelerate
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);
99
100 double dxxdt = 1.0 / 6.0 * (a.first + 2.0*(b.first + c.first) + d.first);
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; 95 velocity.first += accel.first * dt;
107 velocity.second += accel.second * dt; 96 velocity.second += accel.second * dt;
108 97
@@ -113,6 +102,10 @@ void PhysicsBodyComponent::tick(Game&, Entity& entity, double dt)
113 if (velocity.first > TERMINAL_VELOCITY_X) velocity.first = TERMINAL_VELOCITY_X; 102 if (velocity.first > TERMINAL_VELOCITY_X) velocity.first = TERMINAL_VELOCITY_X;
114 if (velocity.second < -TERMINAL_VELOCITY_Y) velocity.second = -TERMINAL_VELOCITY_Y; 103 if (velocity.second < -TERMINAL_VELOCITY_Y) velocity.second = -TERMINAL_VELOCITY_Y;
115 if (velocity.second > TERMINAL_VELOCITY_Y) velocity.second = TERMINAL_VELOCITY_Y; 104 if (velocity.second > TERMINAL_VELOCITY_Y) velocity.second = TERMINAL_VELOCITY_Y;
105
106 // Do the movement
107 entity.position.first += velocity.first * dt;
108 entity.position.second += velocity.second * dt;
116} 109}
117 110
118void PhysicsBodyComponent::detectCollision(Game& game, Entity& entity, Entity& collider, std::pair<double, double> old_position) 111void PhysicsBodyComponent::detectCollision(Game& game, Entity& entity, Entity& collider, std::pair<double, double> old_position)