Basic hit detection

This commit is contained in:
2023-03-21 14:42:54 -07:00
parent 3a790e57c9
commit 978c420046
20 changed files with 343 additions and 41 deletions

View File

@ -12,4 +12,70 @@ bool_t boxIsPointInside(glm::vec2 point, glm::vec2 min, glm::vec2 max) {
point.x >= min.x && point.x <= max.x &&
point.y >= min.y && point.y <= max.y
);
}
bool_t Dawn::boxCheckCollision(
glm::vec2 posA, glm::vec2 minA, glm::vec2 maxA,
glm::vec2 posB, glm::vec2 minB, glm::vec2 maxB,
glm::vec2 velocity,
glm::vec2 &normal,
float_t &entryTime,
float_t &exitTime,
glm::vec2 &entryPoint,
glm::vec2 &exitPoint
) {
// Calculate the time intervals of overlap in the x and y axes
glm::vec2 invEntry = glm::vec2(0.0f);
glm::vec2 invExit = glm::vec2(0.0f);
if (velocity.x >= 0.0f) {
invEntry.x = ((minB.x + posB.x) - (maxA.x + posA.x)) / velocity.x;
invExit.x = ((maxB.x + posB.x) - (minA.x + posA.x)) / velocity.x;
} else {
invEntry.x = ((maxB.x + posB.x) - (minA.x + posA.x)) / velocity.x;
invExit.x = ((minB.x + posB.x) - (maxA.x + posA.x)) / velocity.x;
}
if (velocity.y >= 0.0f) {
invEntry.y = ((minB.y + posB.y) - (maxA.y + posA.y)) / velocity.y;
invExit.y = ((maxB.y + posB.y) - (minA.y + posA.y)) / velocity.y;
} else {
invEntry.y = ((maxB.y + posB.y) - (minA.y + posA.y)) / velocity.y;
invExit.y = ((minB.y + posB.y) - (maxA.y + posA.y)) / velocity.y;
}
// Calculate the time of entry and exit for each axis
glm::vec2 entry = glm::max(invEntry, glm::vec2(0.0f));
glm::vec2 exit = glm::min(invExit, glm::vec2(1.0f));
// Find the time of entry and exit
entryTime = glm::max(entry.x, entry.y);
exitTime = glm::min(exit.x, exit.y);
// If there is no overlap in either axis, there is no collision
if (entryTime > exitTime || entry.x < 0.0f && entry.y < 0.0f || entry.x > 1.0f || entry.y > 1.0f) {
return false;
}
// Calculate the normal of the collision
if (entry.x > entry.y) {
if (invEntry.x < 0.0f) {
normal = glm::vec2(1.0f, 0.0f);
} else {
normal = glm::vec2(-1.0f, 0.0f);
}
} else {
if (invEntry.y < 0.0f) {
normal = glm::vec2(0.0f, 1.0f);
} else {
normal = glm::vec2(0.0f, -1.0f);
}
}
// Calculate the entry and exit points
entryPoint = posA + velocity * entryTime;
exitPoint = posA + velocity * exitTime;
return true;
}

View File

@ -6,8 +6,20 @@
#pragma once
#include "dawnlibs.hpp"
#include "assert/assert.hpp"
#include "util/mathutils.hpp"
namespace Dawn {
bool_t boxCheckCollision(
glm::vec2 posA, glm::vec2 minA, glm::vec2 maxA,
glm::vec2 posB, glm::vec2 minB, glm::vec2 maxB,
glm::vec2 velocity,
glm::vec2 &normal,
float_t &entryTime,
float_t &exitTime,
glm::vec2 &entryPoint,
glm::vec2 &exitPoint
);
/**
* Checks if a given point is within the 2D Boundaries of an object.
*

View File

@ -18,7 +18,7 @@ bool_t Ray2D::intersects(struct Ray2D ray, glm::vec2 *out) {
float_t f = -ray.direction.x * direction.y + direction.x * ray.direction.y;
float_t s = (-direction.y * j.x + direction.x * j.y) / f;
float_t t = (ray.direction.x * j.y - ray.direction.y * j.x) / f;
if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
if(s >= 0 && s <= 1 && t >= 0 && t <= 1) {
*out = position + (t * direction);
return true;
}

View File

@ -47,7 +47,7 @@ bool_t aabb3dSweep(
// If the earliest time of overlap is greater than the length of the timestep, the cubes
// will not collide during the timestep
if (tmin > 1.0f) {
if(tmin > 1.0f) {
fraction = 1.0f;
return false;
}

View File

@ -187,9 +187,9 @@ bool_t Dawn::raytestQuad(
// perform ray-quad intersection test
float_t t = -localRayOrigin.z / localRayDirection.z; // intersection distance along ray
if (t < 0) return false; // intersection is behind the ray origin
if(t < 0) return false; // intersection is behind the ray origin
glm::vec2 intersectionPoint = glm::vec2(localRayOrigin) + t * glm::vec2(localRayDirection);
if (
if(
glm::any(glm::lessThan(intersectionPoint, min)) ||
glm::any(glm::greaterThan(intersectionPoint, max))
) {