use double precision for particles, emitters and 3D texts

Positions of particles, of static emitters and of text notes are
worldvec3. gfx.particles, gfx.text3d, gfx.blockwraps, pathfinding and
generation.create_fragment read positions from Lua as doubles: a float
can not hold an odd block coordinate above 16.7M, the object went to a
neighbour block.
This commit is contained in:
ostermine 2026-11-12 01:13:45 +03:00
parent cb39ed464a
commit d012f0530b
14 changed files with 44 additions and 43 deletions

View file

@ -83,7 +83,7 @@ void Decorator::addParticles(const Block& def, const glm::ivec3& pos) {
);
blockEmitters[pos] = renderer.particles->add(std::make_unique<Emitter>(
level,
glm::vec3{pos.x + 0.5, pos.y + 0.5, pos.z + 0.5},
worldvec3 {pos.x + 0.5, pos.y + 0.5, pos.z + 0.5},
*def.particles,
treg.texture,
treg.region,
@ -205,7 +205,7 @@ void Decorator::updateRandom(
);
renderer.particles->add(std::make_unique<Emitter>(
level,
glm::vec3 {
worldvec3 {
pos.x + random.randFloat(),
pos.y + 1.1,
pos.z + random.randFloat()},
@ -308,11 +308,11 @@ void Decorator::updateTextNotes() {
renderer.texts->remove(textsIter->second);
textsIter = playerTexts.erase(textsIter);
} else {
glm::vec3 position = player->getPosition();
worldvec3 position = player->getPosition();
if (auto entity = level.entities->get(player->getEntity())) {
position = entity->getInterpolatedPosition();
}
note->setPosition(position + glm::vec3(0, 1, 0));
note->setPosition(position + worldvec3(0, 1, 0));
if (note->getText() != player->getName()) {
note->setText(player->getName());
}

View file

@ -14,7 +14,7 @@
Emitter::Emitter(
const Level& level,
std::variant<glm::vec3, entityid_t> origin,
EmitterOrigin origin,
ParticlesPreset preset,
const Texture* texture,
const UVRegion& region,
@ -50,15 +50,15 @@ static inline glm::vec3 generate_coord(ParticleSpawnShape shape) {
void Emitter::update(
float delta,
const glm::vec3& cameraPosition,
const worldvec3& cameraPosition,
std::vector<Particle>& particles
) {
const float spawnInterval = preset.spawnInterval;
if (count == 0 || (count == -1 && spawnInterval < FLT_EPSILON)) {
return;
}
glm::vec3 position {};
if (auto staticPos = std::get_if<glm::vec3>(&origin)) {
worldvec3 position {};
if (auto staticPos = std::get_if<worldvec3>(&origin)) {
position = *staticPos;
} else if (auto entityId = std::get_if<entityid_t>(&origin)) {
if (auto entity = level.entities->get(*entityId)) {
@ -105,7 +105,7 @@ void Emitter::update(
spawnOffset *= preset.spawnSpread;
spawnOffset += preset.spawnOffset;
particle.position = position + spawnOffset;
particle.position = position + worldvec3(spawnOffset);
particle.lifetime *= 1.0f - preset.lifetimeSpread * random.randFloat();
particle.velocity += glm::ballRand(1.0f) * preset.explosion;
if (preset.randomSubUV < 1.0f) {

View file

@ -20,7 +20,7 @@ struct Particle {
/// @brief Some random integer for visuals configuration.
int random;
/// @brief Global position
glm::vec3 position;
worldvec3 position;
/// @brief Linear velocity
glm::vec3 velocity;
/// @brief Remaining life time
@ -35,7 +35,7 @@ struct Particle {
class Texture;
using EmitterOrigin = std::variant<glm::vec3, entityid_t>;
using EmitterOrigin = std::variant<worldvec3, entityid_t>;
class Emitter {
const Level& level;
@ -61,7 +61,7 @@ public:
Emitter(
const Level& level,
std::variant<glm::vec3, entityid_t> origin,
EmitterOrigin origin,
ParticlesPreset preset,
const Texture* texture,
const UVRegion& region,
@ -79,7 +79,7 @@ public:
/// @param particles destination particles vector
void update(
float delta,
const glm::vec3& cameraPosition,
const worldvec3& cameraPosition,
std::vector<Particle>& particles
);

View file

@ -38,10 +38,10 @@ static inline void update_particle(
auto& angle = particle.angle;
vel += delta * preset.acceleration;
if (preset.collision && chunks.isObstacleAt(pos + vel * delta)) {
if (preset.collision && chunks.isObstacleAt(pos + worldvec3(vel * delta))) {
vel *= 0.0f;
}
pos += vel * delta;
pos += worldvec3(vel * delta);
angle += particle.angularVelocity * delta;
particle.lifetime -= delta;
}
@ -114,7 +114,8 @@ static inline glm::vec4 calc_lights(
light = glm::max(
light,
MainBatch::sampleLight(
particle.position - size * glm::vec3(x, y, z),
particle.position -
worldvec3(size * glm::vec3(x, y, z)),
chunks,
backlight
)

View file

@ -1,6 +1,6 @@
#include "TextNote.hpp"
TextNote::TextNote(std::wstring text, NotePreset preset, glm::vec3 position)
TextNote::TextNote(std::wstring text, NotePreset preset, worldvec3 position)
: text(std::move(text)),
preset(std::move(preset)),
position(std::move(position)) {
@ -30,11 +30,11 @@ void TextNote::updatePreset(const dv::value& data) {
preset.deserialize(data);
}
void TextNote::setPosition(const glm::vec3& position) {
void TextNote::setPosition(const worldvec3& position) {
this->position = position;
}
const glm::vec3& TextNote::getPosition() const {
const worldvec3& TextNote::getPosition() const {
return position;
}

View file

@ -7,13 +7,13 @@
class TextNote {
std::wstring text;
NotePreset preset;
glm::vec3 position;
worldvec3 position;
entityid_t entity = ENTITY_NONE;
glm::vec3 xAxis {1, 0, 0};
glm::vec3 yAxis {0, 1, 0};
public:
TextNote(std::wstring text, NotePreset preset, glm::vec3 position);
TextNote(std::wstring text, NotePreset preset, worldvec3 position);
void setText(std::wstring_view text);
void setEntity(entityid_t eid);
@ -25,8 +25,8 @@ public:
void updatePreset(const dv::value& data);
void setPosition(const glm::vec3& position);
const glm::vec3& getPosition() const;
void setPosition(const worldvec3& position);
const worldvec3& getPosition() const;
const glm::vec3& getAxisX() const;
const glm::vec3& getAxisY() const;

View file

@ -35,16 +35,16 @@ void TextsRenderer::renderNote(
) {
const auto& text = note.getText();
const auto& preset = note.getPreset();
auto pos = note.getPosition();
auto worldPos = note.getPosition();
entityid_t eid = note.getEntity();
if (eid != ENTITY_NONE) {
if (auto entity = level.entities->get(eid)) {
pos += entity->getTransform().displayPos;
worldPos += entity->getTransform().displayPos;
}
}
pos = util::to_local(pos, camera.getOrigin());
glm::vec3 pos = util::to_local(worldPos, camera.getOrigin());
const glm::vec3 cameraPos = camera.getLocalPosition();
if (util::distance2(pos, cameraPos) >

View file

@ -7,7 +7,7 @@
using namespace scripting;
static int l_wrap(lua::State* L) {
auto position = lua::tovec3(L, 1);
auto position = lua::tovec<3, double>(L, 1);
std::string texture = lua::require_string(L, 2);
float emission = lua::isnumber(L, 3) ? lua::tonumber(L, 3) : 1.0f;
glm::vec4 tint =
@ -31,7 +31,7 @@ static int l_unwrap(lua::State* L) {
static int l_set_pos(lua::State* L) {
if (auto wrapper = renderer->blockWraps->get(lua::tointeger(L, 1))) {
wrapper->position = lua::tovec3(L, 2);
wrapper->position = lua::tovec<3, double>(L, 2);
}
return 0;
}

View file

@ -24,8 +24,8 @@ static int l_save_fragment(lua::State* L) {
}
static int l_create_fragment(lua::State* L) {
auto pointA = lua::tovec<3>(L, 1);
auto pointB = lua::tovec<3>(L, 2);
auto pointA = lua::tovec<3, double>(L, 1);
auto pointB = lua::tovec<3, double>(L, 2);
bool crop = lua::toboolean(L, 3);
bool saveEntities = lua::toboolean(L, 4);

View file

@ -9,7 +9,7 @@ using namespace scripting;
static int l_emit(lua::State* L) {
EmitterOrigin origin;
if (lua::istable(L, 1)) {
origin = lua::tovec3(L, 1);
origin = lua::tovec<3, double>(L, 1);
} else {
origin = static_cast<entityid_t>(lua::tointeger(L, 1));
}
@ -47,8 +47,8 @@ static int l_get_origin(lua::State* L) {
u64id_t id = lua::touinteger(L, 1);
if (auto emitter = renderer->particles->getEmitter(id)) {
const auto& origin = emitter->getOrigin();
if (auto pos = std::get_if<glm::vec3>(&origin)) {
return lua::pushvec3(L, *pos);
if (auto pos = std::get_if<worldvec3>(&origin)) {
return lua::pushvec(L, *pos);
} else if (auto entityid = std::get_if<entityid_t>(&origin)) {
return lua::pushinteger(L, *entityid);
}
@ -60,7 +60,7 @@ static int l_set_origin(lua::State* L) {
u64id_t id = lua::touinteger(L, 1);
if (auto emitter = renderer->particles->getEmitter(id)) {
if (lua::istable(L, 2)) {
emitter->setOrigin(lua::tovec3(L, 2));
emitter->setOrigin(lua::tovec<3, double>(L, 2));
} else {
emitter->setOrigin(static_cast<entityid_t>(lua::tointeger(L, 2)));
}

View file

@ -46,8 +46,8 @@ static int push_route(lua::State* L, const voxels::Route& route) {
static int l_make_route(lua::State* L) {
if (auto agent = get_agent(L)) {
auto start = lua::tovec3(L, 2);
auto target = lua::tovec3(L, 3);
auto start = lua::tovec<3, double>(L, 2);
auto target = lua::tovec<3, double>(L, 3);
agent->state = {};
agent->start = glm::floor(start);
agent->target = target;
@ -62,8 +62,8 @@ static int l_make_route(lua::State* L) {
static int l_make_route_async(lua::State* L) {
if (auto agent = get_agent(L)) {
auto start = lua::tovec3(L, 2);
auto target = lua::tovec3(L, 3);
auto start = lua::tovec<3, double>(L, 2);
auto target = lua::tovec<3, double>(L, 3);
agent->state = {};
agent->start = glm::floor(start);
agent->target = target;

View file

@ -6,7 +6,7 @@
using namespace scripting;
static int l_show(lua::State* L) {
auto position = lua::tovec3(L, 1);
auto position = lua::tovec<3, double>(L, 1);
auto text = lua::require_wstring(L, 2);
auto preset = lua::tovalue(L, 3);
auto extension = lua::tovalue(L, 4);
@ -47,7 +47,7 @@ static int l_get_pos(lua::State* L) {
}
static int l_set_pos(lua::State* L) {
if (auto note = renderer->texts->get(lua::tointeger(L, 1))) {
note->setPosition(lua::tovec3(L, 2));
note->setPosition(lua::tovec<3, double>(L, 2));
}
return 0;
}

View file

@ -52,7 +52,7 @@ voxel& Chunks::require(int32_t x, int32_t y, int32_t z) const {
return blocks_agent::require(*this, x, y, z);
}
const AABB* Chunks::isObstacleAt(float x, float y, float z) const {
const AABB* Chunks::isObstacleAt(double x, double y, double z) const {
int ix = std::floor(x);
int iy = std::floor(y);
int iz = std::floor(z);

View file

@ -123,9 +123,9 @@ public:
const worldvec3& start, const glm::vec3& dir, float maxDist
) const;
const AABB* isObstacleAt(float x, float y, float z) const;
const AABB* isObstacleAt(double x, double y, double z) const;
const AABB* isObstacleAt(const glm::vec3& pos) const {
const AABB* isObstacleAt(const worldvec3& pos) const {
return isObstacleAt(pos.x, pos.y, pos.z);
}