Merge pull request #788 from MihailRis/physics-related

physics-related works
This commit is contained in:
MihailRis 2026-02-13 12:25:07 +03:00 • committed by GitHub
commit a5ef4710bc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 16 additions and 9 deletions

View file

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

View file

@ -42,6 +42,9 @@ struct EntityDef {
/// @brief Does entity prevent blocks setup
bool blocking = true;
/// @brief Max obstacle height that does not require jumping
float stepHeight = 0.5f;
/// @brief save-** flags
struct {
bool enabled = true;

View file

@ -80,4 +80,5 @@ void Rigidbody::initialize(
create_sensor_callback<scripting::on_sensor_enter>(entities),
create_sensor_callback<scripting::on_sensor_exit>(entities)};
}
hitbox.stepHeight = def.stepHeight;
}

View file

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

View file

@ -42,7 +42,7 @@ void PhysicsSolver::step(
vel += gravity * dt * gravityScale;
if (hitbox.type == BodyType::DYNAMIC) {
colisionCalc(chunks, hitbox, vel, pos, half,
(prevGrounded && gravityScale > 0.0f) ? 0.5f : 0.0f);
(prevGrounded && gravityScale > 0.0f) ? hitbox.stepHeight : 0.0f);
}
pos += vel * dt + gravity * gravityScale * dt * dt * 0.5f;
@ -65,6 +65,7 @@ void PhysicsSolver::step(
}
if (!hitbox.grounded) {
pos.z = pz;
vel.z = 0.0f;
}
hitbox.grounded = false;
for (int ix = 0; ix <= (half.x-E)*2/s; ix++) {
@ -79,6 +80,7 @@ void PhysicsSolver::step(
}
if (!hitbox.grounded) {
pos.x = px;
vel.x = 0.0f;
}
hitbox.grounded = true;
}
@ -152,17 +154,16 @@ static bool calc_collision_neg(
return false;
}
glm::vec3 offset(0.0f, stepHeight, 0.0f);
for (int iy = 0; iy <= ((half-offset*0.5f)[ny]-E)*2/s; iy++) {
for (int iy = 0; iy <= std::max<int>(1, ((half-offset*0.5f)[ny]-E)*2/s); iy++) {
glm::vec3 coord;
coord[ny] = ((pos+offset)[ny]-half[ny]+E) + iy * s;
for (int iz = 0; iz <= (half[nz]-E)*2/s; iz++){
coord[nz] = (pos[nz]-half[nz]+E) + iz * s;
coord[nx] = (pos[nx]-half[nx]-E);
if (const auto aabb = chunks.isObstacleAt(coord.x, coord.y, coord.z)) {
vel[nx] = 0.0f;
float newx = std::floor(coord[nx]) + aabb->max()[nx] + half[nx] + E;
if (std::abs(newx-pos[nx]) <= MAX_FIX) {
float newx = std::floor(coord[nx]) + half[nx] + aabb->max()[nx] + E;
if (newx - pos[nx] <= E) {
pos[nx] = newx;
}
return true;
@ -185,7 +186,7 @@ static void calc_collision_pos(
return;
}
glm::vec3 offset(0.0f, stepHeight, 0.0f);
for (int iy = 0; iy <= ((half-offset*0.5f)[ny]-E)*2/s; iy++) {
for (int iy = 0; iy <= std::max<int>(1, ((half-offset*0.5f)[ny]-E)*2/s); iy++) {
glm::vec3 coord;
coord[ny] = ((pos+offset)[ny]-half[ny]+E) + iy * s;
for (int iz = 0; iz <= (half[nz]-E)*2/s; iz++) {
@ -194,7 +195,7 @@ static void calc_collision_pos(
if (const auto aabb = chunks.isObstacleAt(coord.x, coord.y, coord.z)) {
vel[nx] = 0.0f;
float newx = std::floor(coord[nx]) - half[nx] + aabb->min()[nx] - E;
if (std::abs(newx-pos[nx]) <= MAX_FIX) {
if (newx - pos[nx] <= E) {
pos[nx] = newx;
}
return;
@ -211,7 +212,7 @@ void PhysicsSolver::colisionCalc(
const glm::vec3 half,
float stepHeight
) {
// step size (smaller - more accurate, but slower)
// step size (smaller - more accurate, but slower) // TODO: GET RID OF THIS
float s = 2.0f/BLOCK_AABB_GRID;
stepHeight = calc_step_height(chunks, pos, half, stepHeight, s);
@ -224,7 +225,7 @@ void PhysicsSolver::colisionCalc(
calc_collision_neg<2, 1, 0>(chunks, pos, vel, half, stepHeight, s);
calc_collision_pos<2, 1, 0>(chunks, pos, vel, half, stepHeight, s);
if (calc_collision_neg<1, 0, 2>(chunks, pos, vel, half, stepHeight, s)) {
if (calc_collision_neg<1, 0, 2>(chunks, pos, vel, half, 0.0f, s)) {
hitbox.grounded = true;
}