aboutsummaryrefslogtreecommitdiff
path: root/src/test/PhysicsTest.cpp
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-11-18 15:34:22 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-11-18 15:34:22 +0100
commitb92da3c729c8fc7c002cb3acbb75c56cb63bd89e (patch)
tree454b64d31b7e0c9151da88a0d49ee782565f49e9 /src/test/PhysicsTest.cpp
parent8b449e764b5c91cd3cb0c4fa404a290ab295a7ef (diff)
parent121b64b1cb6cfead5814070c8b0185d3d7308095 (diff)
Merge branch 'master' of https://github.com/lonkaars/crepe into wouter/events
Diffstat (limited to 'src/test/PhysicsTest.cpp')
-rw-r--r--src/test/PhysicsTest.cpp60
1 files changed, 33 insertions, 27 deletions
diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp
index 5385962..1e37c26 100644
--- a/src/test/PhysicsTest.cpp
+++ b/src/test/PhysicsTest.cpp
@@ -11,16 +11,17 @@ using namespace std::chrono_literals;
using namespace crepe;
class PhysicsTest : public ::testing::Test {
-protected:
- GameObject * game_object;
- PhysicsSystem physics_system;
+public:
+ ComponentManager component_manager;
+ PhysicsSystem system{component_manager};
+
void SetUp() override {
- ComponentManager & mgr = ComponentManager::get_instance();
- std::vector<std::reference_wrapper<Transform>> transforms
+ ComponentManager & mgr = this->component_manager;
+ vector<reference_wrapper<Transform>> transforms
= mgr.get_components_by_id<Transform>(0);
if (transforms.empty()) {
- game_object = new GameObject(0, "", "", Vector2{0, 0}, 0, 0);
- game_object->add_component<Rigidbody>(Rigidbody::Data{
+ auto entity = mgr.new_object("", "", Vector2{0, 0}, 0, 0);
+ entity.add_component<Rigidbody>(Rigidbody::Data{
.mass = 1,
.gravity_scale = 1,
.body_type = Rigidbody::BodyType::DYNAMIC,
@@ -36,7 +37,7 @@ protected:
transform.position.x = 0.0;
transform.position.y = 0.0;
transform.rotation = 0.0;
- std::vector<std::reference_wrapper<Rigidbody>> rigidbodies
+ vector<reference_wrapper<Rigidbody>> rigidbodies
= mgr.get_components_by_id<Rigidbody>(0);
Rigidbody & rigidbody = rigidbodies.front().get();
rigidbody.data.angular_velocity = 0;
@@ -47,34 +48,36 @@ protected:
TEST_F(PhysicsTest, gravity) {
Config::get_instance().physics.gravity = 1;
- ComponentManager & mgr = ComponentManager::get_instance();
- std::vector<std::reference_wrapper<Transform>> transforms
- = mgr.get_components_by_id<Transform>(0);
+ ComponentManager & mgr = this->component_manager;
+ vector<reference_wrapper<Transform>> transforms = mgr.get_components_by_id<Transform>(0);
const Transform & transform = transforms.front().get();
ASSERT_FALSE(transforms.empty());
EXPECT_EQ(transform.position.y, 0);
- physics_system.update();
+
+ system.update();
EXPECT_EQ(transform.position.y, 1);
- physics_system.update();
+
+ system.update();
EXPECT_EQ(transform.position.y, 3);
}
TEST_F(PhysicsTest, max_velocity) {
- ComponentManager & mgr = ComponentManager::get_instance();
- std::vector<std::reference_wrapper<Rigidbody>> rigidbodies
- = mgr.get_components_by_id<Rigidbody>(0);
+ ComponentManager & mgr = this->component_manager;
+ vector<reference_wrapper<Rigidbody>> rigidbodies = mgr.get_components_by_id<Rigidbody>(0);
Rigidbody & rigidbody = rigidbodies.front().get();
ASSERT_FALSE(rigidbodies.empty());
EXPECT_EQ(rigidbody.data.linear_velocity.y, 0);
+
rigidbody.add_force_linear({100, 100});
rigidbody.add_force_angular(100);
- physics_system.update();
+ system.update();
EXPECT_EQ(rigidbody.data.linear_velocity.y, 10);
EXPECT_EQ(rigidbody.data.linear_velocity.x, 10);
EXPECT_EQ(rigidbody.data.angular_velocity, 10);
+
rigidbody.add_force_linear({-100, -100});
rigidbody.add_force_angular(-100);
- physics_system.update();
+ system.update();
EXPECT_EQ(rigidbody.data.linear_velocity.y, -10);
EXPECT_EQ(rigidbody.data.linear_velocity.x, -10);
EXPECT_EQ(rigidbody.data.angular_velocity, -10);
@@ -82,39 +85,42 @@ TEST_F(PhysicsTest, max_velocity) {
TEST_F(PhysicsTest, movement) {
Config::get_instance().physics.gravity = 0;
- ComponentManager & mgr = ComponentManager::get_instance();
- std::vector<std::reference_wrapper<Rigidbody>> rigidbodies
- = mgr.get_components_by_id<Rigidbody>(0);
+ ComponentManager & mgr = this->component_manager;
+ vector<reference_wrapper<Rigidbody>> rigidbodies = mgr.get_components_by_id<Rigidbody>(0);
Rigidbody & rigidbody = rigidbodies.front().get();
- std::vector<std::reference_wrapper<Transform>> transforms
- = mgr.get_components_by_id<Transform>(0);
+ vector<reference_wrapper<Transform>> transforms = mgr.get_components_by_id<Transform>(0);
const Transform & transform = transforms.front().get();
ASSERT_FALSE(rigidbodies.empty());
ASSERT_FALSE(transforms.empty());
+
rigidbody.add_force_linear({1, 1});
rigidbody.add_force_angular(1);
- physics_system.update();
+ system.update();
EXPECT_EQ(transform.position.x, 1);
EXPECT_EQ(transform.position.y, 1);
EXPECT_EQ(transform.rotation, 1);
+
rigidbody.data.constraints = {1, 1, 1};
EXPECT_EQ(transform.position.x, 1);
EXPECT_EQ(transform.position.y, 1);
EXPECT_EQ(transform.rotation, 1);
+
rigidbody.data.linear_damping.x = 0.5;
rigidbody.data.linear_damping.y = 0.5;
rigidbody.data.angular_damping = 0.5;
- physics_system.update();
+ system.update();
EXPECT_EQ(rigidbody.data.linear_velocity.x, 0.5);
EXPECT_EQ(rigidbody.data.linear_velocity.y, 0.5);
EXPECT_EQ(rigidbody.data.angular_velocity, 0.5);
+
rigidbody.data.constraints = {1, 1, 0};
rigidbody.data.angular_damping = 0;
rigidbody.data.max_angular_velocity = 1000;
rigidbody.data.angular_velocity = 360;
- physics_system.update();
+ system.update();
EXPECT_EQ(transform.rotation, 1);
+
rigidbody.data.angular_velocity = -360;
- physics_system.update();
+ system.update();
EXPECT_EQ(transform.rotation, 1);
}