summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-09-02 13:59:06 +0200
committerStanislaw Halik <sthalik@misaki.pl>2023-09-02 13:59:06 +0200
commit30e369cf24c0ea99e209b75fba5d65d55ef94aec (patch)
tree7f6062cd06830791dec1f3b96e8b09e775021d1c
parent68b2b531bc4e3301f50f55267b5b38521c5074fa (diff)
src: fix the move speed formula a abit
-rw-r--r--compat/math.hpp79
-rw-r--r--src/critter.cpp15
2 files changed, 89 insertions, 5 deletions
diff --git a/compat/math.hpp b/compat/math.hpp
new file mode 100644
index 00000000..d5b628c4
--- /dev/null
+++ b/compat/math.hpp
@@ -0,0 +1,79 @@
+#pragma once
+
+#include <bit>
+#include <cmath>
+
+namespace floormat::math::detail {
+
+constexpr double sqrt_newton_raphson(double x, double curr, double prev)
+{
+ return curr == prev
+ ? curr
+ : sqrt_newton_raphson(x, 0.5 * (curr + x / curr), curr);
+}
+
+template<typename T> requires std::is_floating_point_v<T> struct float_constants;
+
+template<>
+struct float_constants<double>
+{
+ static constexpr auto quiet_nan = std::bit_cast<double>(uint64_t(0x7FF8000000000000ULL));
+ static constexpr auto positive_infinity = std::bit_cast<double>(uint64_t(0x7FF0000000000000ULL));
+ static constexpr auto negative_infinity = std::bit_cast<double>(uint64_t(0xFFF0000000000000ULL));
+};
+
+template<>
+struct float_constants<float>
+{
+ static constexpr auto quiet_nan = std::bit_cast<float>(uint32_t(0x7FC00000U));
+ static constexpr auto positive_infinity = std::bit_cast<float>(uint32_t(0x7F800000U));
+ static constexpr auto negative_infinity = std::bit_cast<float>(uint32_t(0xFF800000U));
+};
+
+} // namespace floormat::math::detail
+
+namespace floormat::math {
+
+template<typename T>
+constexpr inline T sqrt(T x)
+requires std::is_floating_point_v<T>
+{
+ if (std::is_constant_evaluated())
+ {
+ using K = detail::float_constants<T>;
+ return x >= 0 && x < K::positive_infinity
+ ? T(detail::sqrt_newton_raphson(double(x), double(x), 0))
+ : K::quiet_nan;
+ }
+ else
+ return std::sqrt(x);
+}
+
+template<typename T>
+requires std::is_integral_v<T>
+constexpr inline double sqrt(T x)
+{
+ return sqrt(double(x));
+}
+
+template<typename T>
+requires std::is_floating_point_v<T>
+constexpr inline T ceil(T x)
+{
+ if (std::is_constant_evaluated())
+ {
+#ifdef __GNUG__
+ return __builtin_ceil(x);
+#else
+ const auto x0 = uint64_t(x);
+ if (x > x0)
+ return T(x0 + uint64_t(1));
+ else
+ return x0;
+#endif
+ }
+ else
+ return std::ceil(x);
+}
+
+} // namespace floormat::math
diff --git a/src/critter.cpp b/src/critter.cpp
index 9111aaf6..0e8ac643 100644
--- a/src/critter.cpp
+++ b/src/critter.cpp
@@ -4,8 +4,8 @@
#include "src/world.hpp"
#include "src/object.hpp"
#include "shaders/shader.hpp"
-#include "src/RTree-search.hpp"
#include "compat/exception.hpp"
+#include "compat/math.hpp"
#include <cmath>
#include <memory>
#include <utility>
@@ -17,9 +17,13 @@ namespace {
template <typename T> constexpr T sgn(T val) { return T(T(0) < val) - T(val < T(0)); }
-constexpr int tile_size_1 = iTILE_SIZE2.sum()/2,
- framerate = 96 * 3, move_speed = tile_size_1 * 2 * 3;
-constexpr float frame_time = 1.f/framerate;
+constexpr auto vector_length(Vector2 vec)
+{
+ return math::sqrt(Math::dot(vec, vec));
+};
+
+constexpr float framerate = 96 * 3, move_speed = vector_length(TILE_SIZE2) * 4.25f;
+constexpr float frame_time = 1/framerate;
constexpr auto arrows_to_dir(bool left, bool right, bool up, bool down)
{
@@ -30,6 +34,7 @@ constexpr auto arrows_to_dir(bool left, bool right, bool up, bool down)
const auto bits = unsigned(left << 3 | right << 2 | up << 1 | down << 0);
constexpr unsigned L = 1 << 3, R = 1 << 2, U = 1 << 1, D = 1 << 0;
+ CORRADE_ASSUME(bits <= 0xff);
switch (bits)
{
@@ -109,7 +114,7 @@ bool critter_proto::operator==(const object_proto& e0) const
int critter::allocate_frame_time(float dt)
{
- int d = int(delta) + int(65535u * dt);
+ int d = int(delta) + int(65535 * dt);
constexpr int framerate_ = 65535/framerate;
static_assert(framerate_ > 0);
auto ret = d / framerate_;