fix: correct velocity clamping and hitbox loading

Preserve sign when clamping velocity; negative velocities now clamp to zero instead of flipping direction. Support loading hitbox definitions from JSON arrays and objects. Tune pig entity movement, spawn height, and wander probabilities.
This commit is contained in:
2026-08-02 16:14:56 +08:00
parent a54858e4c6
commit 9bb1882d92
5 changed files with 26 additions and 16 deletions

View File

@@ -86,15 +86,19 @@ HitboxManager::Handle HitboxManager::load(std::string_view name) {
std::ifstream s{p}; std::ifstream s{p};
json j = json::parse(s); json j = json::parse(s);
if (j.contains("boxes")) { if (j.contains("boxes")) {
if (j["boxes"].contains("center")) { const json* box = &j["boxes"];
center.x = j["boxes"]["center"].at(0).get<float>(); if (box->is_array() && !box->empty()) {
center.y = j["boxes"]["center"].at(1).get<float>(); box = &(*box)[0];
center.z = j["boxes"]["center"].at(2).get<float>();
} }
if (j["boxes"].contains("half")) { if (box->contains("center")) {
half.x = j["boxes"]["half"].at(0).get<float>(); center.x = (*box)["center"].at(0).get<float>();
half.y = j["boxes"]["half"].at(1).get<float>(); center.y = (*box)["center"].at(1).get<float>();
half.z = j["boxes"]["half"].at(2).get<float>(); center.z = (*box)["center"].at(2).get<float>();
}
if (box->contains("half")) {
half.x = (*box)["half"].at(0).get<float>();
half.y = (*box)["half"].at(1).get<float>();
half.z = (*box)["half"].at(2).get<float>();
} }
} }
{ {

View File

@@ -20,6 +20,9 @@ void ServerEntityManager::init() {
m_factories.try_emplace("cubed:pig", [this]() { m_factories.try_emplace("cubed:pig", [this]() {
BaseServerCreature c; BaseServerCreature c;
c.hitbox = HitboxManager::instance().get_hitbox_id("cubed:pig"); c.hitbox = HitboxManager::instance().get_hitbox_id("cubed:pig");
c.gravity.value = 1.0f;
c.movement.acceleration = 1.0f;
c.movement.deceleration = 1.0f;
return create_entity_in_factory( return create_entity_in_factory(
Entity{m_next}, EntityInfo{"cubed:pig", ""}, std::move(c), PigTag{}, Entity{m_next}, EntityInfo{"cubed:pig", ""}, std::move(c), PigTag{},
AIBase{}, WanderAITag{}, MoveBoost{}); AIBase{}, WanderAITag{}, MoveBoost{});

View File

@@ -179,7 +179,7 @@ void ServerWorld::send_chunk(int task_id, const std::string& uuid,
void ServerWorld::init_world() { void ServerWorld::init_world() {
m_entity_manager.init(); m_entity_manager.init();
m_entity_manager.add_entity("cubed:pig", {0, 90, 0}); m_entity_manager.add_entity("cubed:pig", {0, 225, 0});
register_timer("player disconnect", 5, [this]() { register_timer("player disconnect", 5, [this]() {
std::vector<std::string> disconnect; std::vector<std::string> disconnect;
{ {

View File

@@ -23,11 +23,14 @@ void SpeedSystem::update(float dt, entt::registry& registry) {
} }
creature.velocity.value.y += -creature.gravity.value * dt; creature.velocity.value.y += -creature.gravity.value * dt;
auto v_clamp = [](float& v, float max) { auto v_clamp = [](float& v, float max) {
auto temp = std::abs(v); if (max < 0.0f) {
max = std::abs(max); return;
temp = std::clamp(temp, 0.0f, max); }
auto sign = v < 0.0f ? -1.0f : 1.0f;
v = temp; v = sign * std::clamp(std::abs(v), 0.0f, max);
if (v < 0.0f) {
v = 0.0f;
}
}; };
v_clamp(creature.velocity.value.x, creature.velocity.max.x); v_clamp(creature.velocity.value.x, creature.velocity.max.x);
v_clamp(creature.velocity.value.y, creature.velocity.max.y); v_clamp(creature.velocity.value.y, creature.velocity.max.y);

View File

@@ -5,8 +5,8 @@
#include "Cubed/tools/cubed_random.hpp" #include "Cubed/tools/cubed_random.hpp"
namespace { namespace {
constexpr double DIRECTION_PROBABILITY = 0.1; constexpr double DIRECTION_PROBABILITY = 0.01;
constexpr double MOVE_PROBABILITY = 0.1; constexpr double MOVE_PROBABILITY = 0.01;
} // namespace } // namespace
namespace Cubed { namespace Cubed {