box collider

This commit is contained in:
2026-04-13 14:03:59 +02:00
parent 0f776347f4
commit dc55af5323
8 changed files with 67 additions and 5 deletions

View File

@@ -1,4 +1,5 @@
#include <object/components/boxcollider.hpp>
#include <object/entity.hpp>
namespace Game::Object::Components {
BoxCollider::BoxCollider(const BoxCollider& other) : Component(other) {
@@ -31,5 +32,44 @@ namespace Game::Object::Components {
void BoxCollider::update(float deltaTime, Object::Entity* thisEntity) {
// Collision detection and response logic can go here
// Make box bounds
Transform* transform = thisEntity->getTransform();
float halfWidth = transform->scaleX * UNIVERSAL_SCALE_COEFFICIENT * 0.5f;
float halfHeight = transform->scaleY * UNIVERSAL_SCALE_COEFFICIENT * 0.5f;
float left = transform->x - halfWidth;
float right = transform->x + halfWidth;
float top = transform->y - halfHeight;
float bottom = transform->y + halfHeight;
mBounds = {left, right, top, bottom};
// Check for collisions with other entities that have box colliders
// For simplicity, just check each
std::vector<Entity*> entities = State::GameState::getInstance().getEntitiesSnapshot();
for (Entity* other : entities) {
if (other == thisEntity) continue; // Don't check collision with self
BoxCollider* otherCollider = other->getComponent<BoxCollider>();
if (!otherCollider) continue; // No collider, skip
BoxColliderBounds otherBounds = otherCollider->getBounds();
if (left < otherBounds.right && right > otherBounds.left && top < otherBounds.bottom && bottom > otherBounds.top) {
// Collision detected
if (!mIsColliding) {
mIsColliding = true;
thisEntity->onCollisionEnter(other);
other->onCollisionEnter(thisEntity);
} else {
thisEntity->onCollisionStay(other);
other->onCollisionStay(thisEntity);
}
} else {
// No collision
if (mIsColliding) {
mIsColliding = false;
thisEntity->onCollisionExit(other);
other->onCollisionExit(thisEntity);
}
}
}
}
}