Skip to content

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.

fix(particle): rectangle emitter aims by angleSpread instead of hardcoded up

yhh585cfb46a127
diff --git a/src/esengine/particle/ParticleSystem.cpp b/src/esengine/particle/ParticleSystem.cpp
index a8da50951..080b25b62 100644
--- a/src/esengine/particle/ParticleSystem.cpp
+++ b/src/esengine/particle/ParticleSystem.cpp
@@ -216,15 +216,17 @@ void ParticleSystem::emitParticles(const ecs::ParticleEmitter& emitter,
}
break;
}
- case ecs::EmitterShape::Rectangle:
- dir = glm::vec2(0.0f, 1.0f);
- break;
case ecs::EmitterShape::Cone: {
f32 halfAngle = emitter.shapeAngle * 0.5f * math::DEG_TO_RAD;
f32 angle = randomRange(-halfAngle, halfAngle);
dir = glm::vec2(std::sin(angle), std::cos(angle));
break;
}
+ // Point and Rectangle share one aiming rule: the shape decides WHERE a
+ // particle spawns (a single point vs. a filled box), while angleSpread
+ // decides which way it flies. Circle and Cone override this with their
+ // own radial / conic aim, which is intrinsic to those shapes.
+ case ecs::EmitterShape::Rectangle:
case ecs::EmitterShape::Point:
default:
dir = randomDirection(emitter.angleSpreadMin, emitter.angleSpreadMax);
diff --git a/tests/particle/test_particle.cpp b/tests/particle/test_particle.cpp
index 05cfc80a6..d01a050be 100644
--- a/tests/particle/test_particle.cpp
+++ b/tests/particle/test_particle.cpp
@@ -920,7 +920,7 @@ TEST_CASE("shape_circle_direction_radial_outward") {
CHECK(allRadial);
}
-TEST_CASE("shape_rectangle_direction_upward") {
+TEST_CASE("shape_rectangle_direction_follows_angle_spread") {
ecs::Registry registry;
ParticleSystem system;
@@ -935,6 +935,10 @@ TEST_CASE("shape_rectangle_direction_upward") {
emitter.maxParticles = 200;
emitter.speedMin = 10.0f;
emitter.speedMax = 10.0f;
+ // A rectangle is an area emitter: the shape spreads only WHERE particles
+ // spawn; direction comes from angleSpread, exactly like a point. Aim down.
+ emitter.angleSpreadMin = 260.0f;
+ emitter.angleSpreadMax = 280.0f;
emitter.gravity = glm::vec2(0.0f);
emitter.damping = 0.0f;
emitter.enabled = true;
@@ -943,15 +947,18 @@ TEST_CASE("shape_rectangle_direction_upward") {
system.update(registry, 0.01f);
CHECK(system.aliveCount(e) > 0u);
- bool allUpward = true;
+ bool allDownward = true;
+ int checked = 0;
system.forEachParticle(e, [&](const Particle& p) {
if (glm::length(p.velocity) < 0.001f) return;
glm::vec2 dir = glm::normalize(p.velocity);
- if (dir.y < 0.95f) {
- allUpward = false;
+ if (dir.y > -0.9f) {
+ allDownward = false;
}
+ checked++;
});
- CHECK(allUpward);
+ CHECK(checked > 0);
+ CHECK(allDownward);
}
TEST_CASE("shape_cone_direction_within_angle") {