From 7bdd8829b033eab5b71c80a618059c92517b865f Mon Sep 17 00:00:00 2001 From: Ninjaws Date: Tue, 10 Dec 2024 21:42:50 +0100 Subject: [PATCH] [Updated] Collision, looks a bit better now, [TODO] Fix the weird slide-up behaviour when jumping against a block now --- include/entities/Map.h | 4 ++ include/entities/Player.h | 101 +++++++++--------------------- include/entities/TextureWrapper.h | 2 +- 3 files changed, 34 insertions(+), 73 deletions(-) diff --git a/include/entities/Map.h b/include/entities/Map.h index 1e68ca8..7aeece0 100644 --- a/include/entities/Map.h +++ b/include/entities/Map.h @@ -20,6 +20,10 @@ public: /** Checks and returns which corners of the object have been collided with */ std::unordered_map checkForCollision(TextureWrapper texture); + int getTileSize() { + return this->tileSize; + } + private: /** Holds the number for every single tile, which refers to a certain sprite in the tileset */ std::vector> map; diff --git a/include/entities/Player.h b/include/entities/Player.h index 177543d..eea1a0b 100644 --- a/include/entities/Player.h +++ b/include/entities/Player.h @@ -27,7 +27,6 @@ public: gravity(); movement(); handleCollision(); - applyVelocity(); } void draw() @@ -42,7 +41,6 @@ public: protected: private: - // Vector2 position; TextureWrapper texture; Vector2 velocity = {0, 0}; @@ -53,114 +51,73 @@ private: * Holds the corners of the player * Is used for precise collision detection */ - // Keeps tracko f whether or not the player has collided with the top of the crate - // std::unordered_map void gravity() { - - // PositionUpdate(); - // Collision(); - - // if (!groundCollision && !crateTopCollision) - // { if (!groundCollision) { velocity.y += 0.6f; } else { - velocity.y = 0.0f; + // velocity.y = 0.0f; } - - // if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) && (groundCollision == true || crateTopCollision == true)) - // { - // velocity.y = -GetJumpSpeed(); - // } - - // this->texture.move(velocity); } void movement() { velocity.x = 0.0f; - if (InputService::getInstance().isKeyDown(KEY_A)) { - // std::cout << "Left" << std::endl; - velocity.x -= 3.0f; - } else if (InputService::getInstance().isKeyDown(KEY_D)) { - velocity.x += 3.0f; - // std::cout << "Right" << std::endl; + if (InputService::getInstance().isKeyDown(KEY_A)) + { + velocity.x -= 5.0f; + } + else if (InputService::getInstance().isKeyDown(KEY_D)) + { + velocity.x += 5.0f; } - if(InputService::getInstance().isKeyPressed(KEY_SPACE) && groundCollision) { + if (InputService::getInstance().isKeyPressed(KEY_SPACE) && groundCollision) + { velocity.y -= 7.2f; } - - this->texture.move(velocity); } + /** + * Stores the current position + * Applies the current velocity + * Checks for collision, and removes all velocities that are causing collisions + * Set the object exactly against the object it is colliding with position + */ void handleCollision() { + Vector2 tempPos = this->texture.getPosition(); + this->texture.move(velocity); + groundCollision = false; std::unordered_map collisions = MapService::getInstance().getMap().checkForCollision(this->texture); if (collisions[ECorner::BOTTOM_LEFT] || collisions[ECorner::BOTTOM_RIGHT]) { - this->texture.move({0, -velocity.y}); velocity.y = 0.0f; groundCollision = true; + this->texture.setPosition({this->texture.getPosition().x, floorf(this->texture.getPosition().y / MapService::getInstance().getMap().getTileSize()) * MapService::getInstance().getMap().getTileSize()}); } - if (collisions[ECorner::LEFT_TOP] && collisions[ECorner::LEFT_BOTTOM]) { - this->texture.move({+velocity.x, 0}); + if (collisions[ECorner::LEFT_TOP] && collisions[ECorner::LEFT_BOTTOM]) + { velocity.x = 0.0f; + this->texture.setPosition({ceilf(this->texture.getPosition().x / MapService::getInstance().getMap().getTileSize()) * MapService::getInstance().getMap().getTileSize(), this->texture.getPosition().y}); } - if (collisions[ECorner::TOP_LEFT] || collisions[ECorner::TOP_RIGHT]) { - this->texture.move({0, +velocity.y}); + if (collisions[ECorner::TOP_LEFT] || collisions[ECorner::TOP_RIGHT]) + { velocity.y = 0.0f; + this->texture.setPosition({this->texture.getPosition().x, (ceilf(this->texture.getPosition().y / MapService::getInstance().getMap().getTileSize())) * MapService::getInstance().getMap().getTileSize()}); } - if (collisions[ECorner::RIGHT_TOP] && collisions[ECorner::RIGHT_BOTTOM]) { - this->texture.move({-velocity.x, 0}); + if (collisions[ECorner::RIGHT_TOP] && collisions[ECorner::RIGHT_BOTTOM]) + { velocity.x = 0.0f; + this->texture.setPosition({floorf(this->texture.getPosition().x / MapService::getInstance().getMap().getTileSize()) * MapService::getInstance().getMap().getTileSize(), this->texture.getPosition().y}); } } - - void applyVelocity() { - // std::cout << velocity.x << std::endl; - this->texture.move(velocity); - } - - // timesCollision.clear(); - - // for (int i = 0; i < tiles.size(); i++) - // { - // if (GetColMap()[tiles[i].y][tiles[i].x] == 1) - // { - // timesCollision.push_back(i); - // } - // } - - // groundCollision = false; - - // if (timesCollision.size() > 0) - // { - // for (int i = 0; i < timesCollision.size(); i++) - // { - // if (timesCollision[i] == 0 || timesCollision[i] == 1) //Topleft and Topright - // { - // velocity.y = 0; - // } - // else if (timesCollision[i] == 2 || timesCollision[i] == 3 || //Lefttop and Leftbottom - // timesCollision[i] == 4 || timesCollision[i] == 5) //Righttop and Lefttop - // { - // velocity.x = 0; - // } - // else if (timesCollision[i] == 6 || timesCollision[i] == 7) //Bottomleft and Bottomright - // { - // groundCollision = true; - // } - // } - // } - // } }; #endif diff --git a/include/entities/TextureWrapper.h b/include/entities/TextureWrapper.h index 0bd2af9..accd1c8 100644 --- a/include/entities/TextureWrapper.h +++ b/include/entities/TextureWrapper.h @@ -52,7 +52,7 @@ class TextureWrapper { float right = this->position.x+this->size.x; float bottom = this->position.y + this->size.y; float left = this->position.x; - float margin = 5; // Space to the corner + float margin = 8; // Space to the corner corners[ECorner::TOP_LEFT] = {left+margin, top}; corners[ECorner::TOP_RIGHT] = {right-margin, top}; corners[ECorner::RIGHT_TOP] = {right, top+margin};