add 'mass' and 'elasticity' entity properties

This commit is contained in:
MihailRis 2026-03-14 18:47:10 +03:00
parent f945719d7a
commit ad32d7b6db
5 changed files with 20 additions and 0 deletions

View file

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

View file

@ -22,6 +22,7 @@
#include <entt/entity/registry.hpp>
#include <glm/ext/matrix_transform.hpp>
#include <limits>
#include <sstream>
static debug::Logger logger("entities");
@ -318,6 +319,9 @@ void Entities::preparePhysics(float delta) {
if (!eid.def.solid) {
continue;
}
rigidbody.hitbox.mass = eid.def.bodyType == BodyType::DYNAMIC
? eid.def.mass
: std::numeric_limits<float>::infinity();
solidHitboxes.emplace_back(&rigidbody.hitbox);
}
}

View file

@ -10,6 +10,8 @@ void EntityDef::cloneTo(EntityDef& dst) {
dst.skeletonName = skeletonName;
dst.blocking = blocking;
dst.solid = solid;
dst.mass = mass;
dst.elasticity = elasticity;
dst.stepHeight = stepHeight;
dst.save = save;
}

View file

@ -45,6 +45,12 @@ struct EntityDef {
/// @brief Is the entity solid (blocks other entities)
bool solid = false;
/// @brief Mass for physics calculations
float mass = 1.0f;
/// @brief Elasticity for physics calculations
float elasticity = 0.0f;
/// @brief Max obstacle height that does not require jumping
float stepHeight = 0.5f;

View file

@ -26,6 +26,12 @@ static glm::vec3 calc_collsion_velocity_result(
) {
const auto& vA = a.velocity;
const auto& vB = b.velocity;
if (glm::isinf(a.mass)) {
return vA;
}
if (glm::isinf(b.mass)) {
return vA * -a.elasticity + vB;
}
const auto& mA = a.mass;
const auto& mB = b.mass;
return (mA * vA + mB * vB + a.elasticity * mB * (vB - vA)) / (mA + mB);