Skip to content

Commit

Permalink
RigidBody.h:
Browse files Browse the repository at this point in the history
* Added support for critical velocity (default off / nullopt).
  • Loading branch information
razterizer committed Nov 13, 2024
1 parent f971dab commit 2a96972
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Dynamics/DynamicsSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ 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,
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,
inertia_mats, coll_mats));
return rb.get();
}
Expand All @@ -46,6 +48,7 @@ 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::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 @@ -56,6 +59,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),
inertia_mats(s_idx), coll_mats(s_idx));
}
return rigid_body_arr;
Expand Down
6 changes: 6 additions & 0 deletions Dynamics/RigidBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace dynamics

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

AABB<int> curr_sprite_aabb;
AABB<float> curr_aabb;
Expand Down Expand Up @@ -147,6 +148,7 @@ 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,
const std::vector<int>& inertia_mats = { 1 },
const std::vector<int>& coll_mats = { 1 })
: sprite(s)
Expand All @@ -158,6 +160,7 @@ 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)
, inertia_materials(inertia_mats)
, collision_materials(coll_mats)
{
Expand All @@ -183,6 +186,9 @@ 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());
curr_cm += curr_vel * dt;
curr_centroid += curr_vel * dt;
curr_ang_acc = curr_torque * inv_Iz;
Expand Down

0 comments on commit 2a96972

Please sign in to comment.