feat: variant-based hitboxes

This commit is contained in:
MihailRis 2026-10-31 17:15:16 +03:00
parent 563001351b
commit b8b2e4f451
16 changed files with 145 additions and 102 deletions

View file

@ -156,7 +156,6 @@ void Block::cloneTo(Block& dst) {
dst.rotatable = rotatable;
dst.grounded = grounded;
dst.hidden = hidden;
dst.hitboxes = hitboxes;
dst.rotations = rotations;
dst.pickingItem = pickingItem;
dst.scriptName = scriptName;

View file

@ -10,6 +10,7 @@
#include "maths/UVRegion.hpp"
#include "maths/aabb.hpp"
#include "typedefs.hpp"
#include "voxel.hpp"
#include "util/EnumMetadata.hpp"
#include "util/stack_vector.hpp"
#include "interfaces/Serializable.hpp"
@ -182,10 +183,14 @@ struct Variant {
CullingMode culling = CullingMode::DEFAULT;
/// @brief Influences visible block sides for transparent blocks
uint8_t drawGroup = 0;
/// @brief Set of block physical hitboxes
std::vector<AABB> hitboxes {AABB()};
struct {
/// @brief is the block completely opaque for render
bool solid = true;
/// @brief set of hitboxes sets with all coord-systems precalculated
std::vector<AABB> hitboxes[BlockRotProfile::MAX_COUNT];
} rt;
};
@ -261,9 +266,6 @@ public:
/// @brief Grounding behaviour
GroundingBehaviour groundingBehaviour = GroundingBehaviour::PARTIAL;
/// @brief Set of block physical hitboxes
std::vector<AABB> hitboxes {AABB()};
/// @brief Set of available block rotations (coord-systems)
BlockRotProfile rotations = BlockRotProfile::NONE;
@ -312,9 +314,6 @@ public:
// @brief block size is greater than 1x1x1
bool extended = false;
/// @brief set of hitboxes sets with all coord-systems precalculated
std::vector<AABB> hitboxes[BlockRotProfile::MAX_COUNT];
/// @brief set of block callbacks flags
BlockFuncsSet funcsset {};
@ -359,6 +358,13 @@ public:
return getVariantByBits(bits).model;
}
/// @brief Get precalculated hitboxes of the variant and rotation
/// by the block state
const std::vector<AABB>& getHitboxes(blockstate state) const {
return getVariantByBits(state.userbits)
.rt.hitboxes[rotatable ? state.rotation : 0];
}
static bool isReservedBlockField(std::string_view view);
};

View file

@ -70,12 +70,14 @@ const AABB* Chunks::isObstacleAt(float x, float y, float z) const {
return nullptr;
}
glm::ivec3 offset {};
if (v->state.segment) {
blockstate state = v->state;
if (state.segment) {
glm::ivec3 point(ix, iy, iz);
offset = seekOrigin(point, def, v->state) - point;
auto origin = seekOrigin(point, def, state);
offset = origin - point;
state = blocks_agent::hitbox_state(*this, origin, state);
}
const auto& boxes =
def.rotatable ? def.rt.hitboxes[v->state.rotation] : def.hitboxes;
const auto& boxes = def.getHitboxes(state);
for (const auto& hitbox : boxes) {
if (hitbox.contains(
{x - ix - offset.x, y - iy - offset.y, z - iz - offset.z}
@ -262,18 +264,18 @@ glm::vec3 Chunks::rayCastToObstacle(
const auto& def = indices.blocks.require(voxel->id);
if (def.obstacle) {
if (!def.rt.solid) {
const std::vector<AABB>& hitboxes =
def.rt.hitboxes[voxel->state.rotation];
scalar_t distance;
glm::ivec3 norm;
Ray ray(start, dir);
glm::ivec3 offset {};
if (voxel->state.segment) {
offset = seekOrigin({ix, iy, iz}, def, voxel->state) -
glm::ivec3(ix, iy, iz);
blockstate state = voxel->state;
if (state.segment) {
auto origin = seekOrigin({ix, iy, iz}, def, state);
offset = origin - glm::ivec3(ix, iy, iz);
state = blocks_agent::hitbox_state(*this, origin, state);
}
const auto& hitboxes = def.getHitboxes(state);
for (const auto& box : hitboxes) {
// norm is dummy now, can be inefficient

View file

@ -275,19 +275,19 @@ static inline voxel* raycast_blocks(
iend = {ix, iy, iz};
if (!def.rt.solid) {
const std::vector<AABB>& hitboxes =
def.rotatable ? def.rt.hitboxes[voxel->state.rotation]
: def.hitboxes;
scalar_t distance = maxDist;
Ray ray(start, dir);
bool hit = false;
glm::vec3 offset {};
if (voxel->state.segment) {
offset = seek_origin(chunks, iend, def, voxel->state) - iend;
blockstate state = voxel->state;
if (state.segment) {
auto origin = seek_origin(chunks, iend, def, state);
offset = origin - iend;
state = hitbox_state(chunks, origin, state);
}
const auto& hitboxes = def.getHitboxes(state);
for (auto box : hitboxes) {
box.a += offset;

View file

@ -264,6 +264,22 @@ inline glm::ivec3 seek_origin(
}
}
/// @brief Get block state selecting the variant hitboxes.
/// Variant bits of an extended block are stored in its origin segment
/// @param origin extended block origin position
/// @param state state of the checked segment
template <class Storage>
inline blockstate hitbox_state(
Storage& chunks, const glm::ivec3& origin, blockstate state
) {
if (state.segment) {
if (auto* voxel = get(chunks, origin.x, origin.y, origin.z)) {
state.userbits = voxel->state.userbits;
}
}
return state;
}
/// @brief Check blocks replaceability with specified block
/// @tparam Storage chunks storage class
/// @param chunks chunks storage
@ -490,12 +506,14 @@ inline std::optional<AABB> is_obstacle_at(
return std::nullopt;
}
glm::ivec3 offset {};
if (v->state.segment) {
blockstate state = v->state;
if (state.segment) {
glm::ivec3 point(ix, iy, iz);
offset = seek_origin(chunks, point, def, v->state) - point;
auto origin = seek_origin(chunks, point, def, state);
offset = origin - point;
state = hitbox_state(chunks, origin, state);
}
const auto& boxes =
def.rotatable ? def.rt.hitboxes[v->state.rotation] : def.hitboxes;
const auto& boxes = def.getHitboxes(state);
for (const auto& hitbox : boxes) {
if (hitbox.intersects(aabb - glm::ivec3(ix, iy, iz))) {