use double precision for camera position and ray casts

A float position has a step of 6 cm at 1M blocks and of 2 m at 30M:
the camera moved by jumps and rays started not where the camera is.

worldvec3 (glm::dvec3) is the type of a position in the world.
Camera::position, the start and the end of block and entity ray casts
and the hit position of the player selection use it. Lua numbers are
doubles already, so cameras.set_pos/get_pos, block.raycast,
entities.raycast and world.raycast only stop rounding to float.
Entities::rayCast intersects a hitbox in the coordinates relative to
its position instead of a float AABB in the world space.
This commit is contained in:
ostermine 2026-11-12 00:58:10 +03:00
parent 89bdadd6aa
commit ad69663b2e
18 changed files with 86 additions and 71 deletions

View file

@ -180,10 +180,10 @@ void Shadows::generateShadowsMap(
float shadowMapScale = 0.32f / (1 << glm::max(0, quality)) * scale;
float shadowMapSize = resolution * shadowMapScale;
glm::vec3 basePos = glm::floor(camera.position / 4.0f) * 4.0f;
glm::vec3 prevPos = shadowCamera.position;
worldvec3 basePos = glm::floor(camera.position / 4.0) * 4.0;
worldvec3 prevPos = shadowCamera.position;
shadowCamera = Camera(
glm::distance2(prevPos, basePos) > 25.0f ? basePos : prevPos,
glm::distance2(prevPos, basePos) > 25.0 ? basePos : prevPos,
shadowMapSize
);
shadowCamera.near = 0.1f;
@ -206,9 +206,9 @@ void Shadows::generateShadowsMap(
glm::radians(0.0f)
);
shadowCamera.position -= shadowCamera.front * 500.0f;
shadowCamera.position += shadowCamera.up * 0.0f;
shadowCamera.position += camera.front * 0.0f;
shadowCamera.position -= worldvec3(shadowCamera.front * 500.0f);
shadowCamera.position += worldvec3(shadowCamera.up * 0.0f);
shadowCamera.position += worldvec3(camera.front * 0.0f);
auto view = shadowCamera.getView();

View file

@ -118,7 +118,7 @@ void DebugLinesRenderer::render(
if (showChunkBorders) {
linesShader.uniformMatrix("u_projview", camera.getProjView());
glm::vec3 coord = camera.position;
worldvec3 coord = camera.position;
if (coord.x < 0) coord.x--;
if (coord.z < 0) coord.z--;
int cx = floordiv(static_cast<int>(coord.x), CHUNK_W);

View file

@ -120,7 +120,7 @@ void Decorator::updateAcoustics(const Camera& camera) {
glm::vec3 dir = { r * std::cos(phi), r * std::sin(phi), z };
dir = glm::normalize(dir);
glm::vec3 end;
worldvec3 end;
glm::ivec3 norm;
glm::ivec3 iend;
blocks_agent::RaycastSettings raycast {};
@ -128,7 +128,7 @@ void Decorator::updateAcoustics(const Camera& camera) {
if (vox == nullptr) {
continue;
}
auto distance = glm::distance(start, end);
float distance = glm::distance(start, end);
if (distance >= length * 0.98f) {
continue;
}

View file

@ -200,7 +200,7 @@ void CameraControl::update(
refreshPosition();
auto castRay = [](Player& player, Camera& camera, const glm::vec3& front)
-> glm::vec3 {
-> worldvec3 {
auto blockEnd = player.chunks->rayCastToObstacle(camera.position, front, 3.0f);
Entities::RaycastSettings raycast {};
@ -212,7 +212,7 @@ void CameraControl::update(
);
if (entityRay.has_value() &&
entityRay->distance < glm::distance(camera.position, blockEnd)) {
return (camera.position + front * entityRay->distance);
return camera.position + worldvec3(front * entityRay->distance);
} else {
return blockEnd;
}
@ -220,14 +220,14 @@ void CameraControl::update(
camera->updateVectors();
if (player.currentCamera == spCamera) {
spCamera->position =
castRay(player, *camera, camera->front) - 0.4f * camera->front;
spCamera->position = castRay(player, *camera, camera->front) -
worldvec3(0.4f * camera->front);
spCamera->dir = -camera->dir;
spCamera->front = -camera->front;
spCamera->right = -camera->right;
} else if (player.currentCamera == tpCamera) {
tpCamera->position = castRay(player, *camera, camera->front * -1.0f) +
0.4f * camera->front;
worldvec3(0.4f * camera->front);
tpCamera->dir = camera->dir;
tpCamera->front = camera->front;
tpCamera->right = camera->right;
@ -366,7 +366,7 @@ voxel* PlayerController::updateSelection(float maxDistance) {
auto camera = player.fpCamera.get();
auto& selection = player.selection;
glm::vec3 end;
worldvec3 end;
glm::ivec3 iend;
glm::ivec3 norm;
voxel* vox = chunks.rayCast(
@ -393,7 +393,7 @@ voxel* PlayerController::updateSelection(float maxDistance) {
)) {
selection.entity = result->entity;
selection.hitPosition =
camera->position + camera->front * result->distance;
camera->position + worldvec3(camera->front * result->distance);
selection.position = selection.hitPosition;
selection.actualPosition = selection.position;
selection.normal = result->normal;

View file

@ -548,7 +548,7 @@ static int l_destruct(lua::State* L) {
static int l_raycast(lua::State* L) {
const auto& level = require_level();
auto start = lua::tovec<3>(L, 1);
auto start = lua::tovec<3, double>(L, 1);
auto dir = lua::tovec<3>(L, 2);
auto maxDistance = lua::tonumber(L, 3);
@ -576,7 +576,7 @@ static int l_raycast(lua::State* L) {
if (luaStackSize >= 6) {
raycast.includeNonSelectable = lua::toboolean(L, 6);
}
glm::vec3 end;
worldvec3 end;
glm::ivec3 normal;
glm::ivec3 iend;
@ -589,7 +589,7 @@ static int l_raycast(lua::State* L) {
lua::createtable(L, 0, 5);
}
lua::pushvec3(L, end);
lua::pushvec(L, end);
lua::setfield(L, "endpoint");
lua::pushvec3(L, normal);

View file

@ -47,11 +47,11 @@ static int l_name(lua::State* L) {
}
static int getter_pos(lua::State* L, const Camera& camera) {
return lua::pushvec3(L, camera.position);
return lua::pushvec(L, camera.position);
}
static void setter_pos(lua::State* L, Camera& camera, int idx) {
camera.position = lua::tovec<3>(L, idx);
camera.position = lua::tovec<3, double>(L, idx);
}
static int getter_rot(lua::State* L, const Camera& camera) {
@ -104,9 +104,11 @@ static int getter_up(lua::State* L, const Camera& camera) {
static int l_look_at(lua::State* L) {
size_t index = static_cast<size_t>(lua::tointeger(L, 1));
auto& camera = *require_level().cameras.at(index);
auto center = lua::tovec<3>(L, 2);
auto center = lua::tovec<3, double>(L, 2);
auto matrix = glm::inverse(
glm::lookAt(glm::vec3(), center - camera.position, glm::vec3(0, 1, 0))
glm::lookAt(
glm::vec3(), glm::vec3(center - camera.position), glm::vec3(0, 1, 0)
)
);
if (lua::isnumber(L, 3)) {
matrix = glm::mat4_cast(glm::slerp(

View file

@ -160,9 +160,9 @@ static int l_push_ray(
lua::State* L,
const std::optional<Entities::RaycastResult>& ray,
blockid_t block,
const glm::vec3& start,
const worldvec3& start,
const glm::vec3& dir,
const glm::vec3& end,
const worldvec3& end,
const glm::ivec3& iend,
const glm::ivec3& normal
) {
@ -175,7 +175,7 @@ static int l_push_ray(
lua::createtable(L, 0, 6);
}
lua::pushvec3(L, ray ? (start + dir * ray->distance) : end);
lua::pushvec(L, ray ? (start + worldvec3(dir * ray->distance)) : end);
lua::setfield(L, "endpoint");
lua::pushvec3(L, ray ? ray->normal : normal);
@ -202,14 +202,14 @@ static int l_push_ray(
static int l_raycast(
lua::State* L,
const glm::vec3& start,
const worldvec3& start,
const glm::vec3& dir,
float maxDistance,
const blocks_agent::RaycastSettings& voxelRaycast,
const Entities::RaycastSettings& entityRaycast,
bool entities
) {
glm::vec3 end;
worldvec3 end;
glm::ivec3 normal;
glm::ivec3 iend;
@ -276,7 +276,8 @@ static int l_world_raycast(lua::State* L) {
std::set<blockid_t> filteredBlocks {};
std::set<entitydefid_t> filteredEntities {};
glm::vec3 start, dir;
worldvec3 start;
glm::vec3 dir;
float maxDistance;
bool entities = true;
@ -285,7 +286,7 @@ static int l_world_raycast(lua::State* L) {
}
lua::requirefield(L, "start", 1);
start = lua::tovec3(L, -1);
start = lua::tovec<3, double>(L, -1);
lua::requirefield(L, "dir", 1);
dir = lua::tovec3(L, -1);
@ -341,7 +342,7 @@ static int l_raycast(lua::State* L) {
std::set<blockid_t> filteredBlocks {};
const int luaStackSize = lua::gettop(L);
glm::vec3 start = lua::tovec<3>(L, 1);
worldvec3 start = lua::tovec<3, double>(L, 1);
glm::vec3 dir = lua::tovec<3>(L, 2);
float maxDistance = lua::tonumber(L, 3);

View file

@ -1,5 +1,7 @@
#pragma once
#include "typedefs.hpp"
#include <stdint.h>
#include <ctime>
@ -17,8 +19,8 @@ namespace util {
constexpr inline float EPSILON = 1e-6f;
inline glm::vec3 to_local(const glm::vec3& pos, const glm::ivec3& origin) {
return glm::dvec3(pos) - glm::dvec3(origin);
inline glm::vec3 to_local(const worldvec3& pos, const glm::ivec3& origin) {
return glm::vec3(pos - worldvec3(origin));
}
class PseudoRandom {

View file

@ -159,7 +159,7 @@ void Entities::loadEntity(const dv::value& map, Entity& entity) {
}
std::optional<Entities::RaycastResult> Entities::rayCast(
glm::vec3 start,
worldvec3 start,
glm::vec3 dir,
float maxDistance,
const RaycastSettings& settings
@ -187,7 +187,11 @@ std::optional<Entities::RaycastResult> Entities::rayCast(
glm::ivec3 normal;
double distance;
if (ray.intersectAABB(
glm::vec3(), hitbox.getAABB(), maxDistance, normal, distance
hitbox.position,
AABB(-hitbox.halfsize, hitbox.halfsize),
maxDistance,
normal,
distance
) > RayRelation::None) {
foundUID = eid.uid;
foundNormal = normal;

View file

@ -84,7 +84,7 @@ public:
/// @param maxDistance Max ray length
/// @return An optional structure containing entity, normal and distance
std::optional<RaycastResult> rayCast(
glm::vec3 start,
worldvec3 start,
glm::vec3 dir,
float maxDistance,
const RaycastSettings& settings

View file

@ -38,7 +38,7 @@ struct CursorSelection {
glm::ivec3 position {};
glm::ivec3 actualPosition {};
glm::ivec3 normal {};
glm::vec3 hitPosition;
worldvec3 hitPosition;
entityid_t entity = ENTITY_NONE;
};

View file

@ -2,6 +2,7 @@
#include <memory>
#include <cstdint>
#include <glm/fwd.hpp>
using scriptenv = std::shared_ptr<int>;
@ -12,6 +13,9 @@ using integer_t = int64_t;
/// @brief dynamic floating-point type (64 bit floating point)
using number_t = double;
/// @brief position in the world (64 bit floating point components)
using worldvec3 = glm::dvec3;
using uint = unsigned int;
using u64id_t = uint64_t;

View file

@ -211,10 +211,10 @@ void Chunks::set(
}
voxel* Chunks::rayCast(
const glm::vec3& start,
const worldvec3& start,
const glm::vec3& dir,
float maxDist,
glm::vec3& end,
worldvec3& end,
glm::ivec3& norm,
glm::ivec3& iend,
const blocks_agent::RaycastSettings& settings
@ -224,12 +224,12 @@ voxel* Chunks::rayCast(
);
}
glm::vec3 Chunks::rayCastToObstacle(
const glm::vec3& start, const glm::vec3& dir, float maxDist
worldvec3 Chunks::rayCastToObstacle(
const worldvec3& start, const glm::vec3& dir, float maxDist
) const {
const float px = start.x;
const float py = start.y;
const float pz = start.z;
const double px = start.x;
const double py = start.y;
const double pz = start.z;
float dx = dir.x;
float dy = dir.y;
@ -286,11 +286,11 @@ glm::vec3 Chunks::rayCastToObstacle(
norm,
distance
) > RayRelation::None) {
return start + (dir * glm::vec3(distance));
return start + worldvec3(dir) * distance;
}
}
} else {
return glm::vec3(px + t * dx, py + t * dy, pz + t * dz);
return worldvec3(px + t * dx, py + t * dy, pz + t * dz);
}
}
}
@ -316,7 +316,7 @@ glm::vec3 Chunks::rayCastToObstacle(
}
}
}
return glm::vec3(px + maxDist * dx, py + maxDist * dy, pz + maxDist * dz);
return worldvec3(px + maxDist * dx, py + maxDist * dy, pz + maxDist * dz);
}
void Chunks::setCenter(int32_t x, int32_t z) {

View file

@ -110,17 +110,17 @@ public:
void setRotation(int32_t x, int32_t y, int32_t z, uint8_t rotation);
voxel* rayCast(
const glm::vec3& start,
const worldvec3& start,
const glm::vec3& dir,
float maxLength,
glm::vec3& end,
worldvec3& end,
glm::ivec3& norm,
glm::ivec3& iend,
const blocks_agent::RaycastSettings& settings
) const;
glm::vec3 rayCastToObstacle(
const glm::vec3& start, const glm::vec3& dir, float maxDist
worldvec3 rayCastToObstacle(
const worldvec3& start, const glm::vec3& dir, float maxDist
) const;
const AABB* isObstacleAt(float x, float y, float z) const;

View file

@ -217,18 +217,18 @@ bool blocks_agent::set(
template <class Storage>
static inline voxel* raycast_blocks(
const Storage& chunks,
const glm::vec3& start,
const worldvec3& start,
const glm::vec3& dir,
float maxDist,
glm::vec3& end,
worldvec3& end,
glm::ivec3& norm,
glm::ivec3& iend,
const RaycastSettings& settings
) {
const auto& blocks = chunks.getContentIndices().blocks;
float px = start.x;
float py = start.y;
float pz = start.z;
double px = start.x;
double py = start.y;
double pz = start.z;
float dx = dir.x;
float dy = dir.y;
@ -271,7 +271,7 @@ static inline voxel* raycast_blocks(
(def.selectable || includeNonSelectable) &&
((filter == nullptr || filter->find(def.rt.id) == filter->end()) ==
settings.blocksFilterExcludeMode)) {
end = start + t * dir;
end = start + worldvec3(t * dir);
iend = {ix, iy, iz};
if (!def.rt.solid) {
@ -301,7 +301,7 @@ static inline voxel* raycast_blocks(
hit = true;
distance = boxDistance;
norm = boxNorm;
end = start + (dir * glm::vec3(distance));
end = start + worldvec3(dir) * distance;
}
}
@ -342,17 +342,17 @@ static inline voxel* raycast_blocks(
}
}
iend = {ix, iy, iz};
end = start + t * dir;
end = start + worldvec3(t * dir);
norm = {0, 0, 0};
return nullptr;
}
voxel* blocks_agent::raycast(
const Chunks& chunks,
const glm::vec3& start,
const worldvec3& start,
const glm::vec3& dir,
float maxDist,
glm::vec3& end,
worldvec3& end,
glm::ivec3& norm,
glm::ivec3& iend,
const RaycastSettings& settings
@ -362,10 +362,10 @@ voxel* blocks_agent::raycast(
voxel* blocks_agent::raycast(
const GlobalChunks& chunks,
const glm::vec3& start,
const worldvec3& start,
const glm::vec3& dir,
float maxDist,
glm::vec3& end,
worldvec3& end,
glm::ivec3& norm,
glm::ivec3& iend,
const RaycastSettings& settings

View file

@ -447,10 +447,10 @@ struct RaycastSettings {
/// @return voxel pointer or nullptr
voxel* raycast(
const Chunks& chunks,
const glm::vec3& start,
const worldvec3& start,
const glm::vec3& dir,
float maxDist,
glm::vec3& end,
worldvec3& end,
glm::ivec3& norm,
glm::ivec3& iend,
const RaycastSettings& settings
@ -469,10 +469,10 @@ voxel* raycast(
/// @return voxel pointer or nullptr
voxel* raycast(
const GlobalChunks& chunks,
const glm::vec3& start,
const worldvec3& start,
const glm::vec3& dir,
float maxDist,
glm::vec3& end,
worldvec3& end,
glm::ivec3& norm,
glm::ivec3& iend,
const RaycastSettings& settings

View file

@ -5,7 +5,7 @@
inline constexpr double ORIGIN_STEP = 1024.0;
Camera::Camera(glm::vec3 position, float fov) : fov(fov), position(position) {
Camera::Camera(worldvec3 position, float fov) : fov(fov), position(position) {
updateVectors();
}
@ -51,7 +51,7 @@ glm::ivec3 Camera::getOrigin() const {
}
glm::vec3 Camera::getLocalPosition() const {
return glm::dvec3(position) - glm::dvec3(getOrigin());
return glm::vec3(position - worldvec3(getOrigin()));
}
glm::mat4 Camera::getView(bool pos) const {

View file

@ -1,5 +1,7 @@
#pragma once
#include "typedefs.hpp"
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
@ -12,7 +14,7 @@ public:
glm::vec3 right {};
glm::vec3 dir {};
glm::vec3 position {};
worldvec3 position {};
float zoom = 1.0f;
glm::mat4 rotation {1.0f};
@ -27,7 +29,7 @@ public:
Camera() {
updateVectors();
}
Camera(glm::vec3 position, float fov);
Camera(worldvec3 position, float fov);
void updateVectors();
void rotate(float x, float y, float z);