TDME2  1.9.200
CircleParticleEmitter.cpp
Go to the documentation of this file.
2 
3 #include <tdme/tdme.h>
4 #include <tdme/engine/Color4.h>
7 #include <tdme/math/Math.h>
8 #include <tdme/math/Matrix4x4.h>
9 #include <tdme/math/Vector3.h>
10 
15 using tdme::math::Math;
18 
19 CircleParticleEmitter::CircleParticleEmitter(int32_t count, int64_t lifeTime, int64_t lifeTimeRnd, const Vector3& axis0, const Vector3& axis1, const Vector3& center, float radius, float mass, float massRnd, const Vector3& velocity, const Vector3& velocityRnd, const Color4& colorStart, const Color4& colorEnd)
20 {
21  this->count = count;
22  this->lifeTime = lifeTime;
23  this->lifeTimeRnd = lifeTimeRnd;
24  this->axis0.set(axis0).normalize();
25  this->axis1.set(axis1).normalize();
26  this->center.set(center);
27  this->radius = radius;
28  this->worldAxis0.set(axis0).normalize();
29  this->worldAxis1.set(axis1).normalize();
30  this->worldCenter.set(center);
31  this->worldRadius = radius;
32  this->mass = mass;
33  this->massRnd = massRnd;
34  this->velocity.set(velocity);
35  this->velocityRnd.set(velocityRnd);
36  this->colorStart.set(colorStart);
37  this->colorEnd.set(colorEnd);
38 }
39 
41 {
42  Vector3 cosOnAxis0;
43  Vector3 sinOnAxis1;
44  // set up particle
45  particle->active = true;
46  particle->spriteIndex = 0.0f;
47  // emit particle in circle spanned on axis 0 and axis 1
48  auto rnd = static_cast<float>(Math::random());
49  cosOnAxis0.set(worldAxis0).scale(Math::cos(Math::PI * 2 * rnd));
50  sinOnAxis1.set(worldAxis1).scale(Math::sin(Math::PI * 2 * rnd));
51  particle->position.set(cosOnAxis0);
52  particle->position.add(sinOnAxis1);
53  particle->position.scale(worldRadius);
54  // compute velocity
55  particle->velocity.set(
56  velocity[0] + (Math::random() * velocityRnd[0] * (Math::random() > 0.5 ? +1.0f : -1.0f)),
57  velocity[1] + (Math::random() * velocityRnd[1] * (Math::random() > 0.5 ? +1.0f : -1.0f)),
58  velocity[2] + (Math::random() * velocityRnd[2] * (Math::random() > 0.5 ? +1.0f : -1.0f))
59  );
60  // mass
61  particle->mass = mass + static_cast<float>((Math::random() * (massRnd)));
62  // life time
63  particle->lifeTimeMax = lifeTime + static_cast<int64_t>((Math::random() * lifeTimeRnd));
64  particle->lifeTimeCurrent = 0LL;
65  // color
66  particle->color.set(colorStart);
67  particle->colorAdd.set(
68  (colorEnd.getRed() - colorStart.getRed()) / particle->lifeTimeMax,
69  (colorEnd.getGreen() - colorStart.getGreen()) / particle->lifeTimeMax,
70  (colorEnd.getBlue() - colorStart.getBlue()) / particle->lifeTimeMax,
71  (colorEnd.getAlpha() - colorStart.getAlpha()) / particle->lifeTimeMax
72  );
73 }
74 
76 {
77  const auto& transformMatrix = transform.getTransformMatrix();
78  // apply rotation, scale, translation
79  worldCenter = transformMatrix.multiply(center);
80  // apply transform rotation + scale to axis
81  worldAxis0 = transformMatrix.multiplyNoTranslation(axis0).normalize();
82  worldAxis1 = transformMatrix.multiplyNoTranslation(axis1).normalize();
83  // world radius
84  Vector3 worldScale;
85  transformMatrix.getScale(worldScale);
86  worldRadius = radius * Math::max(worldScale.getX(), Math::max(worldScale.getY(), worldScale.getZ()));
87 }
Color 4 definition class.
Definition: Color4.h:18
float getRed() const
Definition: Color4.h:92
float getGreen() const
Definition: Color4.h:107
float getAlpha() const
Definition: Color4.h:137
float getBlue() const
Definition: Color4.h:122
void set(float r, float g, float b, float a)
Sets this color by its components.
Definition: Color4.h:66
Transform which contain scale, rotations and translation.
Definition: Transform.h:29
const Matrix4x4 & getTransformMatrix() const
Definition: Transform.h:169
void setTransform(const Transform &transform) override
Update transform with given transform.
void emit(Particle *particle) override
Emits particles.
Standard math functions.
Definition: Math.h:19
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
float getY() const
Definition: Vector3.h:117
float getX() const
Definition: Vector3.h:100
float getZ() const
Definition: Vector3.h:134
Vector3 & add(float scalar)
Adds a scalar.
Definition: Vector3.h:153
Vector3 & scale(float scalar)
Scales by scalar.
Definition: Vector3.h:201
Vector3 & set(float x, float y, float z)
Sets this vector3 by its components.
Definition: Vector3.h:70
Vector3 & normalize()
Normalizes this vector3.
Definition: Vector3.h:239