About to implement physics

This commit is contained in:
2023-03-19 10:17:33 -07:00
parent 704b8f38ef
commit e1d991916c
9 changed files with 109 additions and 69 deletions

View File

@ -15,18 +15,18 @@ bool_t Dawn::aabb3dIntersect(struct AABB3D cube1, struct AABB3D cube2) {
);
}
bool aabbSweep(
const glm::vec3& pos1, const glm::vec3& size1, const glm::vec3& vel1,
const glm::vec3& pos2, const glm::vec3& size2, const glm::vec3& vel2,
float& fraction
bool_t aabb3dSweep(
struct AABB3D cube1, glm::vec3 velocity1,
struct AABB3D cube2, glm::vec3 velocity2,
glm::vec3 *normal
) {
glm::vec3 relVel = vel1 - vel2;
glm::vec3 relPos = pos1 - pos2;
// If the relative velocity is zero, the cubes are already intersecting
i (glm::length2(relVel) == 0.0f) {
fraction = 0.0f;
return true;
if(glm::length2(relVel) == 0.0f) {
fraction = 0.0f;
return true;
}
// Expand the size of the cubes by the magnitude of the relative velocity
@ -34,29 +34,29 @@ bool aabbSweep(
glm::vec3 size2exp = size2 + glm::abs(relVel);
// Compute the times at which the cubes first and last overlap on each axis
float tminx = (size1exp.x + size2exp.x - glm::abs(relPos.x)) / glm::abs(relVel.x);
float tmaxx = (size1.x + size2.x - glm::abs(relPos.x)) / glm::abs(relVel.x);
float tminy = (size1exp.y + size2exp.y - glm::abs(relPos.y)) / glm::abs(relVel.y);
float tmaxy = (size1.y + size2.y - glm::abs(relPos.y)) / glm::abs(relVel.y);
float tminz = (size1exp.z + size2exp.z - glm::abs(relPos.z)) / glm::abs(relVel.z);
float tmaxz = (size1.z + size2.z - glm::abs(relPos.z)) / glm::abs(relVel.z);
float_t tminx = (size1exp.x + size2exp.x - glm::abs(relPos.x)) / glm::abs(relVel.x);
float_t tmaxx = (size1.x + size2.x - glm::abs(relPos.x)) / glm::abs(relVel.x);
float_t tminy = (size1exp.y + size2exp.y - glm::abs(relPos.y)) / glm::abs(relVel.y);
float_t tmaxy = (size1.y + size2.y - glm::abs(relPos.y)) / glm::abs(relVel.y);
float_t tminz = (size1exp.z + size2exp.z - glm::abs(relPos.z)) / glm::abs(relVel.z);
float_t tmaxz = (size1.z + size2.z - glm::abs(relPos.z)) / glm::abs(relVel.z);
// Find the earliest and latest times of overlap
float tmin = glm::max(glm::max(glm::min(tminx, tmaxx), glm::min(tminy, tmaxy)), glm::min(tminz, tmaxz));
float tmax = glm::min(glm::min(glm::max(tminx, tmaxx), glm::max(tminy, tmaxy)), glm::max(tminz, tmaxz));
float_t tmin = glm::max(glm::max(glm::min(tminx, tmaxx), glm::min(tminy, tmaxy)), glm::min(tminz, tmaxz));
float_t tmax = glm::min(glm::min(glm::max(tminx, tmaxx), glm::max(tminy, tmaxy)), glm::max(tminz, tmaxz));
// 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) {
fraction = 1.0f;
return false;
fraction = 1.0f;
return false;
}
// If the latest time of overlap is less than or equal to zero, the cubes are already
// colliding and will collide throughout the timestep
if (tmax <= 0.0f) {
fraction = 0.0f;
return true;
if(tmax <= 0.0f) {
fraction = 0.0f;
return true;
}
// Compute the fraction of the timestep at which the collision occurs

View File

@ -22,4 +22,9 @@ namespace Dawn {
bool_t aabb3dIntersect(struct AABB3D cube1, struct AABB3D cube2);
bool_t aabb3dSweep(
glm::vec3 pos1, glm::vec3 size1, glm::vec3 vel1,
glm::vec3 pos2, glm::vec3 size2, glm::vec3 vel2,
float_t& fraction
);
}

View File

@ -29,7 +29,6 @@ bool_t Dawn::raytestSphere(
*normal = glm::normalize(*hit - sphere.center);
*distance = t0;
return true;
}