From 35a527101b00c0fc95f01d87c47181a35d46f4d3 Mon Sep 17 00:00:00 2001 From: ineslelin Date: Sat, 27 Jan 2024 17:29:14 +0100 Subject: [PATCH 1/4] left/right sliding collision possible now --- extern/libpng | 1 + extern/zlib | 1 + include/Game.h | 4 ++++ src/Game.cpp | 17 +++++++++++++++-- 4 files changed, 21 insertions(+), 2 deletions(-) create mode 160000 extern/libpng create mode 160000 extern/zlib diff --git a/extern/libpng b/extern/libpng new file mode 160000 index 0000000..71b9b5e --- /dev/null +++ b/extern/libpng @@ -0,0 +1 @@ +Subproject commit 71b9b5e16ef751d973a3935284382bc344ff9941 diff --git a/extern/zlib b/extern/zlib new file mode 160000 index 0000000..09155ea --- /dev/null +++ b/extern/zlib @@ -0,0 +1 @@ +Subproject commit 09155eaa2f9270dc4ed1fa13e2b4b2613e6e4851 diff --git a/include/Game.h b/include/Game.h index ed07f38..64f2ce2 100644 --- a/include/Game.h +++ b/include/Game.h @@ -4,6 +4,8 @@ #include #include +#include "Vector2D.h" + class AssetManager; class ColliderComponent; @@ -29,6 +31,8 @@ public: bool getWinner(); + void handleCollision(Vector2D& characterPos, SDL_Rect& characterCollider, SDL_Rect& componentCollider); + private: int counter = 0; bool isRunning = false; diff --git a/src/Game.cpp b/src/Game.cpp index 26fa8d0..8c8c911 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -158,11 +158,11 @@ void Game::update() { if (SDL_HasIntersection(&player.getComponent().collider, &cc->collider) && strcmp(cc->tag, "player") && cc->hasCollision) { - player.getComponent().position = playerPos; + handleCollision(player.getComponent().position, player.getComponent().collider, cc->collider); } if (SDL_HasIntersection(&enemy.getComponent().collider, &cc->collider) && strcmp(cc->tag, "enemy") && cc->hasCollision) { - enemy.getComponent().position = enemyPos; + handleCollision(enemy.getComponent().position, enemy.getComponent().collider, cc->collider); } } @@ -261,4 +261,17 @@ bool Game::running() const bool Game::getWinner() { return this->winner; +} + +void Game::handleCollision(Vector2D& characterPos, SDL_Rect& characterCollider, SDL_Rect& componentCollider) +{ + // collision to right of character + if (characterPos.x < componentCollider.x) + { + characterPos.x = componentCollider.x - characterCollider.w; + } + else // collision to left of character + { + characterPos.x = componentCollider.x + componentCollider.w; + } } \ No newline at end of file From 314b58ac4acbbc0e3c379c934c7cbb483513d218 Mon Sep 17 00:00:00 2001 From: ineslelin Date: Sat, 27 Jan 2024 19:36:44 +0100 Subject: [PATCH 2/4] sliding collision around borders done --- src/TransformComponent.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/TransformComponent.cpp b/src/TransformComponent.cpp index 9f73934..1cfc8e4 100644 --- a/src/TransformComponent.cpp +++ b/src/TransformComponent.cpp @@ -44,12 +44,31 @@ void TransformComponent::update() // if(velocity.x != 0 && velocity.y != 0) float multiplier = velocity.x != 0 && velocity.y != 0 ? 0.707 : 1; //normalizes vector + Vector2D newPos( position.x + velocity.x * speed * multiplier, position.y + velocity.y * speed * multiplier ); - if (newPos.x < 0 || newPos.x + (this->width * this->scale) > SCREEN_SIZE_WIDTH || newPos.y < 0 || newPos.y + (this->height * this->scale) > SCREEN_SIZE_HEIGHT) - return; + + if (newPos.x < 0) + { + newPos.x = 0; + } + + if (newPos.x + (this->width * this->scale) > SCREEN_SIZE_WIDTH) + { + newPos.x = SCREEN_SIZE_WIDTH - (this->width * this->scale); + } + + if (newPos.y < 0) + { + newPos.y = 0; + } + + if (newPos.y + (this->height * this->scale) > SCREEN_SIZE_HEIGHT) + { + newPos.y = SCREEN_SIZE_HEIGHT - (this->height * this->scale); + } position = newPos; } From c2cb8f9fc6606e3aca045ed9bfd95c55247af186 Mon Sep 17 00:00:00 2001 From: ineslelin Date: Sun, 28 Jan 2024 16:49:08 +0100 Subject: [PATCH 3/4] fixed collision interaction with player --- extern/libpng | 1 - extern/zlib | 1 - include/ColliderComponent.h | 5 +++++ include/Game.h | 2 -- src/ColliderComponent.cpp | 17 +++++++++++++++++ src/Game.cpp | 31 ++++++++++++++++--------------- 6 files changed, 38 insertions(+), 19 deletions(-) delete mode 160000 extern/libpng delete mode 160000 extern/zlib diff --git a/extern/libpng b/extern/libpng deleted file mode 160000 index 71b9b5e..0000000 --- a/extern/libpng +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 71b9b5e16ef751d973a3935284382bc344ff9941 diff --git a/extern/zlib b/extern/zlib deleted file mode 160000 index 09155ea..0000000 --- a/extern/zlib +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 09155eaa2f9270dc4ed1fa13e2b4b2613e6e4851 diff --git a/include/ColliderComponent.h b/include/ColliderComponent.h index 7c587ae..ef4d5c8 100644 --- a/include/ColliderComponent.h +++ b/include/ColliderComponent.h @@ -3,6 +3,7 @@ #include #include "Component.h" +#include "Vector2D.h" class TransformComponent; @@ -15,10 +16,14 @@ public: bool hasCollision; //added for removing collision of destroyed projectiles float hitboxScale; //adds a seperate variable for the scale of the hitbox (not the sprite) so each sprite can have a different hitbox size if needed + bool isProjectile = false; + ColliderComponent(const char* tag); ColliderComponent(const char* tag, float hitboxScale); void init() override; void update() override; void removeCollision(); + + void handleCollision(Vector2D& characterPos, SDL_Rect& characterCollider, SDL_Rect& componentCollider); }; \ No newline at end of file diff --git a/include/Game.h b/include/Game.h index 64f2ce2..a6fe7ab 100644 --- a/include/Game.h +++ b/include/Game.h @@ -31,8 +31,6 @@ public: bool getWinner(); - void handleCollision(Vector2D& characterPos, SDL_Rect& characterCollider, SDL_Rect& componentCollider); - private: int counter = 0; bool isRunning = false; diff --git a/src/ColliderComponent.cpp b/src/ColliderComponent.cpp index d1c59c0..5ba9ad5 100644 --- a/src/ColliderComponent.cpp +++ b/src/ColliderComponent.cpp @@ -25,6 +25,10 @@ void ColliderComponent::init() entity->addComponent(); } + if (strcmp(this->tag, "projectile") == 0) { + this->isProjectile = true; + } + transform = &entity->getComponent(); Game::colliders.push_back(this); } @@ -43,3 +47,16 @@ void ColliderComponent::removeCollision() { this->hasCollision = false; } + +void ColliderComponent::handleCollision(Vector2D& entityPos, SDL_Rect& entityCollider, SDL_Rect& componentCollider) +{ + // collision to right of character + if (entityPos.x < componentCollider.x) + { + entityPos.x = componentCollider.x - entityCollider.w; + } + else // collision to left of character + { + entityPos.x = componentCollider.x + componentCollider.w; + } +} \ No newline at end of file diff --git a/src/Game.cpp b/src/Game.cpp index 0c55e37..5afe1e3 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -158,11 +158,25 @@ void Game::update() { if (SDL_HasIntersection(&player.getComponent().collider, &cc->collider) && strcmp(cc->tag, "player") && cc->hasCollision) { - handleCollision(player.getComponent().position, player.getComponent().collider, cc->collider); + if (!cc->isProjectile) + { + player.getComponent().handleCollision(player.getComponent().position, player.getComponent().collider, cc->collider); + } + else + { + player.getComponent().position = playerPos; + } } if (SDL_HasIntersection(&enemy.getComponent().collider, &cc->collider) && strcmp(cc->tag, "enemy") && cc->hasCollision) { - handleCollision(enemy.getComponent().position, enemy.getComponent().collider, cc->collider); + if (!cc->isProjectile) + { + enemy.getComponent().handleCollision(enemy.getComponent().position, enemy.getComponent().collider, cc->collider); + } + else + { + enemy.getComponent().position = enemyPos; + } } } @@ -261,17 +275,4 @@ bool Game::running() const bool Game::getWinner() { return this->winner; -} - -void Game::handleCollision(Vector2D& characterPos, SDL_Rect& characterCollider, SDL_Rect& componentCollider) -{ - // collision to right of character - if (characterPos.x < componentCollider.x) - { - characterPos.x = componentCollider.x - characterCollider.w; - } - else // collision to left of character - { - characterPos.x = componentCollider.x + componentCollider.w; - } } \ No newline at end of file From 00190facab30c04619e0c2346167f0f3480b48c8 Mon Sep 17 00:00:00 2001 From: ineslelin Date: Sun, 28 Jan 2024 17:52:18 +0100 Subject: [PATCH 4/4] minimal changes in transformcomponent::update --- src/TransformComponent.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/TransformComponent.cpp b/src/TransformComponent.cpp index 1cfc8e4..8e02ad3 100644 --- a/src/TransformComponent.cpp +++ b/src/TransformComponent.cpp @@ -54,8 +54,7 @@ void TransformComponent::update() { newPos.x = 0; } - - if (newPos.x + (this->width * this->scale) > SCREEN_SIZE_WIDTH) + else if (newPos.x + (this->width * this->scale) > SCREEN_SIZE_WIDTH) { newPos.x = SCREEN_SIZE_WIDTH - (this->width * this->scale); } @@ -64,8 +63,7 @@ void TransformComponent::update() { newPos.y = 0; } - - if (newPos.y + (this->height * this->scale) > SCREEN_SIZE_HEIGHT) + else if (newPos.y + (this->height * this->scale) > SCREEN_SIZE_HEIGHT) { newPos.y = SCREEN_SIZE_HEIGHT - (this->height * this->scale); }