fix crouching check

This commit is contained in:
MihailRis 2026-02-24 03:34:17 +03:00
parent b52e990e40
commit ad5089f383
2 changed files with 48 additions and 41 deletions

View file

@ -47,14 +47,14 @@ struct AABB {
} }
/// @brief Multiply AABB size from center /// @brief Multiply AABB size from center
inline void scale(const glm::vec3 mul) { inline void scale(const glm::vec3& mul) {
glm::vec3 center = (a + b) * 0.5f; glm::vec3 center = (a + b) * 0.5f;
a = (a - center) * mul + center; a = (a - center) * mul + center;
b = (b - center) * mul + center; b = (b - center) * mul + center;
} }
/// @brief Multiply AABB size from given origin /// @brief Multiply AABB size from given origin
inline void scale(const glm::vec3 mul, const glm::vec3 orig) { inline void scale(const glm::vec3& mul, const glm::vec3& orig) {
glm::vec3 beg = min(); glm::vec3 beg = min();
glm::vec3 end = max(); glm::vec3 end = max();
glm::vec3 center = glm::mix(beg, end, orig); glm::vec3 center = glm::mix(beg, end, orig);
@ -101,8 +101,9 @@ struct AABB {
inline bool intersects(const AABB& aabb) const { inline bool intersects(const AABB& aabb) const {
return ( return (
a.x <= aabb.b.x && b.x >= aabb.a.x && a.y <= aabb.b.y && a.x <= aabb.b.x && b.x >= aabb.a.x &&
b.y >= aabb.a.y && a.z <= aabb.b.z && b.z >= aabb.a.z a.y <= aabb.b.y && b.y >= aabb.a.y &&
a.z <= aabb.b.z && b.z >= aabb.a.z
); );
} }

View file

@ -28,11 +28,11 @@ static float calc_step_height(
aabb.scale(glm::vec3(1.0f - E * 2, 1.0f, 1.0f - E * 2)); aabb.scale(glm::vec3(1.0f - E * 2, 1.0f, 1.0f - E * 2));
aabb = aabb + pos + glm::vec3(0.0f, stepHeight, 0.0f); aabb = aabb + pos + glm::vec3(0.0f, stepHeight, 0.0f);
if (stepHeight > 0.0f) { if (stepHeight > 0.0f) {
for (int ix = 0; ix <= glm::ceil((half.x-E) * 2); ix++) { for (int ix = 0; ix <= glm::ceil((half.x - E) * 2); ix++) {
float x = (pos.x-half.x) + ix; float x = (pos.x - half.x) + ix;
for (int iz = 0; iz <= glm::ceil((half.z-E)*2); iz++) { for (int iz = 0; iz <= glm::ceil((half.z - E) * 2); iz++) {
float z = (pos.z-half.z) + iz; float z = (pos.z - half.z) + iz;
if (chunks.isObstacleAt(x, pos.y+half.y+stepHeight, z, aabb)) { if (chunks.isObstacleAt(x, pos.y + half.y + stepHeight, z, aabb)) {
return 0.0f; return 0.0f;
} }
} }
@ -44,22 +44,23 @@ static float calc_step_height(
template <int nx, int ny, int nz> template <int nx, int ny, int nz>
static void calc_collision_pos( static void calc_collision_pos(
const GlobalChunks& chunks, const GlobalChunks& chunks,
const std::vector<Hitbox*>& solidHitboxes,
glm::vec3& pos, glm::vec3& pos,
glm::vec3& vel, glm::vec3& vel,
const glm::vec3& half, const glm::vec3& half,
float stepHeight, float stepHeight,
bool collided[3] bool (&collided)[3]
) { ) {
glm::vec3 offset(0.0f, stepHeight + E, 0.0f);
if (vel[nx] <= 0.0f) { if (vel[nx] <= 0.0f) {
return; return;
} }
glm::vec3 offset(0.0f, stepHeight + E * 2, 0.0f);
for (int iy = 0; iy <= glm::ceil(((half - offset * 0.5f)[ny] - E) * 2); iy++) { for (int iy = 0; iy <= glm::ceil(((half - offset * 0.5f)[ny] - E) * 2); iy++) {
glm::vec3 coord; glm::vec3 coord;
coord[ny] = ((pos + offset)[ny] - half[ny] + E) + iy; coord[ny] = ((pos + offset)[ny] - half[ny] + E) + iy;
for (int iz = 0; iz <= glm::ceil((half[nz] - E) * 2); iz++) { for (int iz = 0; iz <= glm::ceil((half[nz] - E) * 2); iz++) {
coord[nz] = (pos[nz] - half[nz] + E) + iz; coord[nz] = (pos[nz] - half[nz] + E) + iz;
coord[nx] = (pos[nx] + half[nx] + E) + E; coord[nx] = pos[nx] + half[nx] + E * 2;
auto boxAABB = AABB(pos - half, pos + half); auto boxAABB = AABB(pos - half, pos + half);
glm::vec3 scale(1.0f); glm::vec3 scale(1.0f);
@ -84,22 +85,23 @@ static void calc_collision_pos(
template <int nx, int ny, int nz> template <int nx, int ny, int nz>
static bool calc_collision_neg( static bool calc_collision_neg(
const GlobalChunks& chunks, const GlobalChunks& chunks,
const std::vector<Hitbox*>& solidHitboxes,
glm::vec3& pos, glm::vec3& pos,
glm::vec3& vel, glm::vec3& vel,
const glm::vec3& half, const glm::vec3& half,
float stepHeight, float stepHeight,
bool collided[3] bool (&collided)[3]
) { ) {
glm::vec3 offset(0.0f, stepHeight + E, 0.0f);
if (vel[nx] >= 0.0f) { if (vel[nx] >= 0.0f) {
return false; return false;
} }
glm::vec3 offset(0.0f, stepHeight + E, 0.0f);
for (int iy = 0; iy <= glm::ceil(((half - offset * 0.5f)[ny] - E) * 2); iy++) { for (int iy = 0; iy <= glm::ceil(((half - offset * 0.5f)[ny] - E) * 2); iy++) {
glm::vec3 coord; glm::vec3 coord;
coord[ny] = ((pos + offset)[ny] - half[ny] + E) + iy; coord[ny] = ((pos + offset)[ny] - half[ny] + E) + iy;
for (int iz = 0; iz <= glm::ceil((half[nz] - E) * 2); iz++) { for (int iz = 0; iz <= glm::ceil((half[nz] - E) * 2); iz++) {
coord[nz] = (pos[nz] - half[nz] + E) + iz; coord[nz] = (pos[nz] - half[nz] + E) + iz;
coord[nx] = (pos[nx] - half[nx] - E) - E; coord[nx] = pos[nx] - half[nx] - E * 2;
auto boxAABB = AABB(pos - half, pos + half); auto boxAABB = AABB(pos - half, pos + half);
glm::vec3 scale(1.0f); glm::vec3 scale(1.0f);
@ -130,21 +132,21 @@ static bool calc_collision_neg_y(
const glm::vec3& half, const glm::vec3& half,
glm::vec3& groundVelocty glm::vec3& groundVelocty
) { ) {
for (const auto& box : solidHitboxes) { for (auto box : solidHitboxes) {
if (glm::distance2(box->position, pos) < E) { if (glm::distance2(box->position, pos) < E) {
continue; continue;
} }
auto boxhalf = box->getHalfSize(); auto boxhalf = box->getHalfSize();
if (box->getAABB().intersects(AABB(pos - half, pos + half))) { if (box->getAABB().intersects(AABB(pos - half, pos + half))) {
if (vel.y < 0.0f) { float newy = box->position.y + boxhalf.y + half.y;
vel.y = 0.0f; if (pos.y < newy) {
} pos.y = newy;
float newx = box->position.y + boxhalf.y + half.y;
if (newx - pos.y <= 0.5f) {
pos.y = newx;
} }
groundVelocty = box->velocity; groundVelocty = box->velocity;
return true; if (vel.y < 0.0f) {
vel.y = 0.0f;
return true;
}
} }
} }
if (vel.y >= 0.0f) { if (vel.y >= 0.0f) {
@ -155,8 +157,8 @@ static bool calc_collision_neg_y(
glm::vec3 coord; glm::vec3 coord;
coord.x = (pos.x-half.x+E) + ix; coord.x = (pos.x-half.x+E) + ix;
for (int iz = 0; iz <= glm::ceil((half.z-E)*2); iz++){ for (int iz = 0; iz <= glm::ceil((half.z-E)*2); iz++){
coord.z = (pos.z-half.z+E) + iz; coord.z = (pos.z - half.z + E) + iz;
coord.y = (pos.y-half.y-E) - E; coord.y = pos.y - half.y - E * 2;
auto boxAABB = AABB(pos - half, pos + half); auto boxAABB = AABB(pos - half, pos + half);
glm::vec3 scale(1.0f); glm::vec3 scale(1.0f);
@ -192,11 +194,11 @@ void PhysicsSolver::calcCollisions(
auto prevPos = pos; auto prevPos = pos;
calc_collision_neg<0, 1, 2>(chunks, pos, vel, half, stepHeight, hitbox.collided); calc_collision_neg<0, 1, 2>(chunks, solidHitboxes, pos, vel, half, stepHeight, hitbox.collided);
calc_collision_pos<0, 1, 2>(chunks, pos, vel, half, stepHeight, hitbox.collided); calc_collision_pos<0, 1, 2>(chunks, solidHitboxes, pos, vel, half, stepHeight, hitbox.collided);
calc_collision_neg<2, 1, 0>(chunks, pos, vel, half, stepHeight, hitbox.collided); calc_collision_neg<2, 1, 0>(chunks, solidHitboxes, pos, vel, half, stepHeight, hitbox.collided);
calc_collision_pos<2, 1, 0>(chunks, pos, vel, half, stepHeight, hitbox.collided); calc_collision_pos<2, 1, 0>(chunks, solidHitboxes, pos, vel, half, stepHeight, hitbox.collided);
if (calc_collision_neg_y(chunks, solidHitboxes, pos, vel, half, hitbox.groundVelocity)) { if (calc_collision_neg_y(chunks, solidHitboxes, pos, vel, half, hitbox.groundVelocity)) {
hitbox.grounded = true; hitbox.grounded = true;
@ -233,12 +235,10 @@ void PhysicsSolver::calcCollisions(
// step on // step on
if (stepHeight > 0.0 && vel.y <= 0.0f) { if (stepHeight > 0.0 && vel.y <= 0.0f) {
AABB boxAABB = AABB(-half, +half); AABB boxAABB = AABB(pos - half, pos + half);
boxAABB.scale(glm::vec3(1.0f - E * 2, 1.0f - E * 2, 1.0f - E * 2)); boxAABB.scale(glm::vec3(1.0f - E * 2, 1.0f - E * 2, 1.0f - E * 2));
boxAABB = boxAABB.translated(pos);
for (int ix = 0; ix <= glm::ceil((half.x - E) * 2); ix++) { for (int ix = 0; ix <= glm::ceil((half.x - E) * 2); ix++) {
float x = (pos.x-half.x) + ix; float x = (pos.x - half.x) + ix;
for (int iz = 0; iz <= glm::ceil((half.z - E) * 2); iz++) { for (int iz = 0; iz <= glm::ceil((half.z - E) * 2); iz++) {
float z = (pos.z - half.z) + iz; float z = (pos.z - half.z) + iz;
float y = (pos.y - half.y + E) + E; float y = (pos.y - half.y + E) + E;
@ -252,7 +252,7 @@ void PhysicsSolver::calcCollisions(
} }
} }
} }
for (const auto& box : solidHitboxes) { for (auto box : solidHitboxes) {
if (glm::distance2(box->position, pos) < E) { if (glm::distance2(box->position, pos) < E) {
continue; continue;
} }
@ -306,19 +306,23 @@ void PhysicsSolver::calcSubstep(
for (int axis = 0; axis <= 2; axis += 2) { for (int axis = 0; axis <= 2; axis += 2) {
hitbox.grounded = false; hitbox.grounded = false;
AABB aabb(pos - half, pos + half); auto checkPos = pos;
aabb.scale(glm::vec3(1.0f - E * 4, 1.0f, 1.0f - E * 4)); checkPos.x = axis != 0 ? initpos.x : pos.x;
checkPos.z = axis == 0 ? initpos.z : pos.z;
AABB boxAABB(checkPos - half, checkPos + half);
boxAABB.scale(glm::vec3(1.0f - E * 4, 1.0f, 1.0f - E * 4));
for (int ix = 0; ix <= glm::ceil((half.x - E) * 2); ix++) { for (int ix = 0; ix <= glm::ceil((half.x - E) * 2); ix++) {
float x = (initpos.x - half.x + E) + ix; float x = (pos.x - half.x + E) + ix;
for (int iz = 0; iz <= glm::ceil((half.z - E) * 2); iz++){ for (int iz = 0; iz <= glm::ceil((half.z - E) * 2); iz++){
float z = (pos.z - half.z + E) + iz; float z = (pos.z - half.z + E) + iz;
if (chunks.isObstacleAt(x, y, z, aabb)){ if (chunks.isObstacleAt(x, y, z, boxAABB)){
hitbox.grounded = true; hitbox.grounded = true;
break; break;
} }
} }
} }
for (const auto& box : solidHitboxes) { for (auto box : solidHitboxes) {
if (glm::distance2(box->position, pos) < E) { if (glm::distance2(box->position, pos) < E) {
continue; continue;
} }
@ -395,5 +399,7 @@ void PhysicsSolver::step(
} }
void PhysicsSolver::removeSensor(Sensor* sensor) { void PhysicsSolver::removeSensor(Sensor* sensor) {
sensors.erase(std::remove(sensors.begin(), sensors.end(), sensor), sensors.end()); sensors.erase(
std::remove(sensors.begin(), sensors.end(), sensor), sensors.end()
);
} }