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.
This commit is contained in:
ostermine 2026-11-12 01:19:23 +03:00
parent ebd506de09
commit 95d2121318
3 changed files with 133 additions and 9 deletions

94
dev/tests/far_coords.lua Normal file
View file

@ -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")

30
test/physics/Hitbox.cpp Normal file
View file

@ -0,0 +1,30 @@
#include <gtest/gtest.h>
#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);
}

View file

@ -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;