Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
ParticleShapes.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
16#pragma once
17
18#include "../core/Types.hpp"
19#include "../math/Math.hpp"
21
22#include <cmath>
23#include <functional>
24
25namespace esengine::particle {
26
28using RandomRange = std::function<f32(f32, f32)>;
29
33 glm::vec3 position{0.0f};
34 glm::quat rotation{1.0f, 0.0f, 0.0f, 0.0f};
35 glm::vec3 scale{1.0f};
36};
37
39inline glm::vec3 worldPointOf(const glm::vec3& local, const EmitterFrame& f) {
40 return f.position + f.rotation * (local * f.scale);
41}
42
48inline glm::vec3 worldDirOf(const glm::vec3& local, const EmitterFrame& f) {
49 return f.rotation * local;
50}
51
54 glm::vec3 position{0.0f};
55 glm::vec3 direction{0.0f, 1.0f, 0.0f};
56};
57
65inline glm::vec3 coneDirection(f32 halfAngle, const RandomRange& rand) {
66 const f32 cosTheta = rand(std::cos(halfAngle), 1.0f);
67 const f32 sinTheta = std::sqrt(std::max(0.0f, 1.0f - cosTheta * cosTheta));
68 const f32 phi = rand(0.0f, math::TWO_PI);
69 return glm::vec3(sinTheta * std::cos(phi), cosTheta, sinTheta * std::sin(phi));
70}
71
73inline SpawnSample sampleShape(const ecs::ParticleEmitter& emitter, const RandomRange& rand) {
75 // Point and Box have no aim of their own, so they take one: an angle in the
76 // emitter's local XY plane, which is what every 2D emitter has always meant.
77 auto planarAim = [&] {
78 const f32 a = rand(emitter.angleSpreadMin, emitter.angleSpreadMax) * math::DEG_TO_RAD;
79 return glm::vec3(std::cos(a), std::sin(a), 0.0f);
80 };
81
82 switch (static_cast<ecs::EmitterShape>(emitter.shape)) {
84 // A disc in the emitter's LOCAL plane, not the world's — a rotated
85 // emitter carries its circle with it.
86 const f32 angle = rand(0.0f, math::TWO_PI);
87 const f32 radius = rand(0.0f, emitter.shapeRadius);
88 s.position = glm::vec3(std::cos(angle) * radius, std::sin(angle) * radius, 0.0f);
89 s.direction = radius > 0.001f ? s.position / radius : planarAim();
90 break;
91 }
93 // A filled VOLUME. A depthless one is the rectangle 2D always had.
94 s.position = glm::vec3(
95 rand(-emitter.shapeSize.x * 0.5f, emitter.shapeSize.x * 0.5f),
96 rand(-emitter.shapeSize.y * 0.5f, emitter.shapeSize.y * 0.5f),
97 emitter.shapeSize.z > 0.0f
98 ? rand(-emitter.shapeSize.z * 0.5f, emitter.shapeSize.z * 0.5f) : 0.0f);
99 s.direction = planarAim();
100 break;
101 }
103 const f32 halfAngle = emitter.shapeAngle * 0.5f * math::DEG_TO_RAD;
104 s.direction = coneDirection(halfAngle, rand);
105 // Spawn spread sampled INDEPENDENTLY of the velocity, so a particle's
106 // birthplace does not predict where it goes.
107 s.position = coneDirection(halfAngle, rand) * rand(0.0f, emitter.shapeRadius);
108 break;
109 }
112 const bool full = static_cast<ecs::EmitterShape>(emitter.shape)
114 const f32 half = full ? math::PI : math::HALF_PI;
115 s.direction = coneDirection(half, rand);
116 // Uniform through the VOLUME: a radius linear in the draw piles the
117 // particles at the centre, which is a ball made of a spike.
118 s.position = coneDirection(half, rand)
119 * (emitter.shapeRadius * std::cbrt(rand(0.0f, 1.0f)));
120 break;
121 }
123 default:
124 s.direction = planarAim();
125 break;
126 }
127 return s;
128}
129
130} // namespace esengine::particle
Math utilities and GLM configuration for ESEngine.
Core type definitions and utilities for ESEngine.
constexpr float PI
Pi constant.
Definition Math.hpp:67
constexpr float TWO_PI
Two times pi (full circle in radians).
Definition Math.hpp:70
constexpr float HALF_PI
Half pi (quarter circle in radians).
Definition Math.hpp:73
constexpr float DEG_TO_RAD
Degrees to radians conversion factor.
Definition Math.hpp:76
std::function< f32(f32, f32)> RandomRange
SpawnSample sampleShape(const ecs::ParticleEmitter &emitter, const RandomRange &rand)
glm::vec3 worldDirOf(const glm::vec3 &local, const EmitterFrame &f)
A local direction placed in the world — rotation ALONE.
glm::vec3 coneDirection(f32 halfAngle, const RandomRange &rand)
A direction inside the cap of half-angle halfAngle about local +Y.
glm::vec3 worldPointOf(const glm::vec3 &local, const EmitterFrame &f)
float f32
32-bit floating point
Definition Types.hpp:59