From 1bfd582b7b7f762011f5f4b7f84e180cf20e9046 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 19:35:19 +0100 Subject: shielded mediator --- src/crepe/api/CMakeLists.txt | 1 + src/crepe/api/Scene.h | 62 ++++++++++++++++++++++++++++++++++++++++++-- src/crepe/api/Scene.hpp | 13 ++++++++++ src/example/game.cpp | 8 +++--- 4 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 src/crepe/api/Scene.hpp diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index fb11c8d..eb7b042 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -36,6 +36,7 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES Vector2.hpp Color.h Scene.h + Scene.hpp Metadata.h Camera.h Animator.h diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h index ba9bb76..a1e5cfe 100644 --- a/src/crepe/api/Scene.h +++ b/src/crepe/api/Scene.h @@ -3,12 +3,16 @@ #include #include "../manager/Mediator.h" +#include "../manager/ResourceManager.h" +#include "../manager/ComponentManager.h" #include "../util/OptionalRef.h" +#include "GameObject.h" namespace crepe { class SceneManager; class ComponentManager; +class Asset; /** * \brief Represents a Scene @@ -38,7 +42,7 @@ public: // TODO: Late references should ALWAYS be private! This is currently kept as-is so unit tests // keep passing, but this reference should not be directly accessible by the user!!! -protected: +private: /** * \name Late references * @@ -51,8 +55,62 @@ protected: * \{ */ //! Mediator reference - OptionalRef mediator; //! \} + OptionalRef mediator; + +protected: + + /** + * \brief Retrieve the reference to the SaveManager instance + * + * \returns A reference to the SaveManager instance held by the Mediator. + */ + SaveManager& get_save_manager() const{ + return mediator->save_manager; + } + + /** + * \brief Create a new game object using the component manager + * + * \param name Metadata::name (required) + * \param tag Metadata::tag (optional, empty by default) + * \param position Transform::position (optional, origin by default) + * \param rotation Transform::rotation (optional, 0 by default) + * \param scale Transform::scale (optional, 1 by default) + * + * \returns GameObject interface + * + * \note This method automatically assigns a new entity ID + */ + GameObject new_object(const std::string & name, const std::string & tag = "", + const vec2 & position = {0, 0}, double rotation = 0, + double scale = 1) { + // Forward the call to ComponentManager's new_object method + return mediator->component_manager->new_object(name, tag, position, rotation, scale); + } + + /** + * \brief Mark a resource as persistent (i.e. used across multiple scenes) + * + * \param asset Asset the concrete resource is instantiated from + * \param persistent Whether this resource is persistent (true=keep, false=destroy) + */ + void set_persistent(const Asset & asset, bool persistent){ + mediator->resource_manager->set_persistent(asset, persistent); + } + + /** + * \brief Log a message using Log::logf + * + * \tparam Args Log::logf parameters + * \param args Log::logf parameters + */ + template + void logf(Args &&... args); + }; } // namespace crepe + + +#include "Scene.hpp" diff --git a/src/crepe/api/Scene.hpp b/src/crepe/api/Scene.hpp new file mode 100644 index 0000000..d0ada65 --- /dev/null +++ b/src/crepe/api/Scene.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "Scene.h" +#include "../util/Log.h" + +namespace crepe { + +template +void Scene::logf(Args &&... args) { + Log::logf(std::forward(args)...); +} + +} diff --git a/src/example/game.cpp b/src/example/game.cpp index 5361f3a..16fe18f 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -160,15 +160,13 @@ public: void load_scene() { - Mediator & m = this->mediator; - ComponentManager & mgr = m.component_manager; Color color(0, 0, 0, 255); float screen_size_width = 320; float screen_size_height = 240; float world_collider = 1000; //define playable world - GameObject world = mgr.new_object( + GameObject world = new_object( "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1); world.add_component(Rigidbody::Data{ .mass = 0, @@ -196,7 +194,7 @@ public: .zoom = 1, }); - GameObject game_object1 = mgr.new_object( + GameObject game_object1 = new_object( "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1); game_object1.add_component(Rigidbody::Data{ .mass = 1, @@ -228,7 +226,7 @@ public: .active = false; - GameObject game_object2 = mgr.new_object( + GameObject game_object2 = new_object( "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1); game_object2.add_component(Rigidbody::Data{ .mass = 1, -- cgit v1.2.3 From c9a093dfb670e29fb92cfd75938aa8f293a0767a Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 19:37:10 +0100 Subject: format --- src/crepe/api/Scene.h | 15 +++++---------- src/crepe/api/Scene.hpp | 4 ++-- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h index a1e5cfe..6ee1f68 100644 --- a/src/crepe/api/Scene.h +++ b/src/crepe/api/Scene.h @@ -2,9 +2,9 @@ #include +#include "../manager/ComponentManager.h" #include "../manager/Mediator.h" #include "../manager/ResourceManager.h" -#include "../manager/ComponentManager.h" #include "../util/OptionalRef.h" #include "GameObject.h" @@ -59,15 +59,12 @@ private: OptionalRef mediator; protected: - /** * \brief Retrieve the reference to the SaveManager instance * * \returns A reference to the SaveManager instance held by the Mediator. */ - SaveManager& get_save_manager() const{ - return mediator->save_manager; - } + SaveManager & get_save_manager() const { return mediator->save_manager; } /** * \brief Create a new game object using the component manager @@ -83,8 +80,8 @@ protected: * \note This method automatically assigns a new entity ID */ GameObject new_object(const std::string & name, const std::string & tag = "", - const vec2 & position = {0, 0}, double rotation = 0, - double scale = 1) { + const vec2 & position = {0, 0}, double rotation = 0, + double scale = 1) { // Forward the call to ComponentManager's new_object method return mediator->component_manager->new_object(name, tag, position, rotation, scale); } @@ -95,7 +92,7 @@ protected: * \param asset Asset the concrete resource is instantiated from * \param persistent Whether this resource is persistent (true=keep, false=destroy) */ - void set_persistent(const Asset & asset, bool persistent){ + void set_persistent(const Asset & asset, bool persistent) { mediator->resource_manager->set_persistent(asset, persistent); } @@ -107,10 +104,8 @@ protected: */ template void logf(Args &&... args); - }; } // namespace crepe - #include "Scene.hpp" diff --git a/src/crepe/api/Scene.hpp b/src/crepe/api/Scene.hpp index d0ada65..58aacb6 100644 --- a/src/crepe/api/Scene.hpp +++ b/src/crepe/api/Scene.hpp @@ -1,7 +1,7 @@ #pragma once -#include "Scene.h" #include "../util/Log.h" +#include "Scene.h" namespace crepe { @@ -10,4 +10,4 @@ void Scene::logf(Args &&... args) { Log::logf(std::forward(args)...); } -} +} // namespace crepe -- cgit v1.2.3 From 5f09a71b433e01a9dc17f93c5d9f79117cef618e Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 19:38:17 +0100 Subject: fixed includes --- src/crepe/api/Scene.h | 1 + src/crepe/api/Scene.hpp | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h index a1e5cfe..cb8147f 100644 --- a/src/crepe/api/Scene.h +++ b/src/crepe/api/Scene.h @@ -6,6 +6,7 @@ #include "../manager/ResourceManager.h" #include "../manager/ComponentManager.h" #include "../util/OptionalRef.h" + #include "GameObject.h" namespace crepe { diff --git a/src/crepe/api/Scene.hpp b/src/crepe/api/Scene.hpp index d0ada65..b0ef392 100644 --- a/src/crepe/api/Scene.hpp +++ b/src/crepe/api/Scene.hpp @@ -1,8 +1,9 @@ #pragma once -#include "Scene.h" #include "../util/Log.h" +#include "Scene.h" + namespace crepe { template -- cgit v1.2.3 From 1cce3f1456f9df143d700d3ac37fcfd318577a36 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 19:39:01 +0100 Subject: removed merge include --- src/crepe/api/Scene.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/crepe/api/Scene.hpp b/src/crepe/api/Scene.hpp index ee89981..ecb21ed 100644 --- a/src/crepe/api/Scene.hpp +++ b/src/crepe/api/Scene.hpp @@ -1,7 +1,6 @@ #pragma once #include "../util/Log.h" -#include "Scene.h" #include "Scene.h" -- cgit v1.2.3 From f7b4866811c63ae24c366d9452e53d24e504336f Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 12 Dec 2024 19:47:59 +0100 Subject: fps fix + regression test --- src/crepe/manager/LoopTimerManager.cpp | 3 +-- src/crepe/manager/LoopTimerManager.h | 6 +++--- src/test/LoopTimerTest.cpp | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index 9819632..9d24c2b 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -1,6 +1,5 @@ #include #include - #include "../util/Log.h" #include "LoopTimerManager.h" @@ -31,7 +30,7 @@ void LoopTimerManager::update() { this->delta_time = this->maximum_delta_time; } if (this->delta_time > 0s) { - this->actual_fps = 1.0 / duration_cast(this->delta_time).count(); + this->actual_fps = static_cast(1.0 / this->delta_time.count()); } else { this->actual_fps = 0; } diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h index 91403e4..c943d41 100644 --- a/src/crepe/manager/LoopTimerManager.h +++ b/src/crepe/manager/LoopTimerManager.h @@ -6,7 +6,7 @@ namespace crepe { -typedef std::chrono::duration duration_t; +typedef std::chrono::duration duration_t; typedef std::chrono::duration elapsed_time_t; /** @@ -149,9 +149,9 @@ private: private: //! Target frames per second. - unsigned target_fps = 60; + unsigned int target_fps = 60; //! Actual frames per second. - unsigned actual_fps = 0; + unsigned int actual_fps = 0; //! Time scale for speeding up or slowing down the game (0 = pause, < 1 = slow down, 1 = normal speed, > 1 = speed up). float time_scale = 1; //! Maximum delta time in seconds to avoid large jumps. diff --git a/src/test/LoopTimerTest.cpp b/src/test/LoopTimerTest.cpp index 5e1eccf..c468567 100644 --- a/src/test/LoopTimerTest.cpp +++ b/src/test/LoopTimerTest.cpp @@ -76,3 +76,19 @@ TEST_F(LoopTimerTest, getCurrentTime) { ASSERT_NEAR(loop_timer.get_elapsed_time().count(), elapsed_time, 5); } +TEST_F(LoopTimerTest, getFPS) { + // Set the target FPS to 60 (which gives a target time per frame of ~16.67 ms) + loop_timer.set_target_framerate(60); + + auto start_time = steady_clock::now(); + loop_timer.enforce_frame_rate(); + + auto elapsed_time = steady_clock::now() - start_time; + loop_timer.update(); + unsigned int fps = loop_timer.get_fps(); + auto elapsed_ms = duration_cast(elapsed_time).count(); + + // For 60 FPS, the target frame time is around 16.67ms + ASSERT_NEAR(elapsed_ms, 16.7, 1); + ASSERT_NEAR(fps, 60, 2); +} -- cgit v1.2.3 From 15c25ba41d0e128c61b38c1751bf5eb36b5a040c Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 12 Dec 2024 19:51:10 +0100 Subject: made return unsigned int --- src/crepe/manager/LoopTimerManager.cpp | 2 +- src/crepe/manager/LoopTimerManager.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index 9d24c2b..57956f2 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -1,6 +1,6 @@ +#include "../util/Log.h" #include #include -#include "../util/Log.h" #include "LoopTimerManager.h" diff --git a/src/crepe/manager/LoopTimerManager.h b/src/crepe/manager/LoopTimerManager.h index c943d41..76b02d3 100644 --- a/src/crepe/manager/LoopTimerManager.h +++ b/src/crepe/manager/LoopTimerManager.h @@ -55,7 +55,7 @@ public: * * \return Current FPS. */ - unsigned get_fps() const; + unsigned int get_fps() const; /** * \brief Get the current time scale. -- cgit v1.2.3 From 66b0a4e535759df5381972c15b1babcf0fb30154 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 12 Dec 2024 19:59:58 +0100 Subject: include fix --- src/crepe/manager/LoopTimerManager.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/crepe/manager/LoopTimerManager.cpp b/src/crepe/manager/LoopTimerManager.cpp index 57956f2..a6e4788 100644 --- a/src/crepe/manager/LoopTimerManager.cpp +++ b/src/crepe/manager/LoopTimerManager.cpp @@ -1,7 +1,8 @@ -#include "../util/Log.h" #include #include +#include "../util/Log.h" + #include "LoopTimerManager.h" using namespace crepe; -- cgit v1.2.3 From c5667ee90d7d542aa9984eb31b48b59d6240119b Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 13 Dec 2024 12:13:43 +0100 Subject: bugfix animatorsystem --- src/crepe/system/AnimatorSystem.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 31eb85c..107b25d 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -3,20 +3,23 @@ #include "../api/Animator.h" #include "../manager/ComponentManager.h" #include "../manager/LoopTimerManager.h" +#include #include "AnimatorSystem.h" using namespace crepe; +using namespace std::chrono; void AnimatorSystem::update() { ComponentManager & mgr = this->mediator.component_manager; LoopTimerManager & timer = this->mediator.loop_timer; RefVector animations = mgr.get_components_by_type(); - unsigned long long elapsed_time = timer.get_elapsed_time().count(); + float elapsed_time = duration_cast>(timer.get_elapsed_time()).count(); for (Animator & a : animations) { if (!a.active) continue; + if (a.data.fps == 0) continue; Animator::Data & ctx = a.data; float frame_duration = 1.0f / ctx.fps; -- cgit v1.2.3 From ca4c004d473ad5ed02abd4a7beff3a5a65c83487 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Fri, 13 Dec 2024 16:31:13 +0100 Subject: made test less strict --- src/test/LoopTimerTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/LoopTimerTest.cpp b/src/test/LoopTimerTest.cpp index 6391076..7bd6305 100644 --- a/src/test/LoopTimerTest.cpp +++ b/src/test/LoopTimerTest.cpp @@ -30,7 +30,7 @@ TEST_F(LoopTimerTest, EnforcesTargetFrameRate) { auto elapsed_ms = duration_cast(elapsed_time).count(); // For 60 FPS, the target frame time is around 16.67ms - ASSERT_NEAR(elapsed_ms, 16.7, 1); + ASSERT_NEAR(elapsed_ms, 16.7, 5); } TEST_F(LoopTimerTest, SetTargetFps) { -- cgit v1.2.3 From 5a8ae8b82019afbb5aa40f4fbcf45a9df82d43f3 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 17:56:35 +0100 Subject: moved mediator up --- src/crepe/api/Scene.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h index 9ef75b5..1203612 100644 --- a/src/crepe/api/Scene.h +++ b/src/crepe/api/Scene.h @@ -56,8 +56,8 @@ private: * \{ */ //! Mediator reference - //! \} OptionalRef mediator; + //! \} protected: /** -- cgit v1.2.3 From 128e7975eb6417438fcf6f51224b6067554a2004 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 18:43:12 +0100 Subject: added feedback --- src/crepe/api/Scene.cpp | 5 +++++ src/crepe/api/Scene.h | 38 +++++++++++++++----------------------- src/crepe/api/Scene.hpp | 11 ++++++++--- 3 files changed, 28 insertions(+), 26 deletions(-) create mode 100644 src/crepe/api/Scene.cpp diff --git a/src/crepe/api/Scene.cpp b/src/crepe/api/Scene.cpp new file mode 100644 index 0000000..5117766 --- /dev/null +++ b/src/crepe/api/Scene.cpp @@ -0,0 +1,5 @@ +#include "Scene.h" + +using namespace crepe; + +SaveManager & Scene::get_save_manager() const { return mediator->save_manager; } diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h index 1203612..cba4e10 100644 --- a/src/crepe/api/Scene.h +++ b/src/crepe/api/Scene.h @@ -6,6 +6,7 @@ #include "../manager/Mediator.h" #include "../manager/ResourceManager.h" #include "../util/OptionalRef.h" +#include "../util/Log.h" #include "GameObject.h" @@ -65,20 +66,10 @@ protected: * * \returns A reference to the SaveManager instance held by the Mediator. */ - SaveManager & get_save_manager() const { return mediator->save_manager; } + SaveManager & get_save_manager() const; /** - * \brief Create a new game object using the component manager - * - * \param name Metadata::name (required) - * \param tag Metadata::tag (optional, empty by default) - * \param position Transform::position (optional, origin by default) - * \param rotation Transform::rotation (optional, 0 by default) - * \param scale Transform::scale (optional, 1 by default) - * - * \returns GameObject interface - * - * \note This method automatically assigns a new entity ID + * \copydoc ComponentManager::new_object */ GameObject new_object(const std::string & name, const std::string & tag = "", const vec2 & position = {0, 0}, double rotation = 0, @@ -88,23 +79,24 @@ protected: } /** - * \brief Mark a resource as persistent (i.e. used across multiple scenes) - * - * \param asset Asset the concrete resource is instantiated from - * \param persistent Whether this resource is persistent (true=keep, false=destroy) + * \copydoc ResourceManager::set_persistent */ void set_persistent(const Asset & asset, bool persistent) { mediator->resource_manager->set_persistent(asset, persistent); } /** - * \brief Log a message using Log::logf - * - * \tparam Args Log::logf parameters - * \param args Log::logf parameters - */ - template - void logf(Args &&... args); + * \name Logging functions + * \see Log + * \{ + */ + //! \copydoc Log::logf + template + void logf(const Log::Level & level, std::format_string fmt, Args &&... args); + //! \copydoc Log::logf + template + void logf(std::format_string fmt, Args &&... args); + //! \} }; } // namespace crepe diff --git a/src/crepe/api/Scene.hpp b/src/crepe/api/Scene.hpp index ecb21ed..14635df 100644 --- a/src/crepe/api/Scene.hpp +++ b/src/crepe/api/Scene.hpp @@ -6,9 +6,14 @@ namespace crepe { -template -void Scene::logf(Args &&... args) { - Log::logf(std::forward(args)...); +template +void Scene::logf(const Log::Level & level, std::format_string fmt, Args &&... args) { + Log::logf(level, fmt, std::forward(args)...); +} + +template +void Scene::logf(std::format_string fmt, Args &&... args) { + Log::logf(fmt, std::forward(args)...); } } // namespace crepe -- cgit v1.2.3 From 374cdf9b14e372e85c7a88c0b994146b34977193 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 18:45:12 +0100 Subject: added scene to cmake --- src/crepe/api/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index eb7b042..8f84f06 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -20,6 +20,7 @@ target_sources(crepe PUBLIC Button.cpp UIObject.cpp AI.cpp + Scene.cpp ) target_sources(crepe PUBLIC FILE_SET HEADERS FILES -- cgit v1.2.3 From e7d9c28c509588d627169568776d40fc5752698f Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 20:27:33 +0100 Subject: multiple bug/feature fixes --- src/crepe/api/LoopManager.cpp | 2 +- src/crepe/system/CollisionSystem.cpp | 59 +++++++++++++++++++++++++----------- src/example/game.cpp | 23 ++++++++------ 3 files changed, 56 insertions(+), 28 deletions(-) diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index a76c167..1baa21d 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -35,9 +35,9 @@ void LoopManager::set_running(bool running) { this->game_running = running; } void LoopManager::fixed_update() { // TODO: retrieve EventManager from direct member after singleton refactor + this->get_system().update(); EventManager & ev = this->mediator.event_manager; ev.dispatch_events(); - this->get_system().update(); this->get_system().update(); this->get_system().update(); } diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 44a0431..5c49983 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -192,13 +192,15 @@ CollisionSystem::collision_handler(CollisionInternal & data1, CollisionInternal resolution_direction = Direction::BOTH; } else if (resolution.x != 0) { resolution_direction = Direction::X_DIRECTION; - if (data1.rigidbody.data.linear_velocity.y != 0) - resolution.y = data1.rigidbody.data.linear_velocity.y + //checks if the other velocity has a value and if this object moved + if (data1.rigidbody.data.linear_velocity.y != 0 && data1.rigidbody.data.linear_velocity.x != 0) + resolution.y = -data1.rigidbody.data.linear_velocity.y * (resolution.x / data1.rigidbody.data.linear_velocity.x); } else if (resolution.y != 0) { resolution_direction = Direction::Y_DIRECTION; - if (data1.rigidbody.data.linear_velocity.x != 0) - resolution.x = data1.rigidbody.data.linear_velocity.x + //checks if the other velocity has a value and if this object moved + if (data1.rigidbody.data.linear_velocity.x != 0 && data1.rigidbody.data.linear_velocity.y != 0) + resolution.x = -data1.rigidbody.data.linear_velocity.x * (resolution.y / data1.rigidbody.data.linear_velocity.y); } @@ -314,28 +316,51 @@ void CollisionSystem::static_collision_handler(CollisionInfo & info) { // Move object back using calculate move back value info.this_transform.position += info.resolution; - // If bounce is enabled mirror velocity - if (info.this_rigidbody.data.elastisity_coefficient > 0) { - if (info.resolution_direction == Direction::BOTH) { - info.this_rigidbody.data.linear_velocity.y + switch (info.resolution_direction) { + case Direction::BOTH: + //bounce + if(info.this_rigidbody.data.elastisity_coefficient > 0){ + info.this_rigidbody.data.linear_velocity.y = -info.this_rigidbody.data.linear_velocity.y * info.this_rigidbody.data.elastisity_coefficient; info.this_rigidbody.data.linear_velocity.x = -info.this_rigidbody.data.linear_velocity.x * info.this_rigidbody.data.elastisity_coefficient; - } else if (info.resolution_direction == Direction::Y_DIRECTION) { - info.this_rigidbody.data.linear_velocity.y + } + //stop movement + else { + info.this_rigidbody.data.linear_velocity = {0, 0}; + } + break; + case Direction::Y_DIRECTION: + // Bounce + if (info.this_rigidbody.data.elastisity_coefficient > 0) { + info.this_rigidbody.data.linear_velocity.y = -info.this_rigidbody.data.linear_velocity.y * info.this_rigidbody.data.elastisity_coefficient; - } else if (info.resolution_direction == Direction::X_DIRECTION) { - info.this_rigidbody.data.linear_velocity.x + } + // Stop movement + else{ + info.this_rigidbody.data.linear_velocity.y = 0; + info.this_transform.position.x -= info.resolution.x; + } + break; + case Direction::X_DIRECTION: + // Bounce + if (info.this_rigidbody.data.elastisity_coefficient > 0) { + info.this_rigidbody.data.linear_velocity.x = -info.this_rigidbody.data.linear_velocity.x * info.this_rigidbody.data.elastisity_coefficient; - } - } - // Stop movement if bounce is disabled - else { - info.this_rigidbody.data.linear_velocity = {0, 0}; + } + // Stop movement + else{ + info.this_rigidbody.data.linear_velocity.x = 0; + info.this_transform.position.y -= info.resolution.y; + } + break; + case Direction::NONE: + // Not possible + break; } } diff --git a/src/example/game.cpp b/src/example/game.cpp index 4239c15..af87647 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -29,23 +29,23 @@ class MyScript1 : public Script { Log::logf("Box script keypressed()"); switch (test.key) { case Keycode::A: { - Transform & tf = this->get_component(); - tf.position.x -= 1; + Rigidbody & tf = this->get_component(); + tf.data.linear_velocity.x -= 1 ; break; } case Keycode::W: { - Transform & tf = this->get_component(); - tf.position.y -= 1; + Rigidbody & tf = this->get_component(); + tf.data.linear_velocity.y -= 1 ; break; } case Keycode::S: { - Transform & tf = this->get_component(); - tf.position.y += 1; + Rigidbody & tf = this->get_component(); + tf.data.linear_velocity.y += 1 ; break; } case Keycode::D: { - Transform & tf = this->get_component(); - tf.position.x += 1; + Rigidbody & tf = this->get_component(); + tf.data.linear_velocity.x += 1 ; break; } case Keycode::E: { @@ -80,7 +80,10 @@ class MyScript1 : public Script { [this](const KeyPressEvent & ev) -> bool { return this->keypressed(ev); }); } void update() { - // Retrieve component from the same GameObject this script is on + Rigidbody & tf = this->get_component(); + Log::logf("linear_velocity.x {}",tf.data.linear_velocity.x); + Log::logf("linear_velocity.y {}",tf.data.linear_velocity.y); + // tf.data.linear_velocity = {0,0}; } }; @@ -194,7 +197,7 @@ public: .mass = 1, .gravity_scale = 0, .body_type = Rigidbody::BodyType::DYNAMIC, - .linear_velocity = {0, 0}, + .linear_velocity = {0, 1}, .constraints = {0, 0, 0}, .elastisity_coefficient = 1, .offset = {0, 0}, -- cgit v1.2.3 From d5904cb53bb7cc3a33894cb30746433b724f5634 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 20:29:04 +0100 Subject: reset loop --- src/crepe/api/LoopManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 1baa21d..a76c167 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -35,9 +35,9 @@ void LoopManager::set_running(bool running) { this->game_running = running; } void LoopManager::fixed_update() { // TODO: retrieve EventManager from direct member after singleton refactor - this->get_system().update(); EventManager & ev = this->mediator.event_manager; ev.dispatch_events(); + this->get_system().update(); this->get_system().update(); this->get_system().update(); } -- cgit v1.2.3 From fea0c2f0f96e093962c5faf3643176e856b1e8f6 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 20:42:44 +0100 Subject: make format --- src/crepe/system/CollisionSystem.cpp | 30 ++++++++++++++++-------------- src/example/game.cpp | 12 ++++++------ 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 5c49983..ab9b1aa 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -193,13 +193,15 @@ CollisionSystem::collision_handler(CollisionInternal & data1, CollisionInternal } else if (resolution.x != 0) { resolution_direction = Direction::X_DIRECTION; //checks if the other velocity has a value and if this object moved - if (data1.rigidbody.data.linear_velocity.y != 0 && data1.rigidbody.data.linear_velocity.x != 0) + if (data1.rigidbody.data.linear_velocity.y != 0 + && data1.rigidbody.data.linear_velocity.x != 0) resolution.y = -data1.rigidbody.data.linear_velocity.y * (resolution.x / data1.rigidbody.data.linear_velocity.x); } else if (resolution.y != 0) { resolution_direction = Direction::Y_DIRECTION; //checks if the other velocity has a value and if this object moved - if (data1.rigidbody.data.linear_velocity.x != 0 && data1.rigidbody.data.linear_velocity.y != 0) + if (data1.rigidbody.data.linear_velocity.x != 0 + && data1.rigidbody.data.linear_velocity.y != 0) resolution.x = -data1.rigidbody.data.linear_velocity.x * (resolution.y / data1.rigidbody.data.linear_velocity.y); } @@ -319,13 +321,13 @@ void CollisionSystem::static_collision_handler(CollisionInfo & info) { switch (info.resolution_direction) { case Direction::BOTH: //bounce - if(info.this_rigidbody.data.elastisity_coefficient > 0){ + if (info.this_rigidbody.data.elastisity_coefficient > 0) { info.this_rigidbody.data.linear_velocity.y - = -info.this_rigidbody.data.linear_velocity.y - * info.this_rigidbody.data.elastisity_coefficient; - info.this_rigidbody.data.linear_velocity.x - = -info.this_rigidbody.data.linear_velocity.x - * info.this_rigidbody.data.elastisity_coefficient; + = -info.this_rigidbody.data.linear_velocity.y + * info.this_rigidbody.data.elastisity_coefficient; + info.this_rigidbody.data.linear_velocity.x + = -info.this_rigidbody.data.linear_velocity.x + * info.this_rigidbody.data.elastisity_coefficient; } //stop movement else { @@ -336,11 +338,11 @@ void CollisionSystem::static_collision_handler(CollisionInfo & info) { // Bounce if (info.this_rigidbody.data.elastisity_coefficient > 0) { info.this_rigidbody.data.linear_velocity.y - = -info.this_rigidbody.data.linear_velocity.y - * info.this_rigidbody.data.elastisity_coefficient; + = -info.this_rigidbody.data.linear_velocity.y + * info.this_rigidbody.data.elastisity_coefficient; } // Stop movement - else{ + else { info.this_rigidbody.data.linear_velocity.y = 0; info.this_transform.position.x -= info.resolution.x; } @@ -349,11 +351,11 @@ void CollisionSystem::static_collision_handler(CollisionInfo & info) { // Bounce if (info.this_rigidbody.data.elastisity_coefficient > 0) { info.this_rigidbody.data.linear_velocity.x - = -info.this_rigidbody.data.linear_velocity.x - * info.this_rigidbody.data.elastisity_coefficient; + = -info.this_rigidbody.data.linear_velocity.x + * info.this_rigidbody.data.elastisity_coefficient; } // Stop movement - else{ + else { info.this_rigidbody.data.linear_velocity.x = 0; info.this_transform.position.y -= info.resolution.y; } diff --git a/src/example/game.cpp b/src/example/game.cpp index af87647..dd5b968 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -30,22 +30,22 @@ class MyScript1 : public Script { switch (test.key) { case Keycode::A: { Rigidbody & tf = this->get_component(); - tf.data.linear_velocity.x -= 1 ; + tf.data.linear_velocity.x -= 1; break; } case Keycode::W: { Rigidbody & tf = this->get_component(); - tf.data.linear_velocity.y -= 1 ; + tf.data.linear_velocity.y -= 1; break; } case Keycode::S: { Rigidbody & tf = this->get_component(); - tf.data.linear_velocity.y += 1 ; + tf.data.linear_velocity.y += 1; break; } case Keycode::D: { Rigidbody & tf = this->get_component(); - tf.data.linear_velocity.x += 1 ; + tf.data.linear_velocity.x += 1; break; } case Keycode::E: { @@ -81,8 +81,8 @@ class MyScript1 : public Script { } void update() { Rigidbody & tf = this->get_component(); - Log::logf("linear_velocity.x {}",tf.data.linear_velocity.x); - Log::logf("linear_velocity.y {}",tf.data.linear_velocity.y); + Log::logf("linear_velocity.x {}", tf.data.linear_velocity.x); + Log::logf("linear_velocity.y {}", tf.data.linear_velocity.y); // tf.data.linear_velocity = {0,0}; } }; -- cgit v1.2.3 From c4c5758061c817784743af07271445905d62dfdd Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 20:47:57 +0100 Subject: moved code to cpp --- src/crepe/api/Scene.cpp | 10 ++++++++++ src/crepe/api/Scene.h | 19 ++++--------------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/crepe/api/Scene.cpp b/src/crepe/api/Scene.cpp index 5117766..9a2a1bc 100644 --- a/src/crepe/api/Scene.cpp +++ b/src/crepe/api/Scene.cpp @@ -3,3 +3,13 @@ using namespace crepe; SaveManager & Scene::get_save_manager() const { return mediator->save_manager; } + +GameObject Scene::new_object(const std::string & name, const std::string & tag, const vec2 & position, double rotation, double scale) { + // Forward the call to ComponentManager's new_object method + return mediator->component_manager->new_object(name, tag, position, rotation, scale); +} + + +void Scene::set_persistent(const Asset & asset, bool persistent) { + mediator->resource_manager->set_persistent(asset, persistent); +} diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h index cba4e10..e70d9ba 100644 --- a/src/crepe/api/Scene.h +++ b/src/crepe/api/Scene.h @@ -68,23 +68,12 @@ protected: */ SaveManager & get_save_manager() const; - /** - * \copydoc ComponentManager::new_object - */ - GameObject new_object(const std::string & name, const std::string & tag = "", - const vec2 & position = {0, 0}, double rotation = 0, - double scale = 1) { - // Forward the call to ComponentManager's new_object method - return mediator->component_manager->new_object(name, tag, position, rotation, scale); - } + //! \copydoc ComponentManager::new_object + GameObject new_object(const std::string & name, const std::string & tag = "", const vec2 & position = {0, 0}, double rotation = 0, double scale = 1); - /** - * \copydoc ResourceManager::set_persistent - */ - void set_persistent(const Asset & asset, bool persistent) { - mediator->resource_manager->set_persistent(asset, persistent); - } + //! \copydoc ResourceManager::set_persistent + void set_persistent(const Asset & asset, bool persistent); /** * \name Logging functions * \see Log -- cgit v1.2.3 From ded2f0d56d95edffb97830f406bce9e1ec4293d1 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 20:48:44 +0100 Subject: make format --- src/crepe/api/Scene.cpp | 10 +++++----- src/crepe/api/Scene.h | 9 +++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/crepe/api/Scene.cpp b/src/crepe/api/Scene.cpp index 9a2a1bc..ad729d2 100644 --- a/src/crepe/api/Scene.cpp +++ b/src/crepe/api/Scene.cpp @@ -4,12 +4,12 @@ using namespace crepe; SaveManager & Scene::get_save_manager() const { return mediator->save_manager; } -GameObject Scene::new_object(const std::string & name, const std::string & tag, const vec2 & position, double rotation, double scale) { - // Forward the call to ComponentManager's new_object method - return mediator->component_manager->new_object(name, tag, position, rotation, scale); +GameObject Scene::new_object(const std::string & name, const std::string & tag, + const vec2 & position, double rotation, double scale) { + // Forward the call to ComponentManager's new_object method + return mediator->component_manager->new_object(name, tag, position, rotation, scale); } - void Scene::set_persistent(const Asset & asset, bool persistent) { - mediator->resource_manager->set_persistent(asset, persistent); + mediator->resource_manager->set_persistent(asset, persistent); } diff --git a/src/crepe/api/Scene.h b/src/crepe/api/Scene.h index e70d9ba..dcca9d4 100644 --- a/src/crepe/api/Scene.h +++ b/src/crepe/api/Scene.h @@ -5,8 +5,8 @@ #include "../manager/ComponentManager.h" #include "../manager/Mediator.h" #include "../manager/ResourceManager.h" -#include "../util/OptionalRef.h" #include "../util/Log.h" +#include "../util/OptionalRef.h" #include "GameObject.h" @@ -69,10 +69,11 @@ protected: SaveManager & get_save_manager() const; //! \copydoc ComponentManager::new_object - GameObject new_object(const std::string & name, const std::string & tag = "", const vec2 & position = {0, 0}, double rotation = 0, double scale = 1); - + GameObject new_object(const std::string & name, const std::string & tag = "", + const vec2 & position = {0, 0}, double rotation = 0, + double scale = 1); - //! \copydoc ResourceManager::set_persistent + //! \copydoc ResourceManager::set_persistent void set_persistent(const Asset & asset, bool persistent); /** * \name Logging functions -- cgit v1.2.3 From b57b61e06e6c0f1c7dfc939ef3bf52799749cfa0 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 20:54:50 +0100 Subject: improved readablity --- src/crepe/system/CollisionSystem.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 5c49983..938ae82 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -188,18 +188,18 @@ CollisionSystem::collision_handler(CollisionInternal & data1, CollisionInternal } Direction resolution_direction = Direction::NONE; - if (resolution.x != 0 && resolution.y != 0) { + if (resolution != vec2{0,0}) { resolution_direction = Direction::BOTH; } else if (resolution.x != 0) { resolution_direction = Direction::X_DIRECTION; //checks if the other velocity has a value and if this object moved - if (data1.rigidbody.data.linear_velocity.y != 0 && data1.rigidbody.data.linear_velocity.x != 0) + if (data1.rigidbody.data.linear_velocity != vec2{0,0}) resolution.y = -data1.rigidbody.data.linear_velocity.y * (resolution.x / data1.rigidbody.data.linear_velocity.x); } else if (resolution.y != 0) { resolution_direction = Direction::Y_DIRECTION; //checks if the other velocity has a value and if this object moved - if (data1.rigidbody.data.linear_velocity.x != 0 && data1.rigidbody.data.linear_velocity.y != 0) + if (data1.rigidbody.data.linear_velocity != vec2{0,0}) resolution.x = -data1.rigidbody.data.linear_velocity.x * (resolution.y / data1.rigidbody.data.linear_velocity.y); } -- cgit v1.2.3 From 64e74c336baa1e69a2ad0d9a3bd9e5393e6f436a Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 21:00:33 +0100 Subject: fixed bug --- src/crepe/system/CollisionSystem.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 7cafe82..6dc1597 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -188,7 +188,7 @@ CollisionSystem::collision_handler(CollisionInternal & data1, CollisionInternal } Direction resolution_direction = Direction::NONE; - if (resolution != vec2{0,0}) { + if (resolution.x != 0 && resolution.y != 0) { resolution_direction = Direction::BOTH; } else if (resolution.x != 0) { resolution_direction = Direction::X_DIRECTION; @@ -320,12 +320,13 @@ void CollisionSystem::static_collision_handler(CollisionInfo & info) { case Direction::BOTH: //bounce if (info.this_rigidbody.data.elastisity_coefficient > 0) { - info.this_rigidbody.data.linear_velocity.y - = -info.this_rigidbody.data.linear_velocity.y - * info.this_rigidbody.data.elastisity_coefficient; - info.this_rigidbody.data.linear_velocity.x - = -info.this_rigidbody.data.linear_velocity.x - * info.this_rigidbody.data.elastisity_coefficient; + info.this_rigidbody.data.linear_velocity = -info.this_rigidbody.data.linear_velocity * info.this_rigidbody.data.elastisity_coefficient; + // info.this_rigidbody.data.linear_velocity.y + // = -info.this_rigidbody.data.linear_velocity.y + // * info.this_rigidbody.data.elastisity_coefficient; + // info.this_rigidbody.data.linear_velocity.x + // = -info.this_rigidbody.data.linear_velocity.x + // * info.this_rigidbody.data.elastisity_coefficient; } //stop movement else { -- cgit v1.2.3 From 47cc992a4892d4f3de5702668033ad45ea43dd73 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 21:28:27 +0100 Subject: removed comment and reverted if --- src/crepe/system/CollisionSystem.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 6dc1597..496224e 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -193,13 +193,13 @@ CollisionSystem::collision_handler(CollisionInternal & data1, CollisionInternal } else if (resolution.x != 0) { resolution_direction = Direction::X_DIRECTION; //checks if the other velocity has a value and if this object moved - if (data1.rigidbody.data.linear_velocity != vec2{0,0}) + if (data1.rigidbody.data.linear_velocity.x != 0 && data1.rigidbody.data.linear_velocity.y != 0) resolution.y = -data1.rigidbody.data.linear_velocity.y * (resolution.x / data1.rigidbody.data.linear_velocity.x); } else if (resolution.y != 0) { resolution_direction = Direction::Y_DIRECTION; //checks if the other velocity has a value and if this object moved - if (data1.rigidbody.data.linear_velocity != vec2{0,0}) + if (data1.rigidbody.data.linear_velocity.x != 0 && data1.rigidbody.data.linear_velocity.y != 0) resolution.x = -data1.rigidbody.data.linear_velocity.x * (resolution.y / data1.rigidbody.data.linear_velocity.y); } @@ -321,12 +321,6 @@ void CollisionSystem::static_collision_handler(CollisionInfo & info) { //bounce if (info.this_rigidbody.data.elastisity_coefficient > 0) { info.this_rigidbody.data.linear_velocity = -info.this_rigidbody.data.linear_velocity * info.this_rigidbody.data.elastisity_coefficient; - // info.this_rigidbody.data.linear_velocity.y - // = -info.this_rigidbody.data.linear_velocity.y - // * info.this_rigidbody.data.elastisity_coefficient; - // info.this_rigidbody.data.linear_velocity.x - // = -info.this_rigidbody.data.linear_velocity.x - // * info.this_rigidbody.data.elastisity_coefficient; } //stop movement else { -- cgit v1.2.3 From b9fc66f6922b1f40f2dbe14e8dfc4caa469654bc Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 14 Dec 2024 11:28:34 +0100 Subject: fix SceneManagerTest --- src/test/SceneManagerTest.cpp | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/test/SceneManagerTest.cpp b/src/test/SceneManagerTest.cpp index 9bb260c..480e07a 100644 --- a/src/test/SceneManagerTest.cpp +++ b/src/test/SceneManagerTest.cpp @@ -15,11 +15,9 @@ using namespace crepe; class ConcreteScene1 : public Scene { public: void load_scene() { - Mediator & mediator = this->mediator; - ComponentManager & mgr = mediator.component_manager; - GameObject object1 = mgr.new_object("scene_1", "tag_scene_1", vec2{0, 0}, 0, 1); - GameObject object2 = mgr.new_object("scene_1", "tag_scene_1", vec2{1, 0}, 0, 1); - GameObject object3 = mgr.new_object("scene_1", "tag_scene_1", vec2{2, 0}, 0, 1); + GameObject object1 = new_object("scene_1", "tag_scene_1", vec2{0, 0}, 0, 1); + GameObject object2 = new_object("scene_1", "tag_scene_1", vec2{1, 0}, 0, 1); + GameObject object3 = new_object("scene_1", "tag_scene_1", vec2{2, 0}, 0, 1); } string get_name() const { return "scene1"; } @@ -28,12 +26,10 @@ public: class ConcreteScene2 : public Scene { public: void load_scene() { - Mediator & mediator = this->mediator; - ComponentManager & mgr = mediator.component_manager; - GameObject object1 = mgr.new_object("scene_2", "tag_scene_2", vec2{0, 0}, 0, 1); - GameObject object2 = mgr.new_object("scene_2", "tag_scene_2", vec2{0, 1}, 0, 1); - GameObject object3 = mgr.new_object("scene_2", "tag_scene_2", vec2{0, 2}, 0, 1); - GameObject object4 = mgr.new_object("scene_2", "tag_scene_2", vec2{0, 3}, 0, 1); + GameObject object1 = new_object("scene_2", "tag_scene_2", vec2{0, 0}, 0, 1); + GameObject object2 = new_object("scene_2", "tag_scene_2", vec2{0, 1}, 0, 1); + GameObject object3 = new_object("scene_2", "tag_scene_2", vec2{0, 2}, 0, 1); + GameObject object4 = new_object("scene_2", "tag_scene_2", vec2{0, 3}, 0, 1); } string get_name() const { return "scene2"; } @@ -44,9 +40,7 @@ public: ConcreteScene3(const string & name) : name(name) {} void load_scene() { - Mediator & mediator = this->mediator; - ComponentManager & mgr = mediator.component_manager; - GameObject object1 = mgr.new_object("scene_3", "tag_scene_3", vec2{0, 0}, 0, 1); + GameObject object1 = new_object("scene_3", "tag_scene_3", vec2{0, 0}, 0, 1); } string get_name() const { return name; } -- cgit v1.2.3 From fd2ebb54d0c2b269c15fc84a4ac77993efc917d7 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 14 Dec 2024 11:40:20 +0100 Subject: updated test values --- src/test/CollisionTest.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp index dd45eb6..e93d61f 100644 --- a/src/test/CollisionTest.cpp +++ b/src/test/CollisionTest.cpp @@ -231,7 +231,7 @@ TEST_F(CollisionTest, collision_box_box_dynamic_x_direction) { collision_happend = true; EXPECT_EQ(ev.info.this_collider.game_object_id, 1); EXPECT_EQ(ev.info.resolution.x, -5); - EXPECT_EQ(ev.info.resolution.y, -5); + EXPECT_EQ(ev.info.resolution.y, 5); EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::X_DIRECTION); }; @@ -239,7 +239,7 @@ TEST_F(CollisionTest, collision_box_box_dynamic_x_direction) { collision_happend = true; EXPECT_EQ(ev.info.this_collider.game_object_id, 2); EXPECT_EQ(ev.info.resolution.x, 5); - EXPECT_EQ(ev.info.resolution.y, 5); + EXPECT_EQ(ev.info.resolution.y, -5); EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::X_DIRECTION); }; @@ -259,7 +259,7 @@ TEST_F(CollisionTest, collision_box_box_dynamic_y_direction) { script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; EXPECT_EQ(ev.info.this_collider.game_object_id, 1); - EXPECT_EQ(ev.info.resolution.x, -5); + EXPECT_EQ(ev.info.resolution.x, 5); EXPECT_EQ(ev.info.resolution.y, -5); EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::Y_DIRECTION); @@ -267,7 +267,7 @@ TEST_F(CollisionTest, collision_box_box_dynamic_y_direction) { script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; EXPECT_EQ(ev.info.this_collider.game_object_id, 2); - EXPECT_EQ(ev.info.resolution.x, 5); + EXPECT_EQ(ev.info.resolution.x, -5); EXPECT_EQ(ev.info.resolution.y, 5); EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::Y_DIRECTION); @@ -311,7 +311,7 @@ TEST_F(CollisionTest, collision_box_box_static_x_direction) { collision_happend = true; EXPECT_EQ(ev.info.this_collider.game_object_id, 1); EXPECT_EQ(ev.info.resolution.x, -5); - EXPECT_EQ(ev.info.resolution.y, -5); + EXPECT_EQ(ev.info.resolution.y, 5); EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::X_DIRECTION); }; @@ -335,7 +335,7 @@ TEST_F(CollisionTest, collision_box_box_static_y_direction) { script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; EXPECT_EQ(ev.info.this_collider.game_object_id, 1); - EXPECT_EQ(ev.info.resolution.x, -5); + EXPECT_EQ(ev.info.resolution.x, 5); EXPECT_EQ(ev.info.resolution.y, -5); EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::Y_DIRECTION); -- cgit v1.2.3 From 876896e50711509e80ef551b4e8ad440e8039b97 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 14 Dec 2024 12:07:33 +0100 Subject: `make format` --- src/crepe/system/CollisionSystem.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 496224e..af8adce 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -193,13 +193,15 @@ CollisionSystem::collision_handler(CollisionInternal & data1, CollisionInternal } else if (resolution.x != 0) { resolution_direction = Direction::X_DIRECTION; //checks if the other velocity has a value and if this object moved - if (data1.rigidbody.data.linear_velocity.x != 0 && data1.rigidbody.data.linear_velocity.y != 0) + if (data1.rigidbody.data.linear_velocity.x != 0 + && data1.rigidbody.data.linear_velocity.y != 0) resolution.y = -data1.rigidbody.data.linear_velocity.y * (resolution.x / data1.rigidbody.data.linear_velocity.x); } else if (resolution.y != 0) { resolution_direction = Direction::Y_DIRECTION; //checks if the other velocity has a value and if this object moved - if (data1.rigidbody.data.linear_velocity.x != 0 && data1.rigidbody.data.linear_velocity.y != 0) + if (data1.rigidbody.data.linear_velocity.x != 0 + && data1.rigidbody.data.linear_velocity.y != 0) resolution.x = -data1.rigidbody.data.linear_velocity.x * (resolution.y / data1.rigidbody.data.linear_velocity.y); } @@ -320,7 +322,9 @@ void CollisionSystem::static_collision_handler(CollisionInfo & info) { case Direction::BOTH: //bounce if (info.this_rigidbody.data.elastisity_coefficient > 0) { - info.this_rigidbody.data.linear_velocity = -info.this_rigidbody.data.linear_velocity * info.this_rigidbody.data.elastisity_coefficient; + info.this_rigidbody.data.linear_velocity + = -info.this_rigidbody.data.linear_velocity + * info.this_rigidbody.data.elastisity_coefficient; } //stop movement else { -- cgit v1.2.3