From 689c4bf27316c6271bb922d4f64436121cfe72bc Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 23 Mar 2024 14:04:15 +0100 Subject: w --- src/critter.cpp | 14 ++++++++------ src/critter.hpp | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/critter.cpp b/src/critter.cpp index d1f3caa7..3602b530 100644 --- a/src/critter.cpp +++ b/src/critter.cpp @@ -153,7 +153,7 @@ void critter::update(size_t i, const Ns& dt) const auto new_r = arrows_to_dir(movement.L, movement.R, movement.U, movement.D); if (new_r == rotation_COUNT) { - offset_frac = {}; + offset_frac_ = {}; delta = 0; } else @@ -193,16 +193,17 @@ void critter::update_movement(size_t i, const Ns& dt, rotation new_r) for (unsigned j = 0; j < nvecs; j++) { const auto vec = rotation_to_vec(rotations[j]); - using Frac = decltype(critter::offset_frac)::Type; + using Frac = decltype(critter::offset_frac_); constexpr auto frac = (float{limits::max}+1)/2; constexpr auto inv_frac = 1 / frac; const auto sign_vec = Math::sign(vec); - const auto from_accum = Vector2(offset_frac) * sign_vec * inv_frac; + const auto from_accum = (offset_frac_*inv_frac) * sign_vec.normalized(); auto offset_ = vec + from_accum; auto off_i = Vector2i(offset_); if (!off_i.isZero()) { - offset_frac = Math::Vector2(Math::abs(Math::fmod(offset_, 1.f)) * frac); + auto rem = (offset_ - Vector2(off_i)).length(); + offset_frac_ = Frac(rem * frac); if (can_move_to(off_i)) { can_move = true; @@ -214,7 +215,8 @@ void critter::update_movement(size_t i, const Ns& dt, rotation new_r) else { can_move = true; - offset_frac = Math::Vector2(Math::min({2.f,2.f}, Math::abs(offset_)) * frac); + auto rem = offset_.length(); + offset_frac_ = Frac(rem * frac); break; } } @@ -223,7 +225,7 @@ void critter::update_movement(size_t i, const Ns& dt, rotation new_r) if (!can_move) [[unlikely]] { delta = {}; - offset_frac = {}; + offset_frac_ = {}; } } diff --git a/src/critter.hpp b/src/critter.hpp index e51d37c2..f05c3352 100644 --- a/src/critter.hpp +++ b/src/critter.hpp @@ -39,7 +39,7 @@ struct critter final : object String name; float speed = 1; - Vector2us offset_frac; // todo! switch to Vector2ui due to `allocate_frame_time' + uint16_t offset_frac_ = 0; struct movement_s { bool L : 1 = false, -- cgit v1.2.3