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 --- CMakeLists.txt | 15 +++- 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 ++--- src/body.cpp | 15 ++-- src/particlebody.cpp | 46 ++++++++++++ src/rigidbody.cpp | 34 ++------- src/vector2d.cpp | 20 +++++ test/double_size.cpp | 11 +++ test/rigid_body_test.cpp | 4 +- test/vector2d_test.cpp | 2 + 13 files changed, 312 insertions(+), 65 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 create mode 100644 src/particlebody.cpp create mode 100644 src/vector2d.cpp create mode 100644 test/double_size.cpp create mode 100644 test/vector2d_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3640dc85..8dac4251 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ project(yage) # set version numbers set(YAGE_MAJOR_VERSION 0) set(YAGE_MINOR_VERSION 1) -set(YAGE_PATCH_VERSION 0) +set(YAGE_PATCH_VERSION 1) set(YAGE_VERSION ${YAGE_MAJOR_VERSION}.${YAGE_MINOR_VERSION}${YAGE_PATCH_VERSION}) @@ -31,13 +31,15 @@ set(YAGE_SOURCES ${PROJECT_SOURCE_DIR}/src/imageloader.cpp ${PROJECT_SOURCE_DIR}/src/inputmanager.cpp ${PROJECT_SOURCE_DIR}/src/iomanager.cpp + ${PROJECT_SOURCE_DIR}/src/particlebody.cpp ${PROJECT_SOURCE_DIR}/src/picopng.cpp ${PROJECT_SOURCE_DIR}/src/rectanglecollider.cpp ${PROJECT_SOURCE_DIR}/src/resourcemanager.cpp ${PROJECT_SOURCE_DIR}/src/rigidbody.cpp - ${PROJECT_SOURCE_DIR}/src/spritebatch.cpp + ${PROJECT_SOURCE_DIR}/src/spritebatch.cpp ${PROJECT_SOURCE_DIR}/src/sprite.cpp ${PROJECT_SOURCE_DIR}/src/texturecache.cpp + ${PROJECT_SOURCE_DIR}/src/vector2d.cpp ${PROJECT_SOURCE_DIR}/src/window.cpp) @@ -54,11 +56,18 @@ add_library(${PROJECT_NAME} ${YAGE_SOURCES}) # add tests add_executable(rigid_body_test ${YAGE_TEST_DIR}/rigid_body_test.cpp) - target_link_libraries(rigid_body_test ${YAGE_LIBRARIES} ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES}) +add_executable(double_size + ${YAGE_TEST_DIR}/double_size.cpp) +target_link_libraries(double_size + ${YAGE_LIBRARIES} + ${OPENGL_LIBRARIES} + ${GLEW_LIBRARIES}) + enable_testing() add_test(RigidBodyTest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/rigid_body_test) +add_test(RigidBodyTest ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/double_size) 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 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 + +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 diff --git a/test/double_size.cpp b/test/double_size.cpp new file mode 100644 index 00000000..25c7cef7 --- /dev/null +++ b/test/double_size.cpp @@ -0,0 +1,11 @@ +#include + +int main() +{ + std::cout<<"Size of double: "< 8) + return 0; + return 1; +} diff --git a/test/rigid_body_test.cpp b/test/rigid_body_test.cpp index 39488599..b898a1b4 100644 --- a/test/rigid_body_test.cpp +++ b/test/rigid_body_test.cpp @@ -1,10 +1,10 @@ -#include "Physics/rigidbody.hpp" +#include "Physics/particlebody.hpp" #include int main(int, char**) { - yage::RigidBody body(glm::vec2(0.f, 0.f), 1, glm::vec2(0.f, 9.81f)); + yage::ParticleBody body; for(int i=0; i<60*3; ++i) { body.update(); diff --git a/test/vector2d_test.cpp b/test/vector2d_test.cpp new file mode 100644 index 00000000..2c37bba7 --- /dev/null +++ b/test/vector2d_test.cpp @@ -0,0 +1,2 @@ +#include + -- cgit