add more reason to physics simulation substeps

This commit is contained in:
MihailRis 2026-03-11 22:08:40 +03:00
parent 794ce07bd5
commit 4b4f2439c0
5 changed files with 119 additions and 114 deletions

View file

@ -2,10 +2,13 @@
#include <stdexcept>
Hitbox::Hitbox(BodyType type, glm::vec3 position, glm::vec3 halfsize)
: type(type),
position(position),
halfsize(halfsize),
velocity(0.0f,0.0f,0.0f),
prevPosition(position)
{}
Hitbox::Hitbox(
entityid_t entity, BodyType type, glm::vec3 position, glm::vec3 halfsize
)
: entity(entity),
type(type),
position(position),
halfsize(halfsize),
velocity(0.0f, 0.0f, 0.0f),
prevPosition(position) {
}

View file

@ -51,6 +51,7 @@ VC_ENUM_METADATA(BodyType)
VC_ENUM_END
struct Hitbox {
entityid_t entity;
BodyType type;
glm::vec3 position;
glm::vec3 halfsize;
@ -65,17 +66,18 @@ struct Hitbox {
bool crouching = false;
float stepHeight = 0.5f;
std::string material;
glm::vec3 groundVelocity {};
std::string groundMaterial;
glm::vec3 groundVelocity {};
// garbage tbh
glm::vec3 prevPosition {};
float delta = 0.0f;
glm::vec3 prevVelocity {};
bool prevGrounded = false;
static inline constexpr float TELEPORT_THRESOLD_SQR = 0.5f;
Hitbox(BodyType type, glm::vec3 position, glm::vec3 halfsize);
Hitbox(
entityid_t entity, BodyType type, glm::vec3 position, glm::vec3 halfsize
);
AABB getAABB() const {
return AABB(position - halfsize, position + halfsize);

View file

@ -141,13 +141,10 @@ static bool calc_collision_neg_y(
if (pos.y < newy && glm::abs(pos.y - newy) < boxhalf.y) {
pos.y = newy;
}
if (glm::abs(box->delta) > 1e-5f) {
hitbox.groundVelocity =
(box->position - box->prevPosition) / box->delta;
}
hitbox.groundVelocity = box->position - box->prevPosition;
if (vel.y < 0.0f) {
vel.y = 0.0f;
if (hitbox.groundMaterial.empty()) {
if (hitbox.groundMaterial.empty() && !box->material.empty()) {
hitbox.groundMaterial = box->material;
}
return true;
@ -297,16 +294,15 @@ void PhysicsSolver::calcSubstep(
Hitbox& hitbox,
glm::vec3& vel,
glm::vec3& pos,
bool prevGrounded,
float dt,
int substeps
) {
if (hitbox.grounded) {
pos.x += hitbox.groundVelocity.x * dt * substeps;
if (glm::length2(hitbox.groundVelocity) > 1e-6f) {
pos.x += hitbox.groundVelocity.x;
if (hitbox.groundVelocity.y < 0.0f) {
pos.y += hitbox.groundVelocity.y * dt * substeps;
pos.y += hitbox.groundVelocity.y;
}
pos.z += hitbox.groundVelocity.z * dt * substeps;
pos.z += hitbox.groundVelocity.z;
}
auto initpos = pos;
@ -320,7 +316,7 @@ void PhysicsSolver::calcSubstep(
vel,
pos,
half,
(prevGrounded && gravityScale > 0.0f) ? hitbox.stepHeight : 0.0f
(hitbox.prevGrounded && gravityScale > 0.0f) ? hitbox.stepHeight : 0.0f
);
}
@ -374,64 +370,72 @@ void PhysicsSolver::calcSubstep(
void PhysicsSolver::step(
const GlobalChunks& chunks,
Hitbox& hitbox,
float delta,
uint substeps,
entityid_t entity
uint substeps
) {
hitbox.prevPosition = hitbox.position;
hitbox.delta = delta;
hitbox.groundMaterial.clear();
for (auto hitbox : hitboxes) {
hitbox->groundMaterial.clear();
hitbox->prevGrounded = hitbox->grounded;
hitbox->grounded = false;
hitbox->prevVelocity = hitbox->velocity;
}
float dt = delta / static_cast<float>(substeps);
float linearDamping = hitbox.linearDamping * hitbox.friction;
glm::vec3& pos = hitbox.position;
glm::vec3& vel = hitbox.velocity;
bool prevGrounded = hitbox.grounded;
hitbox.grounded = false;
for (uint i = 0; i < substeps; i++) {
calcSubstep(chunks, hitbox, vel, pos, prevGrounded, dt, substeps);
}
vel.x /= 1.0f + delta * linearDamping;
vel.z /= 1.0f + delta * linearDamping;
if (hitbox.verticalDamping > 0.0f) {
vel.y /= 1.0f + delta * linearDamping * hitbox.verticalDamping;
}
if (prevGrounded && !hitbox.grounded) {
auto appliedVelocity = hitbox.groundVelocity;
vel += appliedVelocity;
hitbox.groundVelocity = {};
}
AABB aabb;
aabb.a = pos - hitbox.getHalfSize();
aabb.b = pos + hitbox.getHalfSize();
for (size_t i = 0; i < sensors.size(); i++) {
auto& sensor = *sensors[i];
if (sensor.entity == entity) {
continue;
for (auto hitbox : hitboxes) {
glm::vec3& pos = hitbox->position;
glm::vec3& vel = hitbox->velocity;
hitbox->prevPosition = hitbox->position;
calcSubstep(chunks, *hitbox, vel, pos, dt, substeps);
}
}
bool triggered = false;
switch (sensor.type) {
case SensorType::AABB:
triggered = aabb.intersects(sensor.calculated.aabb);
break;
case SensorType::RADIUS:
triggered = glm::distance2(
pos, glm::vec3(sensor.calculated.radial))
< sensor.calculated.radial.w;
break;
for (auto hitbox : hitboxes) {
float linearDamping = hitbox->linearDamping * hitbox->friction;
glm::vec3& pos = hitbox->position;
glm::vec3& vel = hitbox->velocity;
vel.x /= 1.0f + delta * linearDamping;
vel.z /= 1.0f + delta * linearDamping;
if (hitbox->verticalDamping > 0.0f) {
vel.y /= 1.0f + delta * linearDamping * hitbox->verticalDamping;
}
if (triggered) {
if (sensor.prevEntered.find(entity) == sensor.prevEntered.end()) {
sensor.enterCallback(sensor.entity, sensor.index, entity);
if (hitbox->prevGrounded && !hitbox->grounded) {
auto appliedVelocity = hitbox->groundVelocity / dt;
vel += appliedVelocity;
hitbox->groundVelocity = {};
}
AABB aabb;
aabb.a = pos - hitbox->getHalfSize();
aabb.b = pos + hitbox->getHalfSize();
for (size_t i = 0; i < sensors.size(); i++) {
auto& sensor = *sensors[i];
if (sensor.entity == hitbox->entity) {
continue;
}
sensor.nextEntered.insert(entity);
bool triggered = false;
switch (sensor.type) {
case SensorType::AABB:
triggered = aabb.intersects(sensor.calculated.aabb);
break;
case SensorType::RADIUS:
triggered = glm::distance2(
pos, glm::vec3(sensor.calculated.radial))
< sensor.calculated.radial.w;
break;
}
if (!triggered) {
continue;
}
if (sensor.prevEntered.find(hitbox->entity) == sensor.prevEntered.end()) {
sensor.enterCallback(sensor.entity, sensor.index, hitbox->entity);
}
sensor.nextEntered.insert(hitbox->entity);
}
hitbox->friction = glm::abs(hitbox->gravityScale <= 1e-7f)
? 8.0f
: (!hitbox->prevGrounded ? 2.0f : 10.0f);
}
}

View file

@ -18,10 +18,8 @@ public:
PhysicsSolver(glm::vec3 gravity);
void step(
const GlobalChunks& chunks,
Hitbox& hitbox,
float delta,
uint substeps,
entityid_t entity
uint substeps
);
auto& getSensorsWriteable() {
@ -32,11 +30,16 @@ public:
return solidHitboxes;
}
auto& getHitboxesWriteable() {
return hitboxes;
}
void removeSensor(Sensor* sensor);
private:
glm::vec3 gravity;
std::vector<Sensor*> sensors;
std::vector<Hitbox*> solidHitboxes;
std::vector<Hitbox*> hitboxes;
void calcCollisions(
const GlobalChunks& chunks,
@ -52,7 +55,6 @@ private:
Hitbox& hitbox,
glm::vec3& vel,
glm::vec3& pos,
bool prevGrounded,
float dt,
int substeps
);