add solid entities (wip)

This commit is contained in:
MihailRis 2026-02-23 17:34:24 +03:00
parent 3cd6ce1df2
commit 0a34f9b767
7 changed files with 158 additions and 116 deletions

View file

@ -88,5 +88,6 @@ template<> void ContentUnitLoader<EntityDef>::loadUnit(
root.at("skeleton-name").get(def.skeletonName); root.at("skeleton-name").get(def.skeletonName);
root.at("blocking").get(def.blocking); root.at("blocking").get(def.blocking);
root.at("solid").get(def.solid);
root.at("step-height").get(def.stepHeight); root.at("step-height").get(def.stepHeight);
} }

View file

@ -278,13 +278,17 @@ void Entities::updateSensors(
} }
void Entities::preparePhysics(float delta) { void Entities::preparePhysics(float delta) {
auto& physics = *level.physics;
auto& solidHitboxes = physics.getSolidHitboxesWriteable();
if (sensorsTickClock.update(delta)) { if (sensorsTickClock.update(delta)) {
auto part = sensorsTickClock.getPart(); auto part = sensorsTickClock.getPart();
auto parts = sensorsTickClock.getParts(); auto parts = sensorsTickClock.getParts();
auto& sensors = physics.getSensorsWriteable();
auto view = registry->view<EntityId, Transform, Rigidbody>(); auto view = registry->view<EntityId, Transform, Rigidbody>();
auto physics = level.physics.get();
std::vector<Sensor*> sensors;
for (auto [entity, eid, transform, rigidbody] : view.each()) { for (auto [entity, eid, transform, rigidbody] : view.each()) {
if (!rigidbody.enabled) { if (!rigidbody.enabled) {
continue; continue;
@ -294,7 +298,16 @@ void Entities::preparePhysics(float delta) {
} }
updateSensors(rigidbody, transform, sensors); updateSensors(rigidbody, transform, sensors);
} }
physics->setSensors(std::move(sensors)); }
solidHitboxes.clear();
auto view = registry->view<EntityId, Rigidbody>();
for (auto [entity, eid, rigidbody] : view.each()) {
if (!eid.def.solid || eid.destroyFlag || !rigidbody.enabled) {
continue;
}
solidHitboxes.emplace_back(&rigidbody.hitbox);
} }
} }

View file

@ -9,5 +9,7 @@ void EntityDef::cloneTo(EntityDef& dst) {
dst.radialSensors = radialSensors; dst.radialSensors = radialSensors;
dst.skeletonName = skeletonName; dst.skeletonName = skeletonName;
dst.blocking = blocking; dst.blocking = blocking;
dst.solid = solid;
dst.stepHeight = stepHeight;
dst.save = save; dst.save = save;
} }

View file

@ -42,6 +42,9 @@ struct EntityDef {
/// @brief Does entity prevent blocks setup /// @brief Does entity prevent blocks setup
bool blocking = true; bool blocking = true;
/// @brief Is the entity solid (blocks other entities)
bool solid = false;
/// @brief Max obstacle height that does not require jumping /// @brief Max obstacle height that does not require jumping
float stepHeight = 0.5f; float stepHeight = 0.5f;

View file

@ -60,6 +60,7 @@ struct Hitbox {
float gravityScale = 1.0f; float gravityScale = 1.0f;
bool crouching = false; bool crouching = false;
float stepHeight = 0.5f; float stepHeight = 0.5f;
glm::vec3 groundVelocity {};
Hitbox(BodyType type, glm::vec3 position, glm::vec3 halfsize); Hitbox(BodyType type, glm::vec3 position, glm::vec3 halfsize);

View file

@ -5,6 +5,7 @@
#include "voxels/Block.hpp" #include "voxels/Block.hpp"
#include "voxels/GlobalChunks.hpp" #include "voxels/GlobalChunks.hpp"
#include "voxels/voxel.hpp" #include "voxels/voxel.hpp"
#include "objects/Entities.hpp"
#include "debug/Logger.hpp" #include "debug/Logger.hpp"
#include <algorithm> #include <algorithm>
@ -12,11 +13,84 @@
#include <glm/gtx/norm.hpp> #include <glm/gtx/norm.hpp>
inline const float E = 0.03f; inline const float E = 0.03f;
inline const float MAX_FIX = 0.1f;
static debug::Logger logger("physics-solver"); static debug::Logger logger("physics-solver");
PhysicsSolver::PhysicsSolver(glm::vec3 gravity) : gravity(gravity) {} PhysicsSolver::PhysicsSolver(glm::vec3 gravity) : gravity(std::move(gravity)) {}
void PhysicsSolver::calcSubstep(
const GlobalChunks& chunks,
Hitbox& hitbox,
glm::vec3& vel,
glm::vec3& pos,
bool prevGrounded,
float dt
) {
auto initpos = pos;
auto half = hitbox.getHalfSize();
float gravityScale = hitbox.gravityScale;
if (hitbox.type == BodyType::DYNAMIC) {
calcCollisions(
chunks,
hitbox,
vel,
pos,
half,
(prevGrounded && gravityScale > 0.0f) ? hitbox.stepHeight : 0.0f
);
}
vel += gravity * dt * gravityScale;
pos += vel * dt + gravity * gravityScale * dt * dt * 0.5f;
if (hitbox.grounded && pos.y < initpos.y) {
pos.y = initpos.y;
}
if (prevGrounded && !hitbox.grounded) {
vel += hitbox.groundVelocity;
hitbox.groundVelocity = {};
}
if (!hitbox.crouching || !hitbox.grounded) {
return;
}
float y = (pos.y-half.y-E);
for (int axis = 0; axis <= 2; axis += 2) {
hitbox.grounded = false;
AABB aabb(pos - half, pos + half);
aabb.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++) {
float x = (initpos.x - half.x + E) + ix;
for (int iz = 0; iz <= glm::ceil((half.z - E) * 2); iz++){
float z = (pos.z - half.z + E) + iz;
if (chunks.isObstacleAt(x, y, z, aabb)){
hitbox.grounded = true;
break;
}
}
}
for (const auto& box : solidHitboxes) {
if (glm::distance2(box->position, pos) < E) {
continue;
}
auto boxhalf = box->getHalfSize();
if (AABB(box->position - boxhalf, box->position + boxhalf)
.intersects(AABB(pos - half, pos + half))) {
hitbox.grounded = true;
break;
}
}
if (!hitbox.grounded) {
pos[axis] = initpos[axis];
vel[axis] = 0.0f;
}
}
hitbox.grounded = true;
}
void PhysicsSolver::step( void PhysicsSolver::step(
const GlobalChunks& chunks, const GlobalChunks& chunks,
@ -28,72 +102,13 @@ void PhysicsSolver::step(
float dt = delta / static_cast<float>(substeps); float dt = delta / static_cast<float>(substeps);
float linearDamping = hitbox.linearDamping * hitbox.friction; float linearDamping = hitbox.linearDamping * hitbox.friction;
auto half = hitbox.getHalfSize();
glm::vec3& pos = hitbox.position; glm::vec3& pos = hitbox.position;
glm::vec3& vel = hitbox.velocity; glm::vec3& vel = hitbox.velocity;
float gravityScale = hitbox.gravityScale;
bool prevGrounded = hitbox.grounded; bool prevGrounded = hitbox.grounded;
hitbox.grounded = false; hitbox.grounded = false;
for (uint i = 0; i < substeps; i++) { for (uint i = 0; i < substeps; i++) {
float px = pos.x; calcSubstep(chunks, hitbox, vel, pos, prevGrounded, dt);
float py = pos.y;
float pz = pos.z;
if (hitbox.type == BodyType::DYNAMIC) {
colisionCalc(chunks, hitbox, vel, pos, half,
(prevGrounded && gravityScale > 0.0f) ? hitbox.stepHeight : 0.0f);
}
vel += gravity * dt * gravityScale;
pos += vel * dt + gravity * gravityScale * dt * dt * 0.5f;
if (hitbox.grounded && pos.y < py) {
pos.y = py;
}
if (hitbox.crouching && hitbox.grounded){
AABB aabb;
float y = (pos.y-half.y-E);
hitbox.grounded = false;
aabb = AABB(pos - half, pos + half);
aabb.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++) {
float x = (px-half.x+E) + ix;
for (int iz = 0; iz <= glm::ceil((half.z-E)*2); iz++){
float z = (pos.z-half.z+E) + iz;
if (chunks.isObstacleAt(x,y,z, aabb)){
hitbox.grounded = true;
break;
}
}
}
if (!hitbox.grounded) {
pos.z = pz;
vel.z = 0.0f;
}
hitbox.grounded = false;
aabb = AABB(pos - half, pos + half);
aabb.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++) {
float x = (pos.x-half.x+E) + ix;
for (int iz = 0; iz <= glm::ceil((half.z-E)*2); iz++){
float z = (pz-half.z+E) + iz;
if (chunks.isObstacleAt(x,y,z, aabb)){
hitbox.grounded = true;
break;
}
}
}
if (!hitbox.grounded) {
pos.x = px;
vel.x = 0.0f;
}
hitbox.grounded = true;
}
} }
vel.x /= 1.0f + delta * linearDamping; vel.x /= 1.0f + delta * linearDamping;
vel.z /= 1.0f + delta * linearDamping; vel.z /= 1.0f + delta * linearDamping;
@ -194,19 +209,22 @@ static bool calc_collision_neg(
static bool calc_collision_neg_y( static bool calc_collision_neg_y(
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,
glm::vec3& groundVelocty
) { ) {
if (vel.y >= 0.0f) { if (vel.y >= 0.0f) {
return false; return false;
} }
groundVelocty = {};
for (int ix = 0; ix <= glm::ceil((half.x-E)*2); ix++) { for (int ix = 0; ix <= glm::ceil((half.x-E)*2); ix++) {
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); coord.y = (pos.y-half.y-E) - E;
auto boxAABB = AABB(pos - half, pos + half); auto boxAABB = AABB(pos - half, pos + half);
glm::vec3 scale(1.0f); glm::vec3 scale(1.0f);
@ -215,15 +233,30 @@ static bool calc_collision_neg_y(
boxAABB.scale(scale); boxAABB.scale(scale);
if (const auto aabb = chunks.isObstacleAt(coord.x, coord.y, coord.z, boxAABB)) { if (const auto aabb = chunks.isObstacleAt(coord.x, coord.y, coord.z, boxAABB)) {
vel.y = 0.0f; float newy = std::floor(coord.y) + half.y + aabb->max().y;
float newx = std::floor(coord.y) + half.y + aabb->max().y; if (newy > pos.y) {
if (newx - pos.y <= E) { vel.y = 0.0f;
pos.y = newx; pos.y = newy;
} }
return true; return true;
} }
} }
} }
for (const auto& box : solidHitboxes) {
if (glm::distance2(box->position, pos) < E) {
continue;
}
auto boxhalf = box->getHalfSize();
if (AABB(box->position - boxhalf, box->position + boxhalf).intersects(AABB(pos - half, pos + half))) {
vel.y = 0.0f;
float newx = box->position.y + boxhalf.y + half.y;
if (newx - pos.y <= 0.5f) {
pos.y = newx;
}
groundVelocty = box->velocity;
return true;
}
}
return false; return false;
} }
@ -265,7 +298,7 @@ static void calc_collision_pos(
} }
} }
void PhysicsSolver::colisionCalc( void PhysicsSolver::calcCollisions(
const GlobalChunks& chunks, const GlobalChunks& chunks,
Hitbox& hitbox, Hitbox& hitbox,
glm::vec3& vel, glm::vec3& vel,
@ -283,7 +316,7 @@ void PhysicsSolver::colisionCalc(
calc_collision_neg<2, 1, 0>(chunks, pos, vel, half, stepHeight); calc_collision_neg<2, 1, 0>(chunks, pos, vel, half, stepHeight);
calc_collision_pos<2, 1, 0>(chunks, pos, vel, half, stepHeight); calc_collision_pos<2, 1, 0>(chunks, pos, vel, half, stepHeight);
if (calc_collision_neg_y(chunks, pos, vel, half)) { if (calc_collision_neg_y(chunks, solidHitboxes, pos, vel, half, hitbox.groundVelocity)) {
hitbox.grounded = true; hitbox.grounded = true;
} }
@ -300,7 +333,7 @@ void PhysicsSolver::colisionCalc(
if ((aabb = chunks.isObstacleAt(x,y,z, boxAABB))){ if ((aabb = chunks.isObstacleAt(x,y,z, boxAABB))){
vel.y = 0.0f; vel.y = 0.0f;
float newy = std::floor(y) + aabb->max().y + half.y; float newy = std::floor(y) + aabb->max().y + half.y;
if (std::abs(newy-pos.y) <= MAX_FIX+stepHeight) { if (std::abs(newy-pos.y) <= stepHeight) {
pos.y = newy; pos.y = newy;
} }
break; break;
@ -321,7 +354,7 @@ void PhysicsSolver::colisionCalc(
if ((aabb = chunks.isObstacleAt(x,y,z, boxAABB))){ if ((aabb = chunks.isObstacleAt(x,y,z, boxAABB))){
vel.y = 0.0f; vel.y = 0.0f;
float newy = std::floor(y) - half.y + aabb->min().y - E; float newy = std::floor(y) - half.y + aabb->min().y - E;
if (std::abs(newy-pos.y) <= MAX_FIX) { if (std::abs(newy-pos.y) <= 0.0f) {
pos.y = newy; pos.y = newy;
} }
break; break;
@ -331,32 +364,6 @@ void PhysicsSolver::colisionCalc(
} }
} }
bool PhysicsSolver::isBlockInside(int x, int y, int z, Hitbox* hitbox) {
const glm::vec3& pos = hitbox->position;
auto half = hitbox->getHalfSize();
return x >= floor(pos.x-half.x) && x <= floor(pos.x+half.x) &&
z >= floor(pos.z-half.z) && z <= floor(pos.z+half.z) &&
y >= floor(pos.y-half.y) && y <= floor(pos.y+half.y);
}
bool PhysicsSolver::isBlockInside(int x, int y, int z, Block* def, blockstate state, Hitbox* hitbox) {
const float e = 0.001f; // inaccuracy
const glm::vec3& pos = hitbox->position;
auto half = hitbox->getHalfSize();
const auto& boxes = def->rotatable
? def->rt.hitboxes[state.rotation]
: def->hitboxes;
for (const auto& block_hitbox : boxes) {
glm::vec3 min = block_hitbox.min();
glm::vec3 max = block_hitbox.max();
if (min.x < pos.x+half.x-x-e && max.x > pos.x-half.x-x+e &&
min.z < pos.z+half.z-z-e && max.z > pos.z-half.z-z+e &&
min.y < pos.y+half.y-y-e && max.y > pos.y-half.y-y+e)
return true;
}
return false;
}
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());
} }

View file

@ -9,12 +9,11 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
class Block; class Block;
class Entities;
class GlobalChunks; class GlobalChunks;
struct Sensor; struct Sensor;
class PhysicsSolver { class PhysicsSolver {
glm::vec3 gravity;
std::vector<Sensor*> sensors;
public: public:
PhysicsSolver(glm::vec3 gravity); PhysicsSolver(glm::vec3 gravity);
void step( void step(
@ -24,7 +23,22 @@ public:
uint substeps, uint substeps,
entityid_t entity entityid_t entity
); );
void colisionCalc(
auto& getSensorsWriteable() {
return sensors;
}
auto& getSolidHitboxesWriteable() {
return solidHitboxes;
}
void removeSensor(Sensor* sensor);
private:
glm::vec3 gravity;
std::vector<Sensor*> sensors;
std::vector<Hitbox*> solidHitboxes;
void calcCollisions(
const GlobalChunks& chunks, const GlobalChunks& chunks,
Hitbox& hitbox, Hitbox& hitbox,
glm::vec3& vel, glm::vec3& vel,
@ -32,12 +46,13 @@ public:
const glm::vec3 half, const glm::vec3 half,
float stepHeight float stepHeight
); );
bool isBlockInside(int x, int y, int z, Hitbox* hitbox);
bool isBlockInside(int x, int y, int z, Block* def, blockstate state, Hitbox* hitbox);
void setSensors(std::vector<Sensor*> sensors) { void calcSubstep(
this->sensors = std::move(sensors); const GlobalChunks& chunks,
} Hitbox& hitbox,
glm::vec3& vel,
void removeSensor(Sensor* sensor); glm::vec3& pos,
bool prevGrounded,
float dt
);
}; };