mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-03 10:26:29 +00:00
giving the collided Axis for context to callback on collisions
This commit is contained in:
@@ -16,8 +16,8 @@ namespace BBM
|
||||
}
|
||||
|
||||
CollisionComponent::CollisionComponent(WAL::Entity &entity,
|
||||
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &onCollide,
|
||||
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &onCollided,
|
||||
const WAL::Callback<WAL::Entity &, const WAL::Entity &, int> &onCollide,
|
||||
const WAL::Callback<WAL::Entity &, const WAL::Entity &, int> &onCollided,
|
||||
Vector3f positionOffset,
|
||||
Vector3f bound)
|
||||
: WAL::Component(entity),
|
||||
@@ -28,8 +28,8 @@ namespace BBM
|
||||
{}
|
||||
|
||||
CollisionComponent::CollisionComponent(WAL::Entity &entity,
|
||||
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &onCollide,
|
||||
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &onCollided,
|
||||
const WAL::Callback<WAL::Entity &, const WAL::Entity &, int> &onCollide,
|
||||
const WAL::Callback<WAL::Entity &, const WAL::Entity &, int> &onCollided,
|
||||
float positionOffset,
|
||||
float boundSize)
|
||||
: WAL::Component(entity),
|
||||
|
||||
@@ -14,10 +14,17 @@ namespace BBM
|
||||
class CollisionComponent : public WAL::Component
|
||||
{
|
||||
public:
|
||||
//! @brief Used to tell the collided axis
|
||||
enum CollidedAxis {
|
||||
X = 1,
|
||||
Y = 2,
|
||||
Z = 4
|
||||
};
|
||||
|
||||
//! @brief onCollide functions to be called
|
||||
WAL::Callback<WAL::Entity &, const WAL::Entity &> onCollide;
|
||||
WAL::Callback<WAL::Entity &, const WAL::Entity &, int> onCollide;
|
||||
//! @brief onCollided functions to be called
|
||||
WAL::Callback<WAL::Entity &, const WAL::Entity &> onCollided;
|
||||
WAL::Callback<WAL::Entity &, const WAL::Entity &, int> onCollided;
|
||||
//! @brief Bound size on all axis
|
||||
Vector3f bound;
|
||||
//! @brief Offset from the position component
|
||||
@@ -31,15 +38,15 @@ namespace BBM
|
||||
|
||||
//! @brief Constructor with a WAL::Callback
|
||||
CollisionComponent(WAL::Entity &entity,
|
||||
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &onCollide,
|
||||
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &onCollided,
|
||||
const WAL::Callback<WAL::Entity &, const WAL::Entity &, int> &onCollide,
|
||||
const WAL::Callback<WAL::Entity &, const WAL::Entity &, int> &onCollided,
|
||||
Vector3f positionOffset,
|
||||
Vector3f bound);
|
||||
|
||||
//! @brief Constructor with a WAL::Callback, same boundSize for all axis
|
||||
CollisionComponent(WAL::Entity &entity,
|
||||
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &onCollide,
|
||||
const WAL::Callback<WAL::Entity &, const WAL::Entity &> &onCollided,
|
||||
const WAL::Callback<WAL::Entity &, const WAL::Entity &, int> &onCollide,
|
||||
const WAL::Callback<WAL::Entity &, const WAL::Entity &, int> &onCollided,
|
||||
float positionOffset,
|
||||
float boundSize = 0);
|
||||
|
||||
|
||||
+15
-18
@@ -3,7 +3,8 @@
|
||||
// Edited by Benjamin Henry on 5/26/21.
|
||||
//
|
||||
|
||||
#include <Component/Collision/CollisionComponent.hpp>
|
||||
#include "Component/Collision/CollisionComponent.hpp"
|
||||
#include "System/Collision/CollisionSystem.hpp"
|
||||
#include "Map.hpp"
|
||||
#include <iostream>
|
||||
|
||||
@@ -11,7 +12,7 @@ namespace RAY3D = RAY::Drawables::Drawables3D;
|
||||
|
||||
namespace BBM
|
||||
{
|
||||
void MapGenerator::wallCollide(WAL::Entity &entity, const WAL::Entity &wall)
|
||||
void MapGenerator::wallCollide(WAL::Entity &entity, const WAL::Entity &wall, int collidedAxis)
|
||||
{
|
||||
auto *mov = entity.tryGetComponent<MovableComponent>();
|
||||
auto posspec = entity.getComponent<PositionComponent>();
|
||||
@@ -20,20 +21,16 @@ namespace BBM
|
||||
if (!mov)
|
||||
return;
|
||||
|
||||
std::cout << "collided coords " << posspec.position << " cube " << posspecwall.position << std::endl;
|
||||
//mov->_velocity = BBM::Vector3f ();
|
||||
return;
|
||||
auto &pos = entity.getComponent<PositionComponent>();
|
||||
const auto &wallPos = wall.getComponent<PositionComponent>();
|
||||
auto diff = pos.position + mov->getVelocity() - wallPos.position;
|
||||
// mov->_velocity = Vector3f();
|
||||
if (diff.x <= 0 && mov->_velocity.x < 0)
|
||||
//mov->_velocity = Vector3f();
|
||||
//return;
|
||||
if (collidedAxis & CollisionComponent::CollidedAxis::X)
|
||||
mov->_velocity.x = 0;
|
||||
if (diff.x >= 0 && mov->_velocity.x > 0)
|
||||
if (collidedAxis & CollisionComponent::CollidedAxis::Y)
|
||||
mov->_velocity.x = 0;
|
||||
if (diff.z <= 0 && mov->_velocity.z < 0)
|
||||
mov->_velocity.z = 0;
|
||||
if (diff.z >= 0 && mov->_velocity.z > 0)
|
||||
if (collidedAxis & CollisionComponent::CollidedAxis::Z)
|
||||
mov->_velocity.z = 0;
|
||||
}
|
||||
|
||||
@@ -60,7 +57,7 @@ namespace BBM
|
||||
if (!(i % 2) && !(j % 2)) {
|
||||
scene->addEntity("Unbreakable Wall")
|
||||
.addComponent<PositionComponent>(i, 0, j)
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &, int>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<Drawable3DComponent, RAY3D::Model>(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePng));
|
||||
}
|
||||
}
|
||||
@@ -74,25 +71,25 @@ namespace BBM
|
||||
|
||||
scene->addEntity("Bottom Wall")
|
||||
.addComponent<PositionComponent>(Vector3f((width + 1) / 2, 0, -1))
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &, int>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<Drawable3DComponent, RAY3D::Model>(unbreakableObj,
|
||||
std::make_pair(MAP_DIFFUSE, unbreakablePnj),
|
||||
RAY::Vector3(width + 3, 1, 1));
|
||||
scene->addEntity("Upper Wall")
|
||||
.addComponent<PositionComponent>(Vector3f((width + 1) / 2, 0, height + 1))
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &, int>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<Drawable3DComponent, RAY3D::Model>(unbreakableObj,
|
||||
std::make_pair(MAP_DIFFUSE, unbreakablePnj),
|
||||
RAY::Vector3(width + 3, 1, 1));
|
||||
scene->addEntity("Left Wall")
|
||||
.addComponent<PositionComponent>(Vector3f(width + 1, 0, height / 2))
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &, int>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<Drawable3DComponent, RAY3D::Model>(unbreakableObj,
|
||||
std::make_pair(MAP_DIFFUSE, unbreakablePnj),
|
||||
RAY::Vector3(1, 1, height + 1));
|
||||
scene->addEntity("Right Wall")
|
||||
.addComponent<PositionComponent>(Vector3f(-1, 0, height / 2))
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &, int>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<Drawable3DComponent, RAY3D::Model>(unbreakableObj,
|
||||
std::make_pair(MAP_DIFFUSE, unbreakablePnj),
|
||||
RAY::Vector3(1, 1, height + 1));
|
||||
@@ -142,7 +139,7 @@ namespace BBM
|
||||
scene->addEntity("Breakable Block")
|
||||
.addComponent<PositionComponent>(coords)
|
||||
.addComponent<HealthComponent>(1)
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &, int>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<Drawable3DComponent, RAY3D::Model>(breakableObj, std::make_pair(MAP_DIFFUSE, breakablePng));
|
||||
}
|
||||
|
||||
@@ -175,7 +172,7 @@ namespace BBM
|
||||
|
||||
scene->addEntity("Unbreakable Block")
|
||||
.addComponent<PositionComponent>(coords)
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &, int>(), &MapGenerator::wallCollide, .75)
|
||||
.addComponent<Drawable3DComponent, RAY3D::Model>(UnbreakableObj,
|
||||
std::make_pair(MAP_DIFFUSE, UnbreakablePng));
|
||||
}
|
||||
|
||||
+1
-1
@@ -156,7 +156,7 @@ namespace BBM
|
||||
static const std::string secondFloorHolePath;
|
||||
|
||||
public:
|
||||
static void wallCollide(WAL::Entity &entity, const WAL::Entity &wall);
|
||||
static void wallCollide(WAL::Entity &entity, const WAL::Entity &wall, int collidedAxis);
|
||||
|
||||
|
||||
//! @param width Width of the map
|
||||
|
||||
@@ -80,11 +80,11 @@ namespace BBM
|
||||
.addComponent<PositionComponent>(10, 20, 10)
|
||||
.addComponent<CameraComponent>(Vector3f(2, 0, 2));
|
||||
scene->addEntity("cube")
|
||||
.addComponent<PositionComponent>(0, 0, 0)
|
||||
.addComponent<PositionComponent>(-5, 0, -5)
|
||||
.addComponent<Drawable3DComponent, RAY3D::Cube>(Vector3f(0, 0, 0), Vector3f(3, 3, 3), RED)
|
||||
.addComponent<ControllableComponent>()
|
||||
.addComponent<KeyboardComponent>()
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &>(), &MapGenerator::wallCollide, -1, 3);
|
||||
.addComponent<CollisionComponent>(WAL::Callback<WAL::Entity &, const WAL::Entity &, int>(), &MapGenerator::wallCollide, -1, 3);
|
||||
std::srand(std::time(nullptr));
|
||||
//MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene);
|
||||
return scene;
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace BBM
|
||||
: System(wal)
|
||||
{ }
|
||||
|
||||
bool CollisionSystem::collide(Vector3f minA, Vector3f maxA, Vector3f minB, Vector3f maxB)
|
||||
bool CollisionSystem::boxesCollide(Vector3f minA, Vector3f maxA, Vector3f minB, Vector3f maxB)
|
||||
{
|
||||
bool overlapX = (minA.x <= maxB.x && maxA.x >= minB.x) || (minB.x <= maxA.x && maxB.x >= minA.x);
|
||||
bool overlapY = (minA.y <= maxB.y && maxA.y >= minB.y) || (minB.y <= maxA.y && maxB.y >= minA.y);
|
||||
@@ -28,32 +28,55 @@ namespace BBM
|
||||
auto &posA = entity.get<PositionComponent>();
|
||||
auto &colA = entity.get<CollisionComponent>();
|
||||
Vector3f pointA = posA.position + colA.positionOffset;
|
||||
Vector3f pointAx;
|
||||
Vector3f pointAy;
|
||||
Vector3f pointAz;
|
||||
|
||||
if (auto *movable = entity->tryGetComponent<MovableComponent>())
|
||||
pointA += movable->getVelocity();
|
||||
if (auto *movable = entity->tryGetComponent<MovableComponent>()) {
|
||||
auto vel = movable->getVelocity();
|
||||
pointAx.x += vel.x;
|
||||
pointAy.y += vel.y;
|
||||
pointAz.z += vel.z;
|
||||
}
|
||||
|
||||
Vector3f minA = Vector3f::min(pointA, pointA + colA.bound);
|
||||
Vector3f maxA = Vector3f::max(pointA, pointA + colA.bound);
|
||||
Vector3f minAx = Vector3f::min(pointAx, pointAx + colA.bound);
|
||||
Vector3f maxAx = Vector3f::max(pointAx, pointAx + colA.bound);
|
||||
|
||||
Vector3f minAy = Vector3f::min(pointAy, pointAy + colA.bound);
|
||||
Vector3f maxAy = Vector3f::max(pointAy, pointAy + colA.bound);
|
||||
|
||||
Vector3f minAz = Vector3f::min(pointAz, pointAz + colA.bound);
|
||||
Vector3f maxAz = Vector3f::max(pointAz, pointAz + colA.bound);
|
||||
|
||||
for (auto &[other, posB, colB] : this->getView()) {
|
||||
if (other.getUid() == entity->getUid())
|
||||
continue;
|
||||
|
||||
auto pointB = posB.position + colB.positionOffset;
|
||||
int collidedAxis = 0;
|
||||
|
||||
// TODO if B is also a movable we don't check with it's changing position
|
||||
Vector3f minB = Vector3f::min(pointB, pointB + colB.bound);
|
||||
Vector3f maxB = Vector3f::max(pointB, pointB + colB.bound);
|
||||
|
||||
if (collide(minA, maxA, minB, maxB)) {
|
||||
std::cout << "collided" << std::endl
|
||||
if (boxesCollide(minAx, maxAx, minB, maxB)) {
|
||||
/* std::cout << "collided" << std::endl
|
||||
<< "minA " << minA << std::endl
|
||||
<< "maxA " << maxA << std::endl
|
||||
<< "minB " << minB << std::endl
|
||||
<< "maxB " << maxB << std::endl;
|
||||
return;
|
||||
colA.onCollide(entity, other);
|
||||
colB.onCollided(entity, other);
|
||||
<< "maxB " << maxB << std::endl;*/
|
||||
//return;
|
||||
collidedAxis += CollisionComponent::CollidedAxis::X;
|
||||
}
|
||||
if (boxesCollide(minAy, maxAy, minB, maxB)) {
|
||||
collidedAxis += CollisionComponent::CollidedAxis::Y;
|
||||
}
|
||||
if (boxesCollide(minAz, maxAz, minB, maxB)) {
|
||||
collidedAxis += CollisionComponent::CollidedAxis::Z;
|
||||
}
|
||||
if (collidedAxis) {
|
||||
colA.onCollide(entity, other, collidedAxis);
|
||||
colB.onCollided(entity, other, collidedAxis);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,6 @@ namespace BBM
|
||||
CollisionSystem &operator=(const CollisionSystem &) = delete;
|
||||
|
||||
//! @brief check AABB collision
|
||||
static bool collide(Vector3f minA, Vector3f maxA, Vector3f minB, Vector3f maxB);
|
||||
static bool boxesCollide(Vector3f minA, Vector3f maxA, Vector3f minB, Vector3f maxB);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user