A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(render): looping particle sheets cycle instead of freezing on the last frame
esenginef51090c44674
diff --git a/src/esengine/particle/ParticleSystem.cpp b/src/esengine/particle/ParticleSystem.cppindex 6c842c52b..635b6d3b4 100644--- a/src/esengine/particle/ParticleSystem.cpp+++ b/src/esengine/particle/ParticleSystem.cpp@@ -4,6 +4,7 @@#include <algorithm>#include <chrono>+#include <cmath>namespace esengine::particle {@@ -202,7 +203,19 @@ void ParticleSystem::emitParticles(const ecs::ParticleEmitter& emitter,bool isWorldSpace = emitter.simulationSpace ==static_cast<i32>(ecs::SimulationSpace::World);if (isWorldSpace) {- p->position = emitterPos + offset;+ // Rotate the spawn footprint by the emitter angle so the emission+ // shape agrees with the (already-rotated) velocity below; otherwise a+ // rotated world-space cone/box spawns along its unrotated axis while+ // the flow points elsewhere. `offset` itself stays unrotated — the+ // Circle aim reads it and gets the same rotation applied once, below.+ glm::vec2 worldOffset = offset;+ if (std::abs(emitterAngle) > 0.001f) {+ f32 cosA = std::cos(emitterAngle);+ f32 sinA = std::sin(emitterAngle);+ worldOffset = glm::vec2(offset.x * cosA - offset.y * sinA,+ offset.x * sinA + offset.y * cosA);+ }+ p->position = emitterPos + worldOffset;} else {p->position = offset;}@@ -308,11 +321,15 @@ void ParticleSystem::updateParticles(const ecs::ParticleEmitter& emitter,if (totalFrames > 1 && emitter.spriteFPS > 0.0f) {f32 frameDuration = 1.0f / emitter.spriteFPS;- u16 frame = static_cast<u16>(std::min(p.age / frameDuration, static_cast<f32>(totalFrames - 1)));+ f32 rawFrame = p.age / frameDuration;+ // Compute the wrap on the float (fmod) so a looping sheet actually+ // cycles: clamping to [0,totalFrames-1] first would make the modulo an+ // identity and freeze every particle on the last frame after one pass.+ u16 frame;if (emitter.spriteLoop) {- frame = frame % static_cast<u16>(totalFrames);+ frame = static_cast<u16>(std::fmod(rawFrame, static_cast<f32>(totalFrames)));} else {- frame = std::min(frame, static_cast<u16>(totalFrames - 1));+ frame = static_cast<u16>(std::min(rawFrame, static_cast<f32>(totalFrames - 1)));}p.sprite_frame = frame;}diff --git a/tests/particle/test_particle.cpp b/tests/particle/test_particle.cppindex d01a050be..c37392433 100644--- a/tests/particle/test_particle.cpp+++ b/tests/particle/test_particle.cpp@@ -166,6 +166,40 @@ TEST_CASE("system_particles_die_after_lifetime") {CHECK_EQ(system.totalAliveParticles(), 0u);}+TEST_CASE("system_sprite_sheet_loops_instead_of_freezing") {+ // A looping sprite sheet must wrap back toward frame 0 after a full pass. A+ // pre-clamp regression made `frame % totalFrames` an identity, freezing every+ // particle on the last frame (index totalFrames-1) forever after one cycle.+ ecs::Registry registry;+ ParticleSystem system;++ Entity e = registry.create();+ registry.emplace<ecs::Transform>(e);+ auto& emitter = registry.emplace<ecs::ParticleEmitter>(e);+ emitter.rate = 500.0f;+ emitter.lifetimeMin = 100.0f;+ emitter.lifetimeMax = 100.0f;+ emitter.maxParticles = 1000;+ emitter.enabled = true;+ emitter.playOnStart = true;+ emitter.looping = false;+ emitter.duration = 0.02f; // one short burst, then emission stops+ emitter.spriteColumns = 4; // 4 frames total+ emitter.spriteRows = 1;+ emitter.spriteFPS = 1.0f; // one 4-frame cycle every 4 s++ system.update(registry, 0.02f); // spawn the burst (age ~0.02 s → frame 0)+ CHECK(system.totalAliveParticles() > 0u);++ system.update(registry, 4.1f); // age ~4.12 s → past a full cycle, wrapped+ u32 checked = 0;+ system.forEachParticle(e, [&](const Particle& p) {+ CHECK(p.sprite_frame < 3); // 3 = last frame; the bug froze here+ checked++;+ });+ CHECK(checked > 0u);+}+TEST_CASE("system_non_looping_stops_after_duration") {ecs::Registry registry;ParticleSystem system;