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 +++++++++++++++++++++++++----------- 2 files changed, 43 insertions(+), 18 deletions(-) (limited to 'src/crepe') 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; } } -- 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(-) (limited to 'src/crepe') 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(-) (limited to 'src/crepe') 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 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(-) (limited to 'src/crepe') 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(-) (limited to 'src/crepe') 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(-) (limited to 'src/crepe') 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