aboutsummaryrefslogtreecommitdiff
path: root/src/test/PhysicsTest.cpp
blob: 7bd7626ad571600748c2c802a24f0f6040098eb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <crepe/api/Config.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Transform.h>
#include <crepe/manager/ComponentManager.h>
#include <crepe/system/PhysicsSystem.h>
#include <gtest/gtest.h>

using namespace std;
using namespace std::chrono_literals;
using namespace crepe;

class PhysicsTest : public ::testing::Test {
	Mediator m;

public:
	ComponentManager component_manager{m};
	PhysicsSystem system{m};

	void SetUp() override {
		ComponentManager & mgr = this->component_manager;
		vector<reference_wrapper<Transform>> transforms
			= mgr.get_components_by_id<Transform>(0);
		if (transforms.empty()) {
			auto entity = mgr.new_object("", "", vec2{0, 0}, 0, 0);
			entity.add_component<Rigidbody>(Rigidbody::Data{
				.mass = 1,
				.gravity_scale = 1,
				.body_type = Rigidbody::BodyType::DYNAMIC,
				.max_linear_velocity = vec2{10, 10},
				.max_angular_velocity = 10,
				.constraints = {0, 0},
			});
		}
		transforms = mgr.get_components_by_id<Transform>(0);
		Transform & transform = transforms.front().get();
		transform.position.x = 0.0;
		transform.position.y = 0.0;
		transform.rotation = 0.0;
		vector<reference_wrapper<Rigidbody>> rigidbodies
			= mgr.get_components_by_id<Rigidbody>(0);
		Rigidbody & rigidbody = rigidbodies.front().get();
		rigidbody.data.angular_velocity = 0;
		rigidbody.data.linear_velocity.x = 0;
		rigidbody.data.linear_velocity.y = 0;
	}
};

TEST_F(PhysicsTest, gravity) {
	Config::get_instance().physics.gravity = 1;
	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);

	system.fixed_update();
	EXPECT_EQ(transform.position.y, 1);

	system.fixed_update();
	EXPECT_EQ(transform.position.y, 3);
}

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();
	ASSERT_FALSE(rigidbodies.empty());
	EXPECT_EQ(rigidbody.data.linear_velocity.y, 0);

	rigidbody.add_force_linear({100, 100});
	rigidbody.add_force_angular(100);
	system.fixed_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);
	system.fixed_update();
	EXPECT_EQ(rigidbody.data.linear_velocity.y, -10);
	EXPECT_EQ(rigidbody.data.linear_velocity.x, -10);
	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();
	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);
	system.fixed_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_velocity_coefficient.x = 0.5;
	rigidbody.data.linear_velocity_coefficient.y = 0.5;
	rigidbody.data.angular_velocity_coefficient = 0.5;
	system.fixed_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_velocity_coefficient = 0;
	rigidbody.data.max_angular_velocity = 1000;
	rigidbody.data.angular_velocity = 360;
	system.fixed_update();
	EXPECT_EQ(transform.rotation, 1);

	rigidbody.data.angular_velocity = -360;
	system.fixed_update();
	EXPECT_EQ(transform.rotation, 1);
}