AABB block hitboxes

This commit is contained in:
MihailRis
2023-11-29 16:36:00 +03:00
parent cad1f6a669
commit 91a6426efe
7 changed files with 86 additions and 17 deletions
+11 -1
View File
@@ -3,6 +3,8 @@
#include <string>
#include "../maths/aabb.h"
#define FACE_MX 0
#define FACE_PX 1
#define FACE_MY 2
@@ -10,6 +12,8 @@
#define FACE_MZ 4
#define FACE_PZ 5
#define BLOCK_AABB_GRID 16
enum class BlockModel {
none, block, xsprite
};
@@ -29,7 +33,13 @@ public:
bool selectable = true;
bool breakable = true;
bool rotatable = false;
float hitboxScale = 1;
AABB hitbox;
struct {
bool solid = true;
bool emissive = false;
bool hitboxGrid[BLOCK_AABB_GRID][BLOCK_AABB_GRID][BLOCK_AABB_GRID];
} rt;
Block(std::string name, std::string texture);
};
+7 -10
View File
@@ -159,6 +159,7 @@ void Chunks::set(int x, int y, int z, int id, uint8_t states){
chunk->setModified(true);
}
#include <iostream>
voxel* Chunks::rayCast(vec3 start,
vec3 dir,
float maxDist,
@@ -207,20 +208,16 @@ voxel* Chunks::rayCast(vec3 start,
end.z = pz + t * dz;
// TODO: replace this dumb solution with something better
if (def && def->hitboxScale < 1.0f) {
const float sz = def->hitboxScale;
const int subs = 16;
if (def && !def->rt.solid) {
const AABB& box = def->hitbox;
const int subs = BLOCK_AABB_GRID;
iend = vec3(ix, iy, iz);
end -= iend;
for (int i = 0; i < subs; i++) {
end.x += dx / float(subs);
end.y += dy / float(subs);
end.z += dz / float(subs);
if (end.x - ix >= 0.5f - sz * 0.5f && end.x - ix <= 0.5f + sz * 0.5f &&
end.y - iy >= 0.0f && end.y - iy <= sz &&
end.z - iz >= 0.5f - sz * 0.5f && end.z - iz <= 0.5f + sz * 0.5f) {
iend.x = ix;
iend.y = iy;
iend.z = iz;
if (box.inside(end)) {
norm.x = norm.y = norm.z = 0.0f;
if (steppedIndex == 0) norm.x = -stepx;
if (steppedIndex == 1) norm.y = -stepy;