From 95d2121318b3d54bf61eb7f42556a8a9de0cbd63 Mon Sep 17 00:00:00 2001 From: ostermine Date: Thu, 12 Nov 2026 01:19:23 +0300 Subject: [PATCH] add tests for positions far from the world origin dev/tests/far_coords.lua: at 1M and 30M blocks a position of a player, a spawn point and an entity is read back as it was set, a ray hits the block it is cast to, a body with a velocity moves the same way as at the world origin, and a position at 30M survives saving and loading of the world. test/physics/Hitbox.cpp checks the AABB of a hitbox far from the world origin, test/window/Camera.cpp checks the view relative to the rendering origin with positions in doubles. --- dev/tests/far_coords.lua | 94 ++++++++++++++++++++++++++++++++++++++++ test/physics/Hitbox.cpp | 30 +++++++++++++ test/window/Camera.cpp | 18 ++++---- 3 files changed, 133 insertions(+), 9 deletions(-) create mode 100644 dev/tests/far_coords.lua create mode 100644 test/physics/Hitbox.cpp diff --git a/dev/tests/far_coords.lua b/dev/tests/far_coords.lua new file mode 100644 index 000000000..57cfa8b5c --- /dev/null +++ b/dev/tests/far_coords.lua @@ -0,0 +1,94 @@ +local util = require "core:tests_util" + +util.create_demo_world("core:default") +app.set_setting("chunks.load-distance", 3) +app.set_setting("chunks.load-speed", 1) + +local pid = player.create("Xerxes") + +local function is_loaded(x, z) + return block.get(math.floor(x), 0, math.floor(z)) ~= -1 +end + +local function wait_chunk(x, z) + player.set_spawnpoint(pid, x, 100, z) + player.set_pos(pid, x, 100, z) + app.sleep_until(function () return is_loaded(x, z) end) +end + +local function spawn_drop(x, y, z) + return entities.spawn("base:drop", {x, y, z}, {base__drop={ + id=item.index("base:stone.item"), + count=1 + }}) +end + +local function move_body(coord) + local x, z = coord + 8.5, -coord - 8.5 + wait_chunk(x - 5, z) + local entity = spawn_drop(x, 50.5, z) + for _ = 1, 20 do + entity.rigidbody:set_vel({3, 0, -2}) + app.tick() + end + local pos = entity.transform:get_pos() + entity:despawn() + return {pos[1] - x, pos[2] - 50.5, pos[3] - z} +end + +local moved = move_body(0) +debug.print(moved) +assert(moved[1] > 1.0, "body didn't move") + +for _, coord in ipairs({1000000, 30000000}) do + local x, y, z = coord + 0.3, 100.6, -coord - 0.7 + + player.set_pos(pid, x, y, z) + util.check_vec3({x, y, z}, {player.get_pos(pid)}, "player.get_pos", 1e-6) + + player.set_spawnpoint(pid, x, y, z) + util.check_vec3( + {x, y, z}, {player.get_spawnpoint(pid)}, "player.get_spawnpoint", 1e-6 + ) + + wait_chunk(x, z) + + local entity = spawn_drop(x, y, z) + util.check_vec3({x, y, z}, entity.transform:get_pos(), "spawn pos", 1e-6) + entity.transform:set_pos({x + 0.25, y, z - 0.25}) + util.check_vec3( + {x + 0.25, y, z - 0.25}, entity.transform:get_pos(), "set_pos", 1e-6 + ) + entity:despawn() + + local ray = world.raycast({ + start = {x, 8, z}, + dir = {0, -1, 0}, + distance = 10, + }) + assert(ray, "ray didn't hit") + util.check_vec3({x, 1, z}, ray.endpoint, "ray.endpoint") + util.check_vec3( + {math.floor(x), 0, math.floor(z)}, ray.iendpoint, "ray.iendpoint" + ) + util.check_float(7, ray.length, "ray.length") + + local far_moved = move_body(coord) + debug.print(far_moved) + util.check_vec3(moved, far_moved, "body displacement", 1e-3) +end + +local x, z = 30000000.3, -30000000.7 +player.set_spawnpoint(pid, x, 100.6, z) +player.set_pos(pid, x, 100.6, z) +app.close_world(true) +app.open_world("demo") + +local px, _, pz = player.get_pos(pid) +util.check_float(x, px, "saved x", 1e-6) +util.check_float(z, pz, "saved z", 1e-6) +util.check_vec3( + {x, 100.6, z}, {player.get_spawnpoint(pid)}, "saved spawnpoint", 1e-6 +) +app.close_world(false) +app.delete_world("demo") diff --git a/test/physics/Hitbox.cpp b/test/physics/Hitbox.cpp new file mode 100644 index 000000000..e7923f97a --- /dev/null +++ b/test/physics/Hitbox.cpp @@ -0,0 +1,30 @@ +#include + +#include "physics/Hitbox.hpp" + +TEST(Hitbox, AABBRelativeToOrigin) { + for (double coord : {0.0, 1e6, 3e7}) { + worldvec3 position(coord + 0.3, 64.25, -coord - 0.7); + Hitbox hitbox( + 1, BodyType::DYNAMIC, position, glm::vec3(0.3f, 0.9f, 0.3f) + ); + AABB aabb = hitbox.getAABB(glm::floor(position)); + EXPECT_NEAR(aabb.a.x, 0.0f, 1e-6f) << "at " << coord; + EXPECT_NEAR(aabb.a.y, -0.65f, 1e-6f) << "at " << coord; + EXPECT_NEAR(aabb.a.z, 0.0f, 1e-6f) << "at " << coord; + EXPECT_NEAR(aabb.b.x, 0.6f, 1e-6f) << "at " << coord; + EXPECT_NEAR(aabb.b.y, 1.15f, 1e-6f) << "at " << coord; + EXPECT_NEAR(aabb.b.z, 0.6f, 1e-6f) << "at " << coord; + } +} + +TEST(Hitbox, SetPosFarFromOrigin) { + worldvec3 position(3e7 + 0.3, 64.25, -3e7 - 0.7); + Hitbox hitbox(1, BodyType::DYNAMIC, {}, glm::vec3(0.3f, 0.9f, 0.3f)); + hitbox.setPos(position); + EXPECT_EQ(hitbox.position, position); + EXPECT_EQ(hitbox.prevPosition, position); + + hitbox.setPos(position + worldvec3(0.05, 0.0, 0.0)); + EXPECT_EQ(hitbox.prevPosition, position); +} diff --git a/test/window/Camera.cpp b/test/window/Camera.cpp index 0015bc93d..354db1f30 100644 --- a/test/window/Camera.cpp +++ b/test/window/Camera.cpp @@ -31,18 +31,18 @@ TEST(Camera, ViewRotationFarFromOrigin) { } TEST(Camera, ViewRelativeToOrigin) { - for (float coord : {0.0f, 500.0f, -1e5f, 1e6f, 3e7f}) { - Camera camera(glm::vec3(coord, 64.0f, -coord), 1.0f); + for (double coord : {0.0, 500.3, -1e5 - 0.3, 1e6 + 0.3, 3e7 + 0.3}) { + Camera camera(worldvec3(coord, 64.0, -coord), 1.0f); camera.rotate(glm::radians(-20.0f), glm::radians(35.0f), 0.0f); - glm::dvec3 origin = camera.getOrigin(); - glm::vec3 local = camera.getLocalPosition(); - EXPECT_LE(glm::abs(local.x), 512.0f) << "at " << coord; - EXPECT_LE(glm::abs(local.z), 512.0f) << "at " << coord; - EXPECT_EQ(glm::dvec3(camera.position), origin + glm::dvec3(local)); + worldvec3 origin = camera.getOrigin(); + worldvec3 local = camera.getLocalPosition(); + EXPECT_LE(glm::abs(local.x), 512.0) << "at " << coord; + EXPECT_LE(glm::abs(local.z), 512.0) << "at " << coord; + EXPECT_NEAR(camera.position.x, origin.x + local.x, 1e-4) << coord; + EXPECT_NEAR(camera.position.z, origin.z + local.z, 1e-4) << coord; - glm::dvec3 point = - glm::dvec3(camera.position) + glm::dvec3(camera.front) * 10.0; + worldvec3 point = camera.position + worldvec3(camera.front) * 10.0; glm::vec4 viewPoint = camera.getView() * glm::vec4(glm::vec3(point - origin), 1.0f); EXPECT_NEAR(viewPoint.x, 0.0f, 1e-3f) << "at " << coord;