diff --git a/Dynamics/DynamicsSystem.h b/Dynamics/DynamicsSystem.h index 0df3971..ab04bbe 100644 --- a/Dynamics/DynamicsSystem.h +++ b/Dynamics/DynamicsSystem.h @@ -25,6 +25,7 @@ namespace dynamics std::optional 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 crit_speed = std::nullopt, const std::vector& inertia_mats = { 1 }, const std::vector& coll_mats = { 1 }) { @@ -32,6 +33,7 @@ namespace dynamics pos, vel, force, ang_vel, torque, e, dyn_friction, + crit_speed, inertia_mats, coll_mats)); return rb.get(); } @@ -46,6 +48,7 @@ namespace dynamics std::function&& torque = [](int){ return 0.f; }, std::function&& e = [](int){ return 0.8f; }, std::function&& dyn_friction = [](int) { return 0.f; }, + std::function(int)>&& crit_speed = [](int){ return std::nullopt; }, std::function(int)>&& inertia_mats = [](int){ return std::vector { 1 }; }, std::function(int)>&& coll_mats = [](int){ return std::vector { 1 }; }) { @@ -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; diff --git a/Dynamics/RigidBody.h b/Dynamics/RigidBody.h index 9b282c3..eba5727 100644 --- a/Dynamics/RigidBody.h +++ b/Dynamics/RigidBody.h @@ -42,6 +42,7 @@ namespace dynamics float coeff_of_restitution = 0.8f; float dynamic_friction = 0.f; + std::optional crit_speed_sq = std::nullopt; AABB curr_sprite_aabb; AABB curr_aabb; @@ -147,6 +148,7 @@ namespace dynamics std::optional 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 crit_speed = std::nullopt, const std::vector& inertia_mats = { 1 }, const std::vector& coll_mats = { 1 }) : sprite(s) @@ -158,6 +160,7 @@ namespace dynamics , curr_torque(torque) , coeff_of_restitution(e) , dynamic_friction(math::clamp(dyn_friction, 0.f, 1.f)) + , crit_speed_sq(crit_speed.has_value() ? std::optional(math::sq(crit_speed.value())) : std::nullopt) , inertia_materials(inertia_mats) , collision_materials(coll_mats) { @@ -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;