diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-05-17 10:36:59 +0100 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-05-17 10:36:59 +0100 |
commit | 7583ad924479b11bbd45122194b53a7b6c8f7a0e (patch) | |
tree | f454120b7bd2f5cc344eb221f975321842980a13 /src/rigidbody.cpp | |
parent | f7fc06b5066018451b6b456be0dbc46bafe2fd88 (diff) | |
download | YAGE-7583ad924479b11bbd45122194b53a7b6c8f7a0e.tar.gz YAGE-7583ad924479b11bbd45122194b53a7b6c8f7a0e.zip |
Adding files
Diffstat (limited to 'src/rigidbody.cpp')
-rw-r--r-- | src/rigidbody.cpp | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/src/rigidbody.cpp b/src/rigidbody.cpp index 4d774937..d8b963de 100644 --- a/src/rigidbody.cpp +++ b/src/rigidbody.cpp @@ -9,15 +9,29 @@ RigidBody::RigidBody(const glm::vec2 ¢er_of_mass, double mass, const glm::ve void RigidBody::applyForce(const glm::vec2 &force) { + force_+=force; +} + +void RigidBody::update() +{ // set the time_step for 60fps double time_step=1.0/60.0; - - // a=F/m - glm::vec2 acceleration((force_.x+force.x)/mass_, (force_.y+force.y)/mass_); // = a(t0) - glm::vec2 intermediate_velocity=acceleration+glm::vec2(acceleration.x*time_step/2, acceleration.y*time_step/2); + // set the last acceleration + glm::vec2 last_acceleration=acceleration_; + + // update the position of the body + center_of_mass_.x+=velocity_.x*time_step+(0.5*last_acceleration.x*time_step*time_step); + center_of_mass_.y+=velocity_.y*time_step+(0.5*last_acceleration.y*time_step*time_step); + + // update the acceleration + acceleration_=glm::vec2(force_.x/mass_, force_.y/mass_); + glm::vec2 avg_acceleration=glm::vec2((acceleration_.x+last_acceleration.x)/2, + (acceleration_.y+last_acceleration.y)/2); - velocity_=acceleration+glm::vec2(intermediate_velocity.x*time_step, intermediate_velocity.y*time_step); + // update the velocity of the body + velocity_.x+=avg_acceleration.x*time_step; + velocity_.y+=avg_acceleration.y*time_step; } } // yage |