[Updated] Collision, looks a bit better now, [TODO] Fix the weird slide-up behaviour when jumping against a block now

This commit is contained in:
Ninjaws
2024-12-10 21:42:50 +01:00
parent 774dc3806b
commit 7bdd8829b0
3 changed files with 34 additions and 73 deletions
+4
View File
@@ -20,6 +20,10 @@ public:
/** Checks and returns which corners of the object have been collided with */ /** Checks and returns which corners of the object have been collided with */
std::unordered_map<ECorner, bool> checkForCollision(TextureWrapper texture); std::unordered_map<ECorner, bool> checkForCollision(TextureWrapper texture);
int getTileSize() {
return this->tileSize;
}
private: private:
/** Holds the number for every single tile, which refers to a certain sprite in the tileset */ /** Holds the number for every single tile, which refers to a certain sprite in the tileset */
std::vector<std::vector<Vector2>> map; std::vector<std::vector<Vector2>> map;
+29 -72
View File
@@ -27,7 +27,6 @@ public:
gravity(); gravity();
movement(); movement();
handleCollision(); handleCollision();
applyVelocity();
} }
void draw() void draw()
@@ -42,7 +41,6 @@ public:
protected: protected:
private: private:
// Vector2 position;
TextureWrapper texture; TextureWrapper texture;
Vector2 velocity = {0, 0}; Vector2 velocity = {0, 0};
@@ -53,114 +51,73 @@ private:
* Holds the corners of the player * Holds the corners of the player
* Is used for precise collision detection * 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<Vector2, >
void gravity() void gravity()
{ {
// PositionUpdate();
// Collision();
// if (!groundCollision && !crateTopCollision)
// {
if (!groundCollision) if (!groundCollision)
{ {
velocity.y += 0.6f; velocity.y += 0.6f;
} }
else 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() void movement()
{ {
velocity.x = 0.0f; velocity.x = 0.0f;
if (InputService::getInstance().isKeyDown(KEY_A)) { if (InputService::getInstance().isKeyDown(KEY_A))
// std::cout << "Left" << std::endl; {
velocity.x -= 3.0f; velocity.x -= 5.0f;
} else if (InputService::getInstance().isKeyDown(KEY_D)) { }
velocity.x += 3.0f; else if (InputService::getInstance().isKeyDown(KEY_D))
// std::cout << "Right" << std::endl; {
velocity.x += 5.0f;
} }
if(InputService::getInstance().isKeyPressed(KEY_SPACE) && groundCollision) { if (InputService::getInstance().isKeyPressed(KEY_SPACE) && groundCollision)
{
velocity.y -= 7.2f; 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() void handleCollision()
{ {
Vector2 tempPos = this->texture.getPosition();
this->texture.move(velocity);
groundCollision = false; groundCollision = false;
std::unordered_map<ECorner, bool> collisions = MapService::getInstance().getMap().checkForCollision(this->texture); std::unordered_map<ECorner, bool> collisions = MapService::getInstance().getMap().checkForCollision(this->texture);
if (collisions[ECorner::BOTTOM_LEFT] || collisions[ECorner::BOTTOM_RIGHT]) if (collisions[ECorner::BOTTOM_LEFT] || collisions[ECorner::BOTTOM_RIGHT])
{ {
this->texture.move({0, -velocity.y});
velocity.y = 0.0f; velocity.y = 0.0f;
groundCollision = true; 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]) { if (collisions[ECorner::LEFT_TOP] && collisions[ECorner::LEFT_BOTTOM])
this->texture.move({+velocity.x, 0}); {
velocity.x = 0.0f; 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]) { if (collisions[ECorner::TOP_LEFT] || collisions[ECorner::TOP_RIGHT])
this->texture.move({0, +velocity.y}); {
velocity.y = 0.0f; 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]) { if (collisions[ECorner::RIGHT_TOP] && collisions[ECorner::RIGHT_BOTTOM])
this->texture.move({-velocity.x, 0}); {
velocity.x = 0.0f; 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 #endif
+1 -1
View File
@@ -52,7 +52,7 @@ class TextureWrapper {
float right = this->position.x+this->size.x; float right = this->position.x+this->size.x;
float bottom = this->position.y + this->size.y; float bottom = this->position.y + this->size.y;
float left = this->position.x; 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_LEFT] = {left+margin, top};
corners[ECorner::TOP_RIGHT] = {right-margin, top}; corners[ECorner::TOP_RIGHT] = {right-margin, top};
corners[ECorner::RIGHT_TOP] = {right, top+margin}; corners[ECorner::RIGHT_TOP] = {right, top+margin};