diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-05-19 20:08:23 +0100 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-05-19 20:08:23 +0100 |
commit | 3308d13ad04fac43e7a111b299ff9444aea4ab9f (patch) | |
tree | 54a91113f75e506318101acf3151d3b11d309e96 /src | |
parent | e15b2c6dd405d008d9e892608c209fd980bcd8ff (diff) | |
download | YAGE-3308d13ad04fac43e7a111b299ff9444aea4ab9f.tar.gz YAGE-3308d13ad04fac43e7a111b299ff9444aea4ab9f.zip |
Adding math to yage
Diffstat (limited to 'src')
-rw-r--r-- | src/body.cpp | 15 | ||||
-rw-r--r-- | src/particlebody.cpp | 46 | ||||
-rw-r--r-- | src/rigidbody.cpp | 34 | ||||
-rw-r--r-- | src/vector2d.cpp | 20 |
4 files changed, 79 insertions, 36 deletions
diff --git a/src/body.cpp b/src/body.cpp index fb6c40e7..23360988 100644 --- a/src/body.cpp +++ b/src/body.cpp @@ -3,22 +3,23 @@ namespace yage { -Body::~Body() -{} +const double Body::GRAVITY=-9.81; -Body::Body(const glm::vec2 ¢er_of_mass, double mass, const glm::vec2 &force, const glm::vec2 &velocity, bool gravity) : - force_(force), velocity_(velocity), center_of_mass_(center_of_mass), - mass_(mass), gravity_(gravity) +Body::~Body() {} float Body::xPosition() const { - return center_of_mass_.x; + return position_.x; } float Body::yPosition() const { - return center_of_mass_.y; + return position_.y; } +Body::Body(const glm::vec2 &position, double mass, const glm::vec2 &velocity, bool gravity) : + position_(position), mass_(mass), velocity_(velocity), gravity_(gravity) +{} + } // yage diff --git a/src/particlebody.cpp b/src/particlebody.cpp new file mode 100644 index 00000000..2c463623 --- /dev/null +++ b/src/particlebody.cpp @@ -0,0 +1,46 @@ +#include "Physics/particlebody.hpp" + +#include <cmath> + +namespace yage +{ + +ParticleBody::ParticleBody(const glm::vec2 &position, + double mass, + const glm::vec2 &velocity, + bool gravity) : + Body(position, mass, velocity, gravity) +{} + +void ParticleBody::applyForce(const glm::vec2 &force) +{ + force_+=force; +} + +void ParticleBody::update() +{ + // set the time_step for 60fps + double time_step=1.0/60.0; + + // set the last acceleration + glm::vec2 last_acceleration=acceleration_; + + // update the position of the body + position_.x+=velocity_.x*time_step+(0.5*last_acceleration.x*std::pow(time_step, 2)); + position_.y+=velocity_.y*time_step+(0.5*last_acceleration.y*std::pow(time_step, 2)); + + // update the acceleration + if(gravity_) + acceleration_=glm::vec2(force_.x/mass_, (GRAVITY+force_.y)/mass_); + else + 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); + + // update the velocity of the body + velocity_.x+=avg_acceleration.x*time_step; + velocity_.y+=avg_acceleration.y*time_step; +} + +} // yage diff --git a/src/rigidbody.cpp b/src/rigidbody.cpp index d8b963de..f92500b8 100644 --- a/src/rigidbody.cpp +++ b/src/rigidbody.cpp @@ -3,35 +3,11 @@ namespace yage { -RigidBody::RigidBody(const glm::vec2 ¢er_of_mass, double mass, const glm::vec2 &force, const glm::vec2 &velocity, bool gravity) : - Body(center_of_mass, mass, force, velocity, gravity) +RigidBody::RigidBody(const glm::vec2 &position, + double mass, + const glm::vec2 &velocity, + bool gravity) : + ParticleBody(position, mass, velocity, gravity) {} -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; - - // 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); - - // update the velocity of the body - velocity_.x+=avg_acceleration.x*time_step; - velocity_.y+=avg_acceleration.y*time_step; -} - } // yage diff --git a/src/vector2d.cpp b/src/vector2d.cpp new file mode 100644 index 00000000..88e637c6 --- /dev/null +++ b/src/vector2d.cpp @@ -0,0 +1,20 @@ +#include "Math/vector2d.hpp" + +namespace yage +{ + +Vector2D::Vector2D(long double x, long double y) : + Vector({ x, y }) +{} + +long double Vector2D::getX() const +{ + return (*this)[0]; +} + +long double Vector2D::getY() const +{ + return (*this)[1]; +} + +} // yage |