From 9e08dc86b8b9fa9830574ff6116d4af5541fc47e Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 28 Feb 2024 13:26:24 +0100 Subject: cleanup some math code --- editor/camera.cpp | 13 +++++-------- editor/draw.cpp | 2 +- src/critter.cpp | 4 +--- src/raycast.cpp | 12 +++++------- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/editor/camera.cpp b/editor/camera.cpp index e919f937..e103c844 100644 --- a/editor/camera.cpp +++ b/editor/camera.cpp @@ -9,7 +9,7 @@ #include "src/camera-offset.hpp" #include "compat/enum-bitset.hpp" #include -#include +#include namespace floormat { @@ -45,9 +45,7 @@ void app::do_camera(float dt, const key_set& cmds, int mods) const auto max_camera_offset = Vector2d(sz * 10); camera_offset -= dir.normalized() * (double)dt * pixels_per_second; - camera_offset[0] = std::clamp(camera_offset[0], -max_camera_offset[0], max_camera_offset[0]); - camera_offset[1] = std::clamp(camera_offset[1], -max_camera_offset[1], max_camera_offset[1]); - + camera_offset = Math::clamp(camera_offset, -max_camera_offset, max_camera_offset); shader.set_camera_offset(camera_offset, shader.depth_offset()); update_cursor_tile(cursor.pixel); @@ -85,7 +83,7 @@ object_id app::get_object_colliding_with_cursor() constexpr auto m = TILE_SIZE2 * Vector2(1- eps, 1- eps); const auto tile_ = Vector2(M->pixel_to_tile_(Vector2d(pixel))); const auto curchunk = Vector2(tile.chunk()), curtile = Vector2(tile.local()); - const auto subpixel_ = Vector2(std::fmod(tile_[0], 1.f), std::fmod(tile_[1], 1.f)); + const auto subpixel_ = Math::fmod(tile_, 1.f); const auto subpixel = m * Vector2(curchunk[0] < 0 ? 1 + subpixel_[0] : subpixel_[0], curchunk[1] < 0 ? 1 + subpixel_[1] : subpixel_[1]); for (int16_t y = miny; y <= maxy; y++) @@ -143,13 +141,12 @@ void app::update_cursor_tile(const Optional& pixel) const auto tile_ = Vector2(M->pixel_to_tile_(Vector2d(*pixel))); const auto curchunk = Vector2(tile.chunk()); - const auto subpixel_ = Vector2(std::fmod(tile_.x(), 1.f), std::fmod(tile_.y(), 1.f)); + const auto subpixel_ = Math::fmod(tile_, 1.f); auto subpixel = TILE_SIZE2 * Vector2(curchunk.x() < 0 ? 1 + subpixel_.x() : subpixel_.x(), curchunk.y() < 0 ? 1 + subpixel_.y() : subpixel_.y()); constexpr auto half_tile = Vector2(iTILE_SIZE2/2); subpixel -= half_tile; - subpixel.x() = Math::clamp(std::round(subpixel.x()), -half_tile.x(), half_tile.x()-1); - subpixel.y() = Math::clamp(std::round(subpixel.y()), -half_tile.y(), half_tile.y()-1); + subpixel = Math::clamp(Math::round(subpixel), -half_tile, half_tile-Vector2{1.f}); cursor.subpixel = Vector2b(subpixel); } else diff --git a/editor/draw.cpp b/editor/draw.cpp index 844d26b3..bd30d80b 100644 --- a/editor/draw.cpp +++ b/editor/draw.cpp @@ -163,7 +163,7 @@ void app::draw_collision_boxes() constexpr auto m = TILE_SIZE2 * Vector2(1- eps, 1- eps); const auto tile_ = Vector2(M->pixel_to_tile_(Vector2d(pixel))); const auto curchunk = Vector2(tile.chunk()), curtile = Vector2(tile.local()); - const auto subpixel_ = Vector2(std::fmod(tile_[0], 1.f), std::fmod(tile_[1], 1.f)); + const auto subpixel_ = Math::fmod(tile_, 1.f); // todo use this formula for dragging objs const auto subpixel = m * Vector2(curchunk[0] < 0 ? 1 + subpixel_[0] : subpixel_[0], curchunk[1] < 0 ? 1 + subpixel_[1] : subpixel_[1]); diff --git a/src/critter.cpp b/src/critter.cpp index 9d7c860c..4068c782 100644 --- a/src/critter.cpp +++ b/src/critter.cpp @@ -191,9 +191,7 @@ void critter::update_playable(size_t i, float dt) auto off_i = Vector2i(offset_); if (!off_i.isZero()) { - offset_frac = - Vector2us(Vector2(Math::abs(std::fmod(offset_.x(), 1.f)), - Math::abs(std::fmod(offset_.y(), 1.f))) * frac); + offset_frac = Vector2us(Math::abs(Math::fmod(offset_, 1.f)) * frac); if (can_move_to(off_i)) { move_to(i, off_i, new_r); diff --git a/src/raycast.cpp b/src/raycast.cpp index b4d64a85..92d81452 100644 --- a/src/raycast.cpp +++ b/src/raycast.cpp @@ -121,9 +121,9 @@ raycast_result_s do_raycasting(std::conditional_t