update vertical damping & add rigidbody:get_vdamping

This commit is contained in:
MihailRis 2025-09-07 17:10:13 +03:00
parent 67a5741ed2
commit e064ace885
6 changed files with 28 additions and 9 deletions

View File

@ -95,10 +95,12 @@ body:get_linear_damping() -> number
-- Sets the linear velocity attenuation multiplier
body:set_linear_damping(value: number)
-- Checks if vertical velocity attenuation is enabled
-- Checks if vertical damping is enabled
body:is_vdamping() -> bool
-- Enables/disables vertical velocity attenuation
body:set_vdamping(enabled: bool)
-- Returns the vertical damping multiplier
body:get_vdamping() -> number
-- Enables/disables vertical damping / sets vertical damping multiplier
body:set_vdamping(enabled: bool | number)
-- Checks if the entity is on the ground
body:is_grounded() -> bool

View File

@ -97,8 +97,10 @@ body:set_linear_damping(value: number)
-- Проверяет, включено ли вертикальное затухание скорости
body:is_vdamping() -> bool
-- Включает/выключает вертикальное затухание скорости
body:set_vdamping(enabled: bool)
-- Возвращает множитель вертикального затухания скорости
body:get_vdamping() -> number
-- Включает/выключает вертикальное затухание скорости / устанавливает значение множителя
body:set_vdamping(enabled: bool | number)
-- Проверяет, находится ли сущность на земле (приземлена)
body:is_grounded() -> bool

View File

@ -25,6 +25,7 @@ local Rigidbody = {__index={
get_linear_damping=function(self) return __rigidbody.get_linear_damping(self.eid) end,
set_linear_damping=function(self, f) return __rigidbody.set_linear_damping(self.eid, f) end,
is_vdamping=function(self) return __rigidbody.is_vdamping(self.eid) end,
get_vdamping=function(self) return __rigidbody.get_vdamping(self.eid) end,
set_vdamping=function(self, b) return __rigidbody.set_vdamping(self.eid, b) end,
is_grounded=function(self) return __rigidbody.is_grounded(self.eid) end,
is_crouching=function(self) return __rigidbody.is_crouching(self.eid) end,

View File

@ -62,6 +62,15 @@ static int l_set_gravity_scale(lua::State* L) {
static int l_is_vdamping(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
return lua::pushboolean(
L, entity->getRigidbody().hitbox.verticalDamping > 0.0
);
}
return 0;
}
static int l_get_vdamping(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
return lua::pushnumber(
L, entity->getRigidbody().hitbox.verticalDamping
);
}
@ -70,7 +79,11 @@ static int l_is_vdamping(lua::State* L) {
static int l_set_vdamping(lua::State* L) {
if (auto entity = get_entity(L, 1)) {
entity->getRigidbody().hitbox.verticalDamping = lua::toboolean(L, 2);
if (lua::isboolean(L, 2)) {
entity->getRigidbody().hitbox.verticalDamping = lua::toboolean(L, 2);
} else {
entity->getRigidbody().hitbox.verticalDamping = lua::tonumber(L, 2);
}
}
return 0;
}
@ -144,6 +157,7 @@ const luaL_Reg rigidbodylib[] = {
{"get_linear_damping", lua::wrap<l_get_linear_damping>},
{"set_linear_damping", lua::wrap<l_set_linear_damping>},
{"is_vdamping", lua::wrap<l_is_vdamping>},
{"get_vdamping", lua::wrap<l_get_vdamping>},
{"set_vdamping", lua::wrap<l_set_vdamping>},
{"is_grounded", lua::wrap<l_is_grounded>},
{"is_crouching", lua::wrap<l_is_crouching>},

View File

@ -54,7 +54,7 @@ struct Hitbox {
glm::vec3 velocity;
float linearDamping = 0.5;
float friction = 1.0f;
bool verticalDamping = false;
float verticalDamping = 1.0f;
bool grounded = false;
float gravityScale = 1.0f;
bool crouching = false;

View File

@ -86,8 +86,8 @@ void PhysicsSolver::step(
}
vel.x /= 1.0f + delta * linearDamping;
vel.z /= 1.0f + delta * linearDamping;
if (hitbox.verticalDamping) {
vel.y /= 1.0f + delta * linearDamping;
if (hitbox.verticalDamping > 0.0f) {
vel.y /= 1.0f + delta * linearDamping * hitbox.verticalDamping;
}
AABB aabb;