From 3308d13ad04fac43e7a111b299ff9444aea4ab9f Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 19 May 2017 20:08:23 +0100 Subject: Adding math to yage --- include/YAGE/Math/vector.hpp | 133 ++++++++++++++++++++++++++++++++++ include/YAGE/Math/vector2d.hpp | 20 +++++ include/YAGE/Physics/body.hpp | 34 +++++---- include/YAGE/Physics/particlebody.hpp | 26 +++++++ include/YAGE/Physics/rigidbody.hpp | 17 ++--- 5 files changed, 206 insertions(+), 24 deletions(-) create mode 100644 include/YAGE/Math/vector.hpp create mode 100644 include/YAGE/Math/vector2d.hpp create mode 100644 include/YAGE/Physics/particlebody.hpp (limited to 'include') diff --git a/include/YAGE/Math/vector.hpp b/include/YAGE/Math/vector.hpp new file mode 100644 index 00000000..39553a49 --- /dev/null +++ b/include/YAGE/Math/vector.hpp @@ -0,0 +1,133 @@ +#ifndef YAGE_MATH_VECTOR_HPP +#define YAGE_MATH_VECTOR_HPP + +#include + +namespace yage +{ + +class Vector +{ +protected: + std::vector members_; + +public: + Vector()=delete; + virtual ~Vector(); + + inline long double &operator[](std::size_t index) + { + return members_[index]; + } + + inline const long double &operator[](std::size_t index) const + { + return members_[index]; + } + + inline Vector &operator+() + { + return *this; + } + + inline Vector &operator-() + { + for(auto &i : members_) + { + i=-i; + } + return *this; + } + + inline Vector &operator+=(const Vector &other) + { + for(std::size_t i=0; i mem; + for(auto i : members_) + { + mem.emplace_back(i+other); + } + return Vector(mem); + } + + inline Vector operator-(long double other) const + { + std::vector mem; + for(auto i : members_) + { + mem.emplace_back(i-other); + } + return Vector(mem); + } + + inline Vector operator*(long double other) const + { + std::vector mem; + for(auto i : members_) + { + mem.emplace_back(i*other); + } + return Vector(mem); + } + + inline Vector operator/(long double other) const + { + std::vector mem; + for(auto i : members_) + { + mem.emplace_back(i/other); + } + return Vector(mem); + } + +protected: + Vector(const std::vector &members); +}; + +} // yage + +#endif diff --git a/include/YAGE/Math/vector2d.hpp b/include/YAGE/Math/vector2d.hpp new file mode 100644 index 00000000..1a6d2856 --- /dev/null +++ b/include/YAGE/Math/vector2d.hpp @@ -0,0 +1,20 @@ +#ifndef YAGE_MATH_VECTOR2D_HPP +#define YAGE_MATH_VECTOR2D_HPP + +#include "vector.hpp" + +namespace yage +{ + +class Vector2D : public Vector +{ +public: + Vector2D(long double x=0.L, long double y=0.L); + + long double getX() const; + long double getY() const; +}; + +} // yage + +#endif diff --git a/include/YAGE/Physics/body.hpp b/include/YAGE/Physics/body.hpp index c781e1d4..54fdeb3f 100644 --- a/include/YAGE/Physics/body.hpp +++ b/include/YAGE/Physics/body.hpp @@ -8,25 +8,28 @@ namespace yage class Body { +public: + // gravity constant + static const double GRAVITY; protected: - // current force acting on object - glm::vec2 force_; - - // current acceleration - glm::vec2 acceleration_=glm::vec2(0.f, 0.f); - - // current velocity of the object - glm::vec2 velocity_; - // center of mass of the object - glm::vec2 center_of_mass_; + glm::vec2 position_=glm::vec2(0.f, 0.f); // mass of the object - double mass_; + double mass_=1.0; + + // current velocity of the object + glm::vec2 velocity_=glm::vec2(0.f, 0.f); // boolean that defines if gravity can act on the object - bool gravity_; - + bool gravity_=true; + + // current acceleration + glm::vec2 acceleration_=glm::vec2(0.f, 0.f); + + // force acting on the body + glm::vec2 force_=glm::vec2(0.f, 0.f); + public: virtual ~Body(); @@ -38,7 +41,10 @@ public: float yPosition() const; protected: // protected constructor to initialize member variables - Body(const glm::vec2 ¢er_of_mass, double mass, const glm::vec2 &force, const glm::vec2 &velocity, bool gravity); + Body(const glm::vec2 &position=glm::vec2(0.f, 0.f), + double mass=1.0, + const glm::vec2 &velocity=glm::vec2(0.f, 0.f), + bool gravity=false); }; } // yage diff --git a/include/YAGE/Physics/particlebody.hpp b/include/YAGE/Physics/particlebody.hpp new file mode 100644 index 00000000..d9d5e08c --- /dev/null +++ b/include/YAGE/Physics/particlebody.hpp @@ -0,0 +1,26 @@ +#ifndef YAGE_PARTICLE_BODY_HPP +#define YAGE_PARTICLE_BODY_HPP + +#include + +#include "body.hpp" + +namespace yage +{ + +class ParticleBody : public Body +{ +public: + ParticleBody(const glm::vec2 &position=glm::vec2(0.f, 0.f), + double mass=1.0, + const glm::vec2 &velocity=glm::vec2(0.f, 0.f), + bool gravity=true); + + // apply a force to the rigid body + virtual void applyForce(const glm::vec2 &force); + virtual void update(); +}; + +} // yage + +#endif diff --git a/include/YAGE/Physics/rigidbody.hpp b/include/YAGE/Physics/rigidbody.hpp index bf729828..48380dac 100644 --- a/include/YAGE/Physics/rigidbody.hpp +++ b/include/YAGE/Physics/rigidbody.hpp @@ -1,23 +1,20 @@ #ifndef YAGE_RIGID_BODY_HPP #define YAGE_RIGID_BODY_HPP -#include "body.hpp" +#include + +#include "particlebody.hpp" namespace yage { -class RigidBody : public Body +class RigidBody : public ParticleBody { public: - RigidBody(const glm::vec2 ¢er_of_mass, - double mass, - const glm::vec2 &force=glm::vec2(0.f, 0.f), - const glm::vec2 &velocity=glm::vec2(0.f, 0.f), + RigidBody(const glm::vec2 &position=glm::vec2(0.f, 0.f), + double mass=1.0, + const glm::vec2 &velocity=glm::vec2(0.f, 0.f), bool gravity=true); - - // apply a force to the rigid body - virtual void applyForce(const glm::vec2 &force); - virtual void update(); }; } // yage -- cgit