aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt14
-rw-r--r--include/YAGE/Physics/body.hpp19
-rw-r--r--include/YAGE/Physics/rigidbody.hpp3
-rw-r--r--src/body.cpp24
-rw-r--r--src/body.cpps1
-rw-r--r--src/rigidbody.cpp24
-rw-r--r--test/rigid_body_test.cpp15
7 files changed, 81 insertions, 19 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 58161830..3640dc85 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,29 +14,33 @@ set(YAGE_VERSION
set(CMAKE_CXX_STANDARD 14)
# set the test sources
-set(YAGE_SOURCE_DIR ${CMAKE_SOURCE_DIR}/src)
-set(YAGE_TEST_DIR ${CMAKE_SOURCE_DIR}/test)
+set(YAGE_SOURCE_DIR ${PROJECT_SOURCE_DIR}/src)
+set(YAGE_TEST_DIR ${PROJECT_SOURCE_DIR}/test)
# add include directory
-set(YAGE_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include/YAGE)
+set(YAGE_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include/YAGE)
# set binary directory
-set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
+set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
# add sources to library
set(YAGE_SOURCES
+ ${PROJECT_SOURCE_DIR}/src/body.cpp
${PROJECT_SOURCE_DIR}/src/camera2d.cpp
${PROJECT_SOURCE_DIR}/src/glslprogram.cpp
${PROJECT_SOURCE_DIR}/src/imageloader.cpp
${PROJECT_SOURCE_DIR}/src/inputmanager.cpp
${PROJECT_SOURCE_DIR}/src/iomanager.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/sprite.cpp
- ${PROJECT_SOURCE_DIR}/src/spritebatch.cpp
${PROJECT_SOURCE_DIR}/src/texturecache.cpp
${PROJECT_SOURCE_DIR}/src/window.cpp)
+
# find libraries
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
diff --git a/include/YAGE/Physics/body.hpp b/include/YAGE/Physics/body.hpp
index 170ef491..c781e1d4 100644
--- a/include/YAGE/Physics/body.hpp
+++ b/include/YAGE/Physics/body.hpp
@@ -1,5 +1,5 @@
-#ifndef YAGE_BODDY_HPP
-#define YAGE_BODDY_HPP
+#ifndef YAGE_BODY_HPP
+#define YAGE_BODY_HPP
#include <glm/glm.hpp>
@@ -11,6 +11,10 @@ class Body
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_;
@@ -24,16 +28,17 @@ protected:
bool gravity_;
public:
- virtual ~Body() {}
+ virtual ~Body();
// apply force to the object and update the velocity
virtual void applyForce(const glm::vec2 &force)=0;
+ virtual void update()=0;
+
+ float xPosition() const;
+ float yPosition() const;
protected:
// protected constructor to initialize member variables
- Body(const glm::vec2 &center_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(const glm::vec2 &center_of_mass, double mass, const glm::vec2 &force, const glm::vec2 &velocity, bool gravity);
};
} // yage
diff --git a/include/YAGE/Physics/rigidbody.hpp b/include/YAGE/Physics/rigidbody.hpp
index 56f089ed..bf729828 100644
--- a/include/YAGE/Physics/rigidbody.hpp
+++ b/include/YAGE/Physics/rigidbody.hpp
@@ -1,7 +1,7 @@
#ifndef YAGE_RIGID_BODY_HPP
#define YAGE_RIGID_BODY_HPP
-#include "Physics/body.hpp"
+#include "body.hpp"
namespace yage
{
@@ -17,6 +17,7 @@ public:
// 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
new file mode 100644
index 00000000..fb6c40e7
--- /dev/null
+++ b/src/body.cpp
@@ -0,0 +1,24 @@
+#include "Physics/body.hpp"
+
+namespace yage
+{
+
+Body::~Body()
+{}
+
+Body::Body(const glm::vec2 &center_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)
+{}
+
+float Body::xPosition() const
+{
+ return center_of_mass_.x;
+}
+
+float Body::yPosition() const
+{
+ return center_of_mass_.y;
+}
+
+} // yage
diff --git a/src/body.cpps b/src/body.cpps
new file mode 100644
index 00000000..3cacc0b9
--- /dev/null
+++ b/src/body.cpps
@@ -0,0 +1 @@
+12 \ No newline at end of file
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 &center_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
diff --git a/test/rigid_body_test.cpp b/test/rigid_body_test.cpp
index b0b1d1d6..39488599 100644
--- a/test/rigid_body_test.cpp
+++ b/test/rigid_body_test.cpp
@@ -1,6 +1,19 @@
#include "Physics/rigidbody.hpp"
+#include <iostream>
+
int main(int, char**)
{
-
+ yage::RigidBody body(glm::vec2(0.f, 0.f), 1, glm::vec2(0.f, 9.81f));
+ for(int i=0; i<60*3; ++i)
+ {
+ body.update();
+ std::cout<<"position: "<<body.xPosition()<<", "<<body.yPosition()<<"\n";
+ }
+
+ double ideal_position=0.5*9.81*3*3;
+
+ if(body.yPosition()>ideal_position*0.95 && body.yPosition()<ideal_position*1.05)
+ return 0;
+ return 1;
}