giving the collided Axis for context to callback on collisions

This commit is contained in:
Clément Le Bihan
2021-06-08 00:19:34 +02:00
parent 65af11f3f9
commit 22606ecf68
7 changed files with 70 additions and 43 deletions
+34 -11
View File
@@ -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);
}
}
}