TDME2  1.9.200
SphereParticleEmitter.cpp
Go to the documentation of this file.
2 
3 #include <memory>
4 
5 #include <tdme/tdme.h>
10 #include <tdme/math/Math.h>
11 #include <tdme/math/Vector3.h>
12 
13 using std::make_unique;
14 using std::unique_ptr;
15 
22 using tdme::math::Math;
24 
25 SphereParticleEmitter::SphereParticleEmitter(int32_t count, int64_t lifeTime, int64_t lifeTimeRnd, float mass, float massRnd, Sphere* sphere, const Vector3& velocity, const Vector3& velocityRnd, const Color4& colorStart, const Color4& colorEnd)
26 {
27  this->count = count;
28  this->lifeTime = lifeTime;
29  this->lifeTimeRnd = lifeTimeRnd;
30  this->mass = mass;
31  this->massRnd = massRnd;
32  this->sphere = unique_ptr<Sphere>(sphere);
33  this->worldSphere = unique_ptr<Sphere>(static_cast<Sphere*>(sphere->clone()));
34  this->velocity.set(velocity);
35  this->velocityRnd.set(velocityRnd);
36  this->colorStart.set(colorStart);
37  this->colorEnd.set(colorEnd);
38 }
39 
41 }
42 
44 {
45  // set up particle
46  particle->active = true;
47  particle->spriteIndex = 0.0f;
48  particle->position.set(
49  Math::random() * 2.0f - 1.0f,
50  Math::random() * 2.0f - 1.0f,
51  Math::random() * 2.0f - 1.0f
52  ).normalize().scale(worldSphere->getRadius());
53  particle->velocity.set(
54  velocity[0] + (Math::random() * velocityRnd[0] * (Math::random() > 0.5 ? +1.0f : -1.0f)),
55  velocity[1] + (Math::random() * velocityRnd[1] * (Math::random() > 0.5 ? +1.0f : -1.0f)),
56  velocity[2] + (Math::random() * velocityRnd[2] * (Math::random() > 0.5 ? +1.0f : -1.0f))
57  );
58  particle->mass = mass + (Math::random() * (massRnd));
59  particle->lifeTimeMax = lifeTime + static_cast<int64_t>((Math::random() * lifeTimeRnd));
60  particle->lifeTimeCurrent = 0LL;
61  particle->color.set(colorStart);
62  particle->colorAdd.set(
63  (colorEnd.getRed() - colorStart.getRed()) / particle->lifeTimeMax,
64  (colorEnd.getGreen() - colorStart.getGreen()) / particle->lifeTimeMax,
65  (colorEnd.getBlue() - colorStart.getBlue()) / particle->lifeTimeMax,
66  (colorEnd.getAlpha() - colorStart.getAlpha()) / particle->lifeTimeMax
67  );
68 }
69 
71 {
72  const auto& transformMatrix = transform.getTransformMatrix();
73  // apply translations
74  Vector3 worldCenter;
75  // translate center
76  worldCenter = transformMatrix.multiply(sphere->getCenter());
77  // world sphere
78  Vector3 worldScale;
79  transformMatrix.getScale(worldScale);
80  worldSphere = make_unique<Sphere>(worldCenter, sphere->getRadius() * Math::max(worldScale.getX(), Math::max(worldScale.getY(), worldScale.getZ())));
81 }
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
Sphere physics primitive.
Definition: Sphere.h:19
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
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 & 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