aboutsummaryrefslogtreecommitdiff
path: root/src/test/PhysicsTest.cpp
diff options
context:
space:
mode:
authorJaro <59013720+JaroWMR@users.noreply.github.com>2024-12-12 18:47:54 +0100
committerGitHub <noreply@github.com>2024-12-12 18:47:54 +0100
commitfd403d038b017ec8976023471073329896035e36 (patch)
treefcf4af55aa56b2de19e87e5246888e1af9f4f527 /src/test/PhysicsTest.cpp
parent05a33d4793520fa84a93bc79882ef29d39cd08e5 (diff)
parentab1423f48d82ba9b0619ec107639a80773edbfc2 (diff)
Merge pull request #64 from lonkaars/jaro/physics-system-improvement
Jaro/physics system improvement
Diffstat (limited to 'src/test/PhysicsTest.cpp')
-rw-r--r--src/test/PhysicsTest.cpp46
1 files changed, 26 insertions, 20 deletions
diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp
index 43d2931..3afb3c7 100644
--- a/src/test/PhysicsTest.cpp
+++ b/src/test/PhysicsTest.cpp
@@ -3,6 +3,8 @@
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Transform.h>
#include <crepe/manager/ComponentManager.h>
+#include <crepe/manager/LoopTimerManager.h>
+#include <crepe/manager/Mediator.h>
#include <crepe/system/PhysicsSystem.h>
#include <gtest/gtest.h>
@@ -16,6 +18,7 @@ class PhysicsTest : public ::testing::Test {
public:
ComponentManager component_manager{m};
PhysicsSystem system{m};
+ LoopTimerManager loop_timer{m};
void SetUp() override {
ComponentManager & mgr = this->component_manager;
@@ -27,7 +30,7 @@ public:
.mass = 1,
.gravity_scale = 1,
.body_type = Rigidbody::BodyType::DYNAMIC,
- .max_linear_velocity = vec2{10, 10},
+ .max_linear_velocity = 10,
.max_angular_velocity = 10,
.constraints = {0, 0},
});
@@ -55,39 +58,40 @@ TEST_F(PhysicsTest, gravity) {
EXPECT_EQ(transform.position.y, 0);
system.update();
- EXPECT_EQ(transform.position.y, 1);
+ EXPECT_NEAR(transform.position.y, 0.0004, 0.0001);
system.update();
- EXPECT_EQ(transform.position.y, 3);
+ EXPECT_NEAR(transform.position.y, 0.002, 0.001);
}
TEST_F(PhysicsTest, max_velocity) {
ComponentManager & mgr = this->component_manager;
vector<reference_wrapper<Rigidbody>> rigidbodies = mgr.get_components_by_id<Rigidbody>(0);
Rigidbody & rigidbody = rigidbodies.front().get();
+ rigidbody.data.gravity_scale = 0;
ASSERT_FALSE(rigidbodies.empty());
EXPECT_EQ(rigidbody.data.linear_velocity.y, 0);
rigidbody.add_force_linear({100, 100});
rigidbody.add_force_angular(100);
system.update();
- EXPECT_EQ(rigidbody.data.linear_velocity.y, 10);
- EXPECT_EQ(rigidbody.data.linear_velocity.x, 10);
+ EXPECT_NEAR(rigidbody.data.linear_velocity.y, 7.07, 0.01);
+ EXPECT_NEAR(rigidbody.data.linear_velocity.x, 7.07, 0.01);
EXPECT_EQ(rigidbody.data.angular_velocity, 10);
rigidbody.add_force_linear({-100, -100});
rigidbody.add_force_angular(-100);
system.update();
- EXPECT_EQ(rigidbody.data.linear_velocity.y, -10);
- EXPECT_EQ(rigidbody.data.linear_velocity.x, -10);
+ EXPECT_NEAR(rigidbody.data.linear_velocity.y, -7.07, 0.01);
+ EXPECT_NEAR(rigidbody.data.linear_velocity.x, -7.07, 0.01);
EXPECT_EQ(rigidbody.data.angular_velocity, -10);
}
TEST_F(PhysicsTest, movement) {
- Config::get_instance().physics.gravity = 0;
ComponentManager & mgr = this->component_manager;
vector<reference_wrapper<Rigidbody>> rigidbodies = mgr.get_components_by_id<Rigidbody>(0);
Rigidbody & rigidbody = rigidbodies.front().get();
+ rigidbody.data.gravity_scale = 0;
vector<reference_wrapper<Transform>> transforms = mgr.get_components_by_id<Transform>(0);
const Transform & transform = transforms.front().get();
ASSERT_FALSE(rigidbodies.empty());
@@ -96,31 +100,33 @@ TEST_F(PhysicsTest, movement) {
rigidbody.add_force_linear({1, 1});
rigidbody.add_force_angular(1);
system.update();
- EXPECT_EQ(transform.position.x, 1);
- EXPECT_EQ(transform.position.y, 1);
- EXPECT_EQ(transform.rotation, 1);
+ EXPECT_NEAR(transform.position.x, 0.02, 0.001);
+ EXPECT_NEAR(transform.position.y, 0.02, 0.001);
+ EXPECT_NEAR(transform.rotation, 0.02, 0.001);
rigidbody.data.constraints = {1, 1, 1};
- EXPECT_EQ(transform.position.x, 1);
- EXPECT_EQ(transform.position.y, 1);
- EXPECT_EQ(transform.rotation, 1);
-
+ EXPECT_NEAR(transform.position.x, 0.02, 0.001);
+ EXPECT_NEAR(transform.position.y, 0.02, 0.001);
+ EXPECT_NEAR(transform.rotation, 0.02, 0.001);
+ rigidbody.data.constraints = {0, 0, 0};
rigidbody.data.linear_velocity_coefficient.x = 0.5;
rigidbody.data.linear_velocity_coefficient.y = 0.5;
rigidbody.data.angular_velocity_coefficient = 0.5;
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);
+ EXPECT_NEAR(rigidbody.data.linear_velocity.x, 0.98, 0.01);
+ EXPECT_NEAR(rigidbody.data.linear_velocity.y, 0.98, 0.01);
+ EXPECT_NEAR(rigidbody.data.angular_velocity, 0.98, 0.01);
rigidbody.data.constraints = {1, 1, 0};
rigidbody.data.angular_velocity_coefficient = 0;
rigidbody.data.max_angular_velocity = 1000;
rigidbody.data.angular_velocity = 360;
system.update();
- EXPECT_EQ(transform.rotation, 1);
+ EXPECT_NEAR(transform.rotation, 7.24, 0.01);
rigidbody.data.angular_velocity = -360;
system.update();
- EXPECT_EQ(transform.rotation, 1);
+ EXPECT_NEAR(transform.rotation, 0.04, 0.001);
+ system.update();
+ EXPECT_NEAR(transform.rotation, 352.84, 0.01);
}