0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 15:53:42 +00:00

fixed sliding

This commit is contained in:
Benedikt Galbavy 2024-01-28 16:59:59 +01:00
parent f538e95623
commit 9f2df7c6c5
2 changed files with 20 additions and 14 deletions

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "Entity.h" #include "Entity.h"
#include "SDL_rect.h"
#include "SpriteComponent.h" #include "SpriteComponent.h"
#include "Vector2D.h" #include "Vector2D.h"
#include <vector> #include <vector>

View File

@ -9,6 +9,7 @@
#include <SDL_rect.h> #include <SDL_rect.h>
#include <bitset> #include <bitset>
#include <cstdio> #include <cstdio>
#include <memory>
IntersectionBitSet CollisionHandler::getIntersection(Entity* entityA, Entity* entityB) IntersectionBitSet CollisionHandler::getIntersection(Entity* entityA, Entity* entityB)
{ {
@ -21,35 +22,39 @@ IntersectionBitSet CollisionHandler::getIntersection(Entity* entityA, Entity* en
!entityB->hasComponent<ColliderComponent>()) !entityB->hasComponent<ColliderComponent>())
return std::bitset<DIRECTION_C>(); return std::bitset<DIRECTION_C>();
SDL_Rect* colliderA = &entityA->getComponent<ColliderComponent>().collider; SDL_Rect colliderA = entityA->getComponent<ColliderComponent>().collider;
SDL_Rect* colliderB = &entityB->getComponent<ColliderComponent>().collider; SDL_Rect colliderB = entityB->getComponent<ColliderComponent>().collider;
colliderA.x += posModA.x;
colliderA.y += posModA.y;
colliderB.x += posModB.x;
colliderB.y += posModB.y;
if (!SDL_HasIntersection( if (!SDL_HasIntersection(
colliderA, &colliderA,
colliderB)) &colliderB))
return std::bitset<DIRECTION_C>(); return std::bitset<DIRECTION_C>();
std::bitset<DIRECTION_C> intersections; std::bitset<DIRECTION_C> intersections;
// checks all 4 directions to allow checking full overlap // checks all 4 directions to allow checking full overlap
if (colliderA->x + posModA.x < colliderB->x + colliderB->w + posModB.x && if (colliderA.x < colliderB.x + colliderB.w &&
colliderA->x + posModA.x > colliderB->x + posModB.x) { colliderA.x > colliderB.x) {
printf("%zu left\n", (size_t) direction::LEFT);
intersections.set((size_t) direction::LEFT); intersections.set((size_t) direction::LEFT);
} }
if (colliderA->x + colliderA->w + posModA.x < colliderB->x + colliderB->w + posModB.x && if (colliderA.x + colliderA.w < colliderB.x + colliderB.w &&
colliderA->x + colliderA->w + posModA.x > colliderB->x + posModB.x) { colliderA.x + colliderA.w > colliderB.x) {
printf("%zu right\n", (size_t) direction::RIGHT);
intersections.set((size_t) direction::RIGHT); intersections.set((size_t) direction::RIGHT);
} }
if (colliderA->y + posModA.y < colliderB->y + colliderB->h + posModB.y && if (colliderA.y < colliderB.y + colliderB.h &&
colliderA->y + posModA.y > colliderB->y + posModB.y) colliderA.y > colliderB.y)
intersections.set((size_t) direction::UP); intersections.set((size_t) direction::UP);
if (colliderA->y + colliderA->h + posModA.y < colliderB->y + colliderB->h + posModB.y && if (colliderA.y + colliderA.h < colliderB.y + colliderB.h &&
colliderA->y + colliderA->h + posModA.y > colliderB->y + posModB.y) colliderA.y + colliderA.h > colliderB.y)
intersections.set((size_t) direction::DOWN); intersections.set((size_t) direction::DOWN);
return intersections; return intersections;