Skip to content

Commit

Permalink
RigidBody.h:
Browse files Browse the repository at this point in the history
* Splitting criticall speed into two optional component for greater control and less weird cross-coupled behaviour as it was before.
  • Loading branch information
razterizer committed Nov 19, 2024
1 parent e15d301 commit 0c43fb0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
10 changes: 6 additions & 4 deletions Dynamics/DynamicsSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ namespace dynamics
std::optional<Vec2> pos = std::nullopt, const Vec2& vel = {}, const Vec2& force = {},
float ang_vel = 0.f, float torque = 0.f,
float e = 0.8f, float dyn_friction = 0.f,
std::optional<float> crit_speed = std::nullopt,
std::optional<float> crit_speed_r = std::nullopt,
std::optional<float> crit_speed_c = std::nullopt,
const std::vector<int>& inertia_mats = { 1 },
const std::vector<int>& coll_mats = { 1 })
{
auto& rb = m_rigid_bodies.emplace_back(std::make_unique<RigidBody>(sprite, rb_mass,
pos, vel, force,
ang_vel, torque,
e, dyn_friction,
crit_speed,
crit_speed_r, crit_speed_c,
inertia_mats, coll_mats));
return rb.get();
}
Expand All @@ -48,7 +49,8 @@ namespace dynamics
std::function<float(int)> torque = [](int){ return 0.f; },
std::function<float(int)> e = [](int){ return 0.8f; },
std::function<float(int)> dyn_friction = [](int) { return 0.f; },
std::function<std::optional<float>(int)> crit_speed = [](int){ return std::nullopt; },
std::function<std::optional<float>(int)> crit_speed_r = [](int){ return std::nullopt; },
std::function<std::optional<float>(int)> crit_speed_c = [](int){ return std::nullopt; },
std::function<std::vector<int>(int)> inertia_mats = [](int){ return std::vector { 1 }; },
std::function<std::vector<int>(int)> coll_mats = [](int){ return std::vector { 1 }; })
{
Expand All @@ -59,7 +61,7 @@ namespace dynamics
pos(s_idx), vel(s_idx), force(s_idx),
ang_vel(s_idx), torque(s_idx),
e(s_idx), dyn_friction(s_idx),
crit_speed(s_idx),
crit_speed_r(s_idx), crit_speed_c(s_idx),
inertia_mats(s_idx), coll_mats(s_idx));
}
return rigid_body_arr;
Expand Down
16 changes: 10 additions & 6 deletions Dynamics/RigidBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ namespace dynamics

float coeff_of_restitution = 0.8f;
float dynamic_friction = 0.f;
std::optional<float> crit_speed_sq = std::nullopt;
std::optional<float> critical_speed_r = std::nullopt;
std::optional<float> critical_speed_c = std::nullopt;

AABB<int> curr_sprite_aabb;
AABB<float> curr_aabb;
Expand Down Expand Up @@ -148,7 +149,8 @@ namespace dynamics
std::optional<Vec2> pos = std::nullopt, const Vec2& vel = {}, const Vec2& force = {},
float ang_vel = 0.f, float torque = 0.f,
float e = 0.8f, float dyn_friction = 0.f,
std::optional<float> crit_speed = std::nullopt,
std::optional<float> crit_speed_r = std::nullopt,
std::optional<float> crit_speed_c = std::nullopt,
const std::vector<int>& inertia_mats = { 1 },
const std::vector<int>& coll_mats = { 1 })
: sprite(s)
Expand All @@ -160,7 +162,8 @@ namespace dynamics
, curr_torque(torque)
, coeff_of_restitution(e)
, dynamic_friction(math::clamp<float>(dyn_friction, 0.f, 1.f))
, crit_speed_sq(crit_speed.has_value() ? std::optional<float>(math::sq(crit_speed.value())) : std::nullopt)
, critical_speed_r(crit_speed_r)
, critical_speed_c(crit_speed_c)
, inertia_materials(inertia_mats)
, collision_materials(coll_mats)
{
Expand All @@ -186,9 +189,10 @@ namespace dynamics
{
curr_acc = curr_force * inv_mass;
curr_vel += curr_acc * dt;
auto curr_speed_sq = math::length_squared(curr_vel);
if (crit_speed_sq.has_value() && curr_speed_sq > crit_speed_sq.value())
curr_vel = math::normalize(curr_vel) * std::sqrt(crit_speed_sq.value());
if (critical_speed_r.has_value() && std::abs(curr_vel.r) > std::abs(critical_speed_r.value()))
curr_vel.r = math::sgn(curr_vel.r)*std::abs(critical_speed_r.value());
if (critical_speed_c.has_value() && std::abs(curr_vel.c) > std::abs(critical_speed_c.value()))
curr_vel.c = math::sgn(curr_vel.c)*std::abs(critical_speed_c.value());
curr_cm += curr_vel * dt;
curr_centroid += curr_vel * dt;
curr_ang_acc = curr_torque * inv_Iz;
Expand Down

0 comments on commit 0c43fb0

Please sign in to comment.