voxelcore/src/physics/scheduling.hpp
ostermine cb39ed464a use double precision for positions of entities and players
Transform::pos, Hitbox::position and Player::position were floats. At
65k blocks the float step is 8 mm, at 1M it is 6-12 cm, at 30M it is
2 m. The physics moves a body by 3 cm or less per substep and pushes it
out of a wall by 10 cm or less: steps got rounded to the grid (the body
changed its speed, then stopped), the AABB of a body was off by a float
step and gave false collisions with the blocks around.

Positions of bodies, entity transforms, players, spawn points and the
skeleton position interpolation are worldvec3 now. Collisions are still
calculated in floats, but in the coordinates relative to the block the
body is in: the solver takes the origin, block lookups and hitboxes of
other bodies are shifted by it, the result is added back in doubles.
So the precision of the physics is the same anywhere in the world.

A calculated sensor is stored relative to the position of its entity.
Entities::getAllInside takes the corners in doubles,
Entities::hasBlockingInside takes the origin of the AABB.

The save format does not change: positions are written as numbers
(float64) as before, saves are compatible in both directions. Lua
functions keep their signatures, they only stop rounding to float.
2026-11-12 10:05:07 +03:00

42 lines
1 KiB
C++

#pragma once
#include "Hitbox.hpp"
#include "typedefs.hpp"
#include <algorithm>
#include <glm/glm.hpp>
namespace physics {
inline void integrate(
Hitbox& box, const glm::vec3& gravity, float dt, uint substeps
) {
float n = static_cast<float>(substeps);
auto accel = gravity * box.gravityScale;
box.position += worldvec3(
box.velocity * (dt * n) + accel * (dt * dt * n * (n + 2.0f) * 0.5f)
);
box.velocity += accel * (dt * n);
}
inline void push_body(
Hitbox& box, int axis, float value, uint wakeSubstep
) {
if (box.velocity[axis] != value) {
box.velocity[axis] = value;
box.nextSubstep = std::min(box.nextSubstep, wakeSubstep);
}
}
uint calc_substep_stride(
const Hitbox& hitbox, const glm::vec3& gravity, float dt, uint left
);
void schedule_substep(
Hitbox& hitbox,
const glm::vec3& gravity,
uint done,
uint substeps,
float dt
);
}