summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-02-28 13:26:24 +0100
committerStanislaw Halik <sthalik@misaki.pl>2024-02-28 13:26:24 +0100
commit9e08dc86b8b9fa9830574ff6116d4af5541fc47e (patch)
tree7a41d076ab1cdcb0282cae07b4ce3bf9b0069adc
parent20e95a5cf095acbc9c76f335258a9bf5e220aaeb (diff)
cleanup some math code
-rw-r--r--editor/camera.cpp13
-rw-r--r--editor/draw.cpp2
-rw-r--r--src/critter.cpp4
-rw-r--r--src/raycast.cpp12
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 <bit>
-#include <algorithm>
+#include <Magnum/Math/Functions.h>
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<Vector2i>& 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<EnableDiagnostics, raycast_dia
using Math::min;
using Math::abs;
using Math::ceil;
+ using Math::copysign;
- constexpr float eps = 1e-6f;
- constexpr float inv_eps = 1/eps;
+ constexpr auto inv_eps = 1e6f, eps = 1/inv_eps;
constexpr int fuzz = 2;
constexpr auto fuzz2 = 0.5f;
@@ -158,10 +158,8 @@ raycast_result_s do_raycasting(std::conditional_t<EnableDiagnostics, raycast_dia
size_[short_axis] = (short_len+nsteps*2-1) / nsteps;
size_[long_axis] = (long_len+nsteps-1) / nsteps;
- auto dir_inv_norm = Vector2(
- abs(dir.x()) < eps ? std::copysign(inv_eps, dir.x()) : 1 / dir.x(),
- abs(dir.y()) < eps ? std::copysign(inv_eps, dir.y()) : 1 / dir.y()
- );
+ auto dir_inv_norm = Vector2(abs(dir.x()) < eps ? copysign(inv_eps, dir.x()) : 1 / dir.x(),
+ abs(dir.y()) < eps ? copysign(inv_eps, dir.y()) : 1 / dir.y());
auto signs = ray_aabb_signs(dir_inv_norm);
result = {
@@ -199,7 +197,7 @@ raycast_result_s do_raycasting(std::conditional_t<EnableDiagnostics, raycast_dia
for (unsigned k = 0; b && k <= nsteps; k++)
{
auto pos_ = ceil(abs(V * (float)k/(float)nsteps));
- auto pos = Vector2i{(int)std::copysign(pos_.x(), V.x()), (int)std::copysign(pos_.y(), V.y())};
+ auto pos = Vector2i(copysign(pos_, V));
auto size = size_;
if (k == 0)