aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt5
-rw-r--r--include/YAGE/Physics/body.hpp45
-rw-r--r--src/particlebody.cpp2
-rw-r--r--test/matrixtest.cpp2
-rw-r--r--test/particlebodytest.cpp33
5 files changed, 46 insertions, 41 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b948be9e..3dec234b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -75,11 +75,12 @@ set(YAGE_LIB_DEP_L yage
# enable tests
enable_testing()
+set(SIMULATION_RUNS 1000)
add_executable(matrixtest ${YAGE_TEST_DIR}/matrixtest.cpp)
target_link_libraries(matrixtest gtest_main ${YAGE_LIB_DEP_L})
-add_test(NAME MatrixTest COMMAND matrixtest --gtest_repeat=1000 --gtest_break_on_failure)
+add_test(NAME MatrixTest COMMAND matrixtest --gtest_repeat=${SIMULATION_RUNS} --gtest_break_on_failure)
add_executable(particlebodytest ${YAGE_TEST_DIR}/particlebodytest.cpp)
target_link_libraries(particlebodytest gtest_main ${YAGE_LIB_DEP_L})
-add_test(NAME ParticleBodyTest COMMAND particlebodytest --gtest_repeat=1000 --gtest_break_on_failure)
+add_test(NAME ParticleBodyTest COMMAND particlebodytest --gtest_repeat=${SIMULATION_RUNS} --gtest_break_on_failure)
diff --git a/include/YAGE/Physics/body.hpp b/include/YAGE/Physics/body.hpp
index f32b767d..fab28fc1 100644
--- a/include/YAGE/Physics/body.hpp
+++ b/include/YAGE/Physics/body.hpp
@@ -12,45 +12,42 @@
#include "Math/matrix.hpp"
namespace yage {
-
class Body {
public:
- // gravity constant
- static const double GRAVITY;
+ // gravity constant
+ static const double GRAVITY;
protected:
- // center of mass of the object
- Vector2d position_ = Vector2d(0, 0);
+ // center of mass of the object
+ Vector2d position_ = Vector2d(0, 0);
- // mass of the object
- double mass_ = 1;
+ // mass of the object
+ double mass_ = 1;
- // current velocity of the object
- Vector2d velocity_ = Vector2d(0, 0);
+ // current velocity of the object
+ Vector2d velocity_ = Vector2d(0, 0);
- // boolean that defines if gravity can act on the object
- bool gravity_ = true;
+ // boolean that defines if gravity can act on the object
+ bool gravity_ = true;
- // current acceleration
- Vector2d acceleration_ = Vector2d(0, 0);
+ // current acceleration
+ Vector2d acceleration_ = Vector2d(0, 0);
- // force acting on the body
- Vector2d force_ = Vector2d(0, 0);
+ // force acting on the body
+ Vector2d force_ = Vector2d(0, 0);
public:
- // apply force to the object and update the velocity
- virtual void applyForce(const Vector2d& force) = 0;
- virtual void update() = 0;
+ // apply force to the object and update the velocity
+ virtual void applyForce(const Vector2d& force) = 0;
+ virtual void update() = 0;
- double xPosition() const;
- double yPosition() const;
+ double xPosition() const;
+ double yPosition() const;
protected:
- // protected constructor to initialize member variables
- Body(const Vector2d& position = Vector2d(0, 0), double mass = 1,
- const Vector2d& velocity = Vector2d(0, 0), bool gravity = false);
+ // protected constructor to initialize member variables
+ Body(const Vector2d& position = Vector2d(0, 0), double mass = 1, const Vector2d& velocity = Vector2d(0, 0), bool gravity = false);
};
-
} // namespace yage
#endif
diff --git a/src/particlebody.cpp b/src/particlebody.cpp
index 09e0a1c0..e120d3c5 100644
--- a/src/particlebody.cpp
+++ b/src/particlebody.cpp
@@ -37,8 +37,6 @@ void ParticleBody::update() {
else
acceleration_ = Vector2d(force_.x() / mass_, force_.y() / mass_);
- std::cout << acceleration_ << "\n";
-
Vector2d avg_acceleration = (acceleration_ + last_acceleration) / 2.0;
// update the velocity of the body
diff --git a/test/matrixtest.cpp b/test/matrixtest.cpp
index 530d3530..ee568067 100644
--- a/test/matrixtest.cpp
+++ b/test/matrixtest.cpp
@@ -41,7 +41,7 @@ int vectorDotProduct(const std::vector<int> &vec_contents_f, const std::vector<i
int x = yage::matrix::dot(v1, v2);
- return x==76;
+ return x;
}
bool matrixMultiplication()
diff --git a/test/particlebodytest.cpp b/test/particlebodytest.cpp
index 54424b37..c8e493a0 100644
--- a/test/particlebodytest.cpp
+++ b/test/particlebodytest.cpp
@@ -6,24 +6,33 @@
* ----------------------------------------------------------------------------
*/
-#include "Physics/particlebody.hpp"
+#include <cmath>
+#include <cstdlib>
-#include <iostream>
+#include "Physics/particlebody.hpp"
+#include "gtest/gtest.h"
-int main(int, char**) {
+double gravityAcceleration(int iterations) {
yage::ParticleBody body;
- for (int i = 0; i < 60 * 3; ++i) {
+ for (int i = 0; i < 60 * iterations; ++i) {
body.update();
- std::cout << "position: " << body.xPosition() << ", "
- << body.yPosition() << "\n";
}
- double ideal_position = 0.5 * -9.81 * 3 * 3;
+ return body.yPosition();
+}
+
+// Tests
+
+TEST(ParticleBody, Gravity) {
+ int random_itr = rand() % 25;
+ double idealPosition = 0.5 * -9.81 * std::pow(random_itr, 2);
- std::cout << "Ideal Position: " << ideal_position << "\n";
+ ASSERT_GE(idealPosition * 0.95, gravityAcceleration(random_itr));
+ ASSERT_LE(idealPosition * 1.05, gravityAcceleration(random_itr));
+}
- if (body.yPosition() < ideal_position * 0.95 &&
- body.yPosition() > ideal_position * 1.05)
- return 0;
- return 1;
+int main(int argc, char** argv) {
+ testing::InitGoogleTest(&argc, argv);
+ srand(static_cast<unsigned>(time(nullptr)));
+ return RUN_ALL_TESTS();
}