mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-11 14:01:49 +00:00
add mass, elasticity getters, setters to entity.rigidbody
This commit is contained in:
parent
1bba713502
commit
bd7f588800
4 changed files with 42 additions and 2 deletions
|
|
@ -164,6 +164,34 @@ static int l_get_material(lua::State* L) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int l_get_mass(lua::State* L) {
|
||||||
|
if (auto entity = get_entity(L, 1)) {
|
||||||
|
return lua::pushnumber(L, entity->getRigidbody().mass);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int l_set_mass(lua::State* L) {
|
||||||
|
if (auto entity = get_entity(L, 1)) {
|
||||||
|
entity->getRigidbody().mass = lua::tonumber(L, 2);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int l_get_elasticity(lua::State* L) {
|
||||||
|
if (auto entity = get_entity(L, 1)) {
|
||||||
|
return lua::pushnumber(L, entity->getRigidbody().elasticity);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int l_set_elasticity(lua::State* L) {
|
||||||
|
if (auto entity = get_entity(L, 1)) {
|
||||||
|
entity->getRigidbody().elasticity = lua::tonumber(L, 2);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
const luaL_Reg rigidbodylib[] = {
|
const luaL_Reg rigidbodylib[] = {
|
||||||
{"is_enabled", lua::wrap<l_is_enabled>},
|
{"is_enabled", lua::wrap<l_is_enabled>},
|
||||||
{"set_enabled", lua::wrap<l_set_enabled>},
|
{"set_enabled", lua::wrap<l_set_enabled>},
|
||||||
|
|
@ -185,5 +213,9 @@ const luaL_Reg rigidbodylib[] = {
|
||||||
{"set_body_type", lua::wrap<l_set_body_type>},
|
{"set_body_type", lua::wrap<l_set_body_type>},
|
||||||
{"set_material", lua::wrap<l_set_material>},
|
{"set_material", lua::wrap<l_set_material>},
|
||||||
{"get_material", lua::wrap<l_get_material>},
|
{"get_material", lua::wrap<l_get_material>},
|
||||||
|
{"get_mass", lua::wrap<l_get_mass>},
|
||||||
|
{"set_mass", lua::wrap<l_set_mass>},
|
||||||
|
{"get_elasticity", lua::wrap<l_get_elasticity>},
|
||||||
|
{"set_elasticity", lua::wrap<l_set_elasticity>},
|
||||||
{nullptr, nullptr}
|
{nullptr, nullptr}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -316,9 +316,9 @@ void Entities::preparePhysics(float delta) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
rigidbody.hitbox.mass = eid.def.bodyType == BodyType::DYNAMIC
|
rigidbody.hitbox.mass = eid.def.bodyType == BodyType::DYNAMIC
|
||||||
? eid.def.mass
|
? rigidbody.mass
|
||||||
: std::numeric_limits<float>::infinity();
|
: std::numeric_limits<float>::infinity();
|
||||||
rigidbody.hitbox.elasticity = eid.def.elasticity;
|
rigidbody.hitbox.elasticity = rigidbody.elasticity;
|
||||||
hitboxes.emplace_back(&rigidbody.hitbox);
|
hitboxes.emplace_back(&rigidbody.hitbox);
|
||||||
if (!eid.def.solid) {
|
if (!eid.def.solid) {
|
||||||
continue;
|
continue;
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,8 @@ dv::value Rigidbody::serialize(bool saveVelocity, bool saveBodySettings) const {
|
||||||
if (hitbox.crouching) {
|
if (hitbox.crouching) {
|
||||||
bodymap["crouch"] = hitbox.crouching;
|
bodymap["crouch"] = hitbox.crouching;
|
||||||
}
|
}
|
||||||
|
bodymap["mass"] = mass;
|
||||||
|
bodymap["elasticity"] = elasticity;
|
||||||
}
|
}
|
||||||
return bodymap;
|
return bodymap;
|
||||||
}
|
}
|
||||||
|
|
@ -33,6 +35,8 @@ void Rigidbody::deserialize(const dv::value& root) {
|
||||||
BodyTypeMeta.getItem(bodyTypeName, hitbox.type);
|
BodyTypeMeta.getItem(bodyTypeName, hitbox.type);
|
||||||
root["crouch"].asBoolean(hitbox.crouching);
|
root["crouch"].asBoolean(hitbox.crouching);
|
||||||
root["damping"].asNumber(hitbox.linearDamping);
|
root["damping"].asNumber(hitbox.linearDamping);
|
||||||
|
root["mass"].asNumber(mass);
|
||||||
|
root["elasticity"].asNumber(elasticity);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <void (*callback)(const Entity&, size_t, entityid_t)>
|
template <void (*callback)(const Entity&, size_t, entityid_t)>
|
||||||
|
|
@ -49,6 +53,8 @@ static sensorcallback create_sensor_callback(Entities& entities) {
|
||||||
void Rigidbody::initialize(
|
void Rigidbody::initialize(
|
||||||
const EntityDef& def, entityid_t id, Entities& entities
|
const EntityDef& def, entityid_t id, Entities& entities
|
||||||
) {
|
) {
|
||||||
|
mass = def.mass;
|
||||||
|
elasticity = def.elasticity;
|
||||||
sensors.resize(def.radialSensors.size() + def.boxSensors.size());
|
sensors.resize(def.radialSensors.size() + def.boxSensors.size());
|
||||||
for (auto& [i, box] : def.boxSensors) {
|
for (auto& [i, box] : def.boxSensors) {
|
||||||
SensorParams params {};
|
SensorParams params {};
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,8 @@ struct Rigidbody {
|
||||||
bool enabled = true;
|
bool enabled = true;
|
||||||
Hitbox hitbox;
|
Hitbox hitbox;
|
||||||
std::vector<Sensor> sensors;
|
std::vector<Sensor> sensors;
|
||||||
|
float mass;
|
||||||
|
float elasticity;
|
||||||
|
|
||||||
dv::value serialize(bool saveVelocity, bool saveBodySettings) const;
|
dv::value serialize(bool saveVelocity, bool saveBodySettings) const;
|
||||||
void deserialize(const dv::value& root);
|
void deserialize(const dv::value& root);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue