From 3b2e4639de929e640fe672e0d77e0faef3acc027 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Tue, 27 Jun 2017 10:53:12 +0100 Subject: Implementing Vector2d --- include/YAGE/Math/matrix.hpp | 36 +++++++++++++++++++++++++++++++++++ include/YAGE/Physics/body.hpp | 18 +++++++++--------- include/YAGE/Physics/particlebody.hpp | 8 ++++---- src/.#particlebody.cpp | 1 + src/particlebody.cpp | 24 +++++++++++------------ test/matrixtest.cpp | 4 ++-- 6 files changed, 64 insertions(+), 27 deletions(-) create mode 120000 src/.#particlebody.cpp diff --git a/include/YAGE/Math/matrix.hpp b/include/YAGE/Math/matrix.hpp index d1c5fd74..238f7d09 100644 --- a/include/YAGE/Math/matrix.hpp +++ b/include/YAGE/Math/matrix.hpp @@ -235,6 +235,36 @@ Matrix operator-(const T &lhs, Matrix rhs) return rhs; } +template +Matrix operator*(Matrix lhs, const T &rhs) +{ + for(auto &data : lhs) + { + data*=rhs; + } + return lhs; +} + +template +Matrix operator*(const T &lhs, Matrix rhs) +{ + for(auto &data : rhs) + { + data*=lhs; + } + return rhs; +} + +template +Matrix operator/(Matrix lhs, const T &rhs) +{ + for(auto &data : lhs) + { + data/=rhs; + } + return lhs; +} + template bool operator==(const Matrix &lhs, const Matrix &rhs) { @@ -280,6 +310,12 @@ template class Vector2 : public Vector<2, Type> { public: Vector2() : Vector<2, Type>() {} + Vector2(Type x, Type y) + { + this->data_[0]=x; + this->data_[1]=y; + } + Vector2(const Matrix<2, 1, Type> &other) : Vector<2, Type>(other) {} Type& x() { diff --git a/include/YAGE/Physics/body.hpp b/include/YAGE/Physics/body.hpp index 54fdeb3f..f09cd5d5 100644 --- a/include/YAGE/Physics/body.hpp +++ b/include/YAGE/Physics/body.hpp @@ -1,7 +1,7 @@ #ifndef YAGE_BODY_HPP #define YAGE_BODY_HPP -#include +#include "Math/matrix.hpp" namespace yage { @@ -13,37 +13,37 @@ public: static const double GRAVITY; protected: // center of mass of the object - glm::vec2 position_=glm::vec2(0.f, 0.f); + Vector2d position_=Vector2d(0, 0); // mass of the object - double mass_=1.0; + double mass_=1; // current velocity of the object - glm::vec2 velocity_=glm::vec2(0.f, 0.f); + Vector2d velocity_=Vector2d(0.f, 0.f); // boolean that defines if gravity can act on the object bool gravity_=true; // current acceleration - glm::vec2 acceleration_=glm::vec2(0.f, 0.f); + Vector2d acceleration_=Vector2d(0, 0); // force acting on the body - glm::vec2 force_=glm::vec2(0.f, 0.f); + Vector2d force_=Vector2d(0, 0); public: virtual ~Body(); // apply force to the object and update the velocity - virtual void applyForce(const glm::vec2 &force)=0; + virtual void applyForce(const Vector2d &force)=0; virtual void update()=0; float xPosition() const; float yPosition() const; protected: // protected constructor to initialize member variables - Body(const glm::vec2 &position=glm::vec2(0.f, 0.f), + Body(const Vector2d &position=Vector2d(0.f, 0.f), double mass=1.0, - const glm::vec2 &velocity=glm::vec2(0.f, 0.f), + const Vector2d &velocity=Vector2d(0.f, 0.f), bool gravity=false); }; diff --git a/include/YAGE/Physics/particlebody.hpp b/include/YAGE/Physics/particlebody.hpp index d9d5e08c..09ad8c7b 100644 --- a/include/YAGE/Physics/particlebody.hpp +++ b/include/YAGE/Physics/particlebody.hpp @@ -1,7 +1,7 @@ #ifndef YAGE_PARTICLE_BODY_HPP #define YAGE_PARTICLE_BODY_HPP -#include +#include "Math/matrix.hpp" #include "body.hpp" @@ -11,13 +11,13 @@ namespace yage class ParticleBody : public Body { public: - ParticleBody(const glm::vec2 &position=glm::vec2(0.f, 0.f), + ParticleBody(const Vector2d &position=Vector2d(0.f, 0.f), double mass=1.0, - const glm::vec2 &velocity=glm::vec2(0.f, 0.f), + const Vector2d &velocity=Vector2d(0.f, 0.f), bool gravity=true); // apply a force to the rigid body - virtual void applyForce(const glm::vec2 &force); + virtual void applyForce(const Vector2d &force); virtual void update(); }; diff --git a/src/.#particlebody.cpp b/src/.#particlebody.cpp new file mode 120000 index 00000000..dc323bf2 --- /dev/null +++ b/src/.#particlebody.cpp @@ -0,0 +1 @@ +yannherklotz@yann-arch.17879:1498487609 \ No newline at end of file diff --git a/src/particlebody.cpp b/src/particlebody.cpp index 2c463623..f882f279 100644 --- a/src/particlebody.cpp +++ b/src/particlebody.cpp @@ -5,14 +5,14 @@ namespace yage { -ParticleBody::ParticleBody(const glm::vec2 &position, +ParticleBody::ParticleBody(const Vector2d &position, double mass, - const glm::vec2 &velocity, + const Vector2d &velocity, bool gravity) : Body(position, mass, velocity, gravity) {} -void ParticleBody::applyForce(const glm::vec2 &force) +void ParticleBody::applyForce(const Vector2d &force) { force_+=force; } @@ -23,24 +23,24 @@ void ParticleBody::update() double time_step=1.0/60.0; // set the last acceleration - glm::vec2 last_acceleration=acceleration_; + Vector2d 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)); + //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)); + + position_+=velocity_*time_step+(0.5*last_acceleration*std::pow(time_step, 2)); // update the acceleration if(gravity_) - acceleration_=glm::vec2(force_.x/mass_, (GRAVITY+force_.y)/mass_); + acceleration_=Vector2d(force_.x()/mass_, (GRAVITY+force_.y())/mass_); else - acceleration_=glm::vec2(force_.x/mass_, force_.y/mass_); + acceleration_=Vector2d(force_.x()/mass_, force_.y()/mass_); - glm::vec2 avg_acceleration=glm::vec2((acceleration_.x+last_acceleration.x)/2, - (acceleration_.y+last_acceleration.y)/2); + Vector2d avg_acceleration=(acceleration_+last_acceleration)/2.0; // update the velocity of the body - velocity_.x+=avg_acceleration.x*time_step; - velocity_.y+=avg_acceleration.y*time_step; + velocity_=avg_acceleration*time_step; } } // yage diff --git a/test/matrixtest.cpp b/test/matrixtest.cpp index ce4e9187..8ee350f6 100644 --- a/test/matrixtest.cpp +++ b/test/matrixtest.cpp @@ -25,7 +25,7 @@ bool matrixAssign() yage::Matrix<4, 5, int> m; m[2][3]=5; - return m[2][3]==4; + return m[2][3]==5; } bool matrixAddition() @@ -63,7 +63,7 @@ int main() { TestBench tb; - bool all_passed=true; + bool all_passed=false; try { -- cgit