diff options
author | JAROWMR <jarorutjes07@gmail.com> | 2024-11-20 15:14:44 +0100 |
---|---|---|
committer | JAROWMR <jarorutjes07@gmail.com> | 2024-11-20 15:14:44 +0100 |
commit | 37a38c8c9b1b78d214d8e26d45d5a85dc7bd89bd (patch) | |
tree | 0af96b2038858ab1c3dc5fc180cd320c36bec8a0 /src | |
parent | 02cdbd367d701d8d858806f45bfe2f15b392bb59 (diff) |
changed test
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/system/CollisionSystem.cpp | 2 | ||||
-rw-r--r-- | src/example/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/test/CollisionTest.cpp | 157 |
3 files changed, 130 insertions, 31 deletions
diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 3e73b44..2132b0d 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -205,7 +205,7 @@ std::vector<std::pair<CollisionSystem::CollidedInfoStor,CollisionSystem::Collide Rigidbody& rigidbody1 = mgr.get_components_by_id<Rigidbody>(game_object_id_1).front().get(); if(!rigidbody1.active) continue; - // Check CircleCollider vs CircleCollider + // Check BoxCollider vs BoxCollider for (size_t j = i + 1; j < boxcolliders.size(); ++j) { if(!boxcolliders[j].get().active) continue; // Skip self collision diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 2facc4d..c5cb63f 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -22,7 +22,6 @@ add_example(script) add_example(log) add_example(rendering) add_example(asset_manager) -add_example(physics) add_example(savemgr) add_example(proxy) add_example(db) @@ -30,6 +29,5 @@ add_example(ecs) add_example(scene_manager) add_example(game) add_example(events) -add_example(particles) add_example(gameloop) diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp index 5da26cc..7aadfa6 100644 --- a/src/test/CollisionTest.cpp +++ b/src/test/CollisionTest.cpp @@ -1,4 +1,5 @@ -#include <cstddef> +#include "system/ScriptSystem.h" +#include "types.h" #include <gtest/gtest.h> #include <crepe/api/Config.h> @@ -11,38 +12,52 @@ #include <crepe/ComponentManager.h> #include <crepe/system/CollisionSystem.h> -#include <type_traits> - +#include "iostream" using namespace std; using namespace std::chrono_literals; using namespace crepe; +//scripts for object 1 collision test +class UnitTestBoxBoxCollision1 : public Script { + static bool oncollision(const CollisionEvent& test) { + std::cout << "collision event 1" << std::endl; + return true; + } + void init() { + EventManager::get_instance().subscribe<CollisionEvent>(oncollision, this->get_game_object_id()); + } + void update() { + // Retrieve component from the same GameObject this script is on + + } +}; -class MyScript : public Script { - public: - static const crepe::CollisionSystem::CollisionInfo* last_collision_info; - private: - static bool oncollision(const CollisionEvent& test) { - last_collision_info = &test.info; - return true; - } - void init() { - EventManager::get_instance().subscribe<CollisionEvent>(oncollision, this->get_game_object_id()); - } - void update() { - // Retrieve component from the same GameObject this script is on - - } +//scripts for object 1 collision test +class UnitTestBoxBoxCollision2 : public Script { + static bool oncollision(const CollisionEvent& test) { + std::cout << "collision event 1" << std::endl; + return true; + } + void init() { + EventManager::get_instance().subscribe<CollisionEvent>(oncollision, this->get_game_object_id()); + } + void update() { + // Retrieve component from the same GameObject this script is on + + } }; class CollisionTest : public ::testing::Test { public: ComponentManager component_manager; CollisionSystem system{component_manager}; + ScriptSystem sr{component_manager}; + const double screen_size_width = 640; + const double screen_size_height = 480; + const double world_collider = 1000; void SetUp() override { - MyScript::last_collision_info = nullptr; ComponentManager & mgr = this->component_manager; if(mgr.get_components_by_id<Transform>(0).empty()) { @@ -50,12 +65,11 @@ public: create_test_components(); } reset_test_components(); + sr.update(); } void create_test_world() { - double screen_size_width = 640; - double screen_size_height = 480; - double world_collider = 1000; + ComponentManager & mgr = this->component_manager; GameObject world = mgr.new_object("Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1); world.add_component<Rigidbody>(Rigidbody::Data{ @@ -75,24 +89,111 @@ public: void create_test_components() { + ComponentManager & mgr = this->component_manager; + GameObject game_object1 = mgr.new_object("Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1); + game_object1.add_component<Rigidbody>(Rigidbody::Data{ + .mass = 1, + .gravity_scale = 0.01, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = {1,0}, + .constraints = {0, 0, 0}, + .use_gravity = true, + .bounce = true, + .elastisity = 1, + .offset = {0,0}, + }); + game_object1.add_component<BoxCollider>(Vector2{0, 0}, 20, 20); + game_object1.add_component<BehaviorScript>().set_script<UnitTestBoxBoxCollision1>(); + + GameObject game_object2 = mgr.new_object("Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2-100}, 0, 1); + game_object2.add_component<Rigidbody>(Rigidbody::Data{ + .mass = 1, + .gravity_scale = 0.01, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = {1,0}, + .constraints = {0, 0, 0}, + .use_gravity = true, + .bounce = true, + .elastisity = 1, + .offset = {0,0}, + }); + game_object2.add_component<BoxCollider>(Vector2{0, 0}, 20, 20); + game_object2.add_component<BehaviorScript>().set_script<UnitTestBoxBoxCollision2>(); } void reset_test_components() { + ComponentManager & mgr = this->component_manager; + //game object 1 + { + game_object_id_t id = 1; + Transform & tf = mgr.get_components_by_id<Transform>(id).front().get(); + tf.position = Vector2{screen_size_width/2, screen_size_height/2}; + tf.rotation = 0; + tf.scale = 1; + tf.active = 1; + Rigidbody & rg = mgr.get_components_by_id<Rigidbody>(id).front().get(); + rg.data.angular_damping = 0; + rg.data.angular_velocity = 0; + rg.data.max_angular_velocity = 100; + rg.data.linear_velocity = {0,0}; + rg.data.linear_damping = {0,0}; + rg.data.max_linear_velocity = {100,100}; + rg.data.bounce = false; + rg.data.elastisity = 0; + rg.data.offset = {0,0}; + rg.data.constraints = {0,0,0}; + } + { + game_object_id_t id = 2; + Transform & tf = mgr.get_components_by_id<Transform>(id).front().get(); + tf.position = Vector2{screen_size_width/2, screen_size_height/2-100}; + tf.rotation = 0; + tf.scale = 1; + tf.active = 1; + Rigidbody & rg = mgr.get_components_by_id<Rigidbody>(id).front().get(); + rg.data.angular_damping = 0; + rg.data.angular_velocity = 0; + rg.data.max_angular_velocity = 100; + rg.data.linear_velocity = {0,0}; + rg.data.linear_damping = {0,0}; + rg.data.max_linear_velocity = {100,100}; + rg.data.bounce = false; + rg.data.elastisity = 0; + rg.data.offset = {0,0}; + rg.data.constraints = {0,0,0}; + } } }; -TEST_F(CollisionTest, collision) { +TEST_F(CollisionTest, collision_example) { // change object data before calling update // call collision system update system.update(); - // should not be nullptr after update - ASSERT_NE(MyScript::last_collision_info, nullptr); + // should be nullptr after update with no collision + //ASSERT_EQ(MyScriptCollider1::last_collision_info_1, nullptr); + //ASSERT_EQ(MyScriptCollider2::last_collision_info_2, nullptr); + // check if values are correct (filled in data) + // EXPECT_EQ(MyScriptCollider1::last_collision_info->first.collider.game_object_id, 1); + // EXPECT_EQ(MyScriptCollider2::last_collision_info->second.collider.game_object_id, 2); + // check test data +} - // check if values are correct - EXPECT_EQ(MyScript::last_collision_info->first.collider.game_object_id, 1); - EXPECT_EQ(MyScript::last_collision_info->second.collider.game_object_id, 2); +TEST_F(CollisionTest, collision_box_box_dynamic) { + // change object data before calling update + ComponentManager & mgr = this->component_manager; + Transform & test = mgr.get_components_by_id<Transform>(2).front().get(); + test.position = {screen_size_width/2,screen_size_height/2}; + // call collision system update + system.update(); + // should be nullptr after update with no collision + // ASSERT_NE(MyScriptCollider1::last_collision_info_1, nullptr); + // ASSERT_NE(MyScriptCollider2::last_collision_info_2, nullptr); + // // check if values are correct (filled in data) + // EXPECT_EQ(MyScriptCollider1::last_collision_info_1->first.collider.game_object_id, 1); + // EXPECT_EQ(MyScriptCollider2::last_collision_info_2->second.collider.game_object_id, 2); + // check test data } |