TDME2  1.9.200
BoundingBoxParticleEmitter.cpp
Go to the documentation of this file.
2 
3 #include <memory>
4 
5 #include <tdme/tdme.h>
6 #include <tdme/engine/Color4.h>
10 #include <tdme/engine/Transform.h>
11 #include <tdme/math/Math.h>
12 #include <tdme/math/Vector3.h>
13 
14 using std::make_unique;
15 using std::unique_ptr;
16 
23 using tdme::math::Math;
25 
26 BoundingBoxParticleEmitter::BoundingBoxParticleEmitter(int32_t count, int64_t lifeTime, int64_t lifeTimeRnd, float mass, float massRnd, OrientedBoundingBox* obb, const Vector3& velocity, const Vector3& velocityRnd, const Color4& colorStart, const Color4& colorEnd)
27 {
28  this->count = count;
29  this->lifeTime = lifeTime;
30  this->lifeTimeRnd = lifeTimeRnd;
31  this->mass = mass;
32  this->massRnd = massRnd;
33  this->obb = unique_ptr<OrientedBoundingBox>(obb);
34  this->velocity.set(velocity);
35  this->velocityRnd.set(velocityRnd);
36  this->colorStart.set(colorStart);
37  this->colorEnd.set(colorEnd);
38  this->worldObb = unique_ptr<OrientedBoundingBox>(static_cast<OrientedBoundingBox*>(obb->clone()));
39 }
40 
42 }
43 
45 {
46  Vector3 tmpAxis;
47  // set up particle
48  particle->active = true;
49  particle->spriteIndex = 0.0f;
50  auto obbAxes = worldObb->getAxes();
51  const auto& obbHalfExtension = worldObb->getHalfExtension();
52  // emit particle in oriented bounding box
53  particle->position.set(0.0f, 0.0f, 0.0f);
54  particle->position.add(tmpAxis.set(obbAxes[0]).scale((static_cast<float>(Math::random()) * obbHalfExtension[0] * 2.0f) - obbHalfExtension[0]));
55  particle->position.add(tmpAxis.set(obbAxes[1]).scale((static_cast<float>(Math::random()) * obbHalfExtension[1] * 2.0f) - obbHalfExtension[1]));
56  particle->position.add(tmpAxis.set(obbAxes[2]).scale((static_cast<float>(Math::random()) * obbHalfExtension[2] * 2.0f) - obbHalfExtension[2]));
57  // compute velocity
58  particle->velocity.set(
59  velocity[0] + (Math::random() * velocityRnd[0] * (Math::random() > 0.5 ? +1.0f : -1.0f)),
60  velocity[1] + (Math::random() * velocityRnd[1] * (Math::random() > 0.5 ? +1.0f : -1.0f)),
61  velocity[2] + (Math::random() * velocityRnd[2] * (Math::random() > 0.5 ? +1.0f : -1.0f))
62  );
63  // mass
64  particle->mass = mass + (Math::random() * (massRnd));
65  // life time
66  particle->lifeTimeMax = lifeTime + (Math::random() * lifeTimeRnd);
67  particle->lifeTimeCurrent = 0LL;
68  // color
69  particle->color.set(colorStart);
70  particle->colorAdd.set(
71  (colorEnd.getRed() - colorStart.getRed()) / particle->lifeTimeMax,
72  (colorEnd.getGreen() - colorStart.getGreen()) / particle->lifeTimeMax,
73  (colorEnd.getBlue() - colorStart.getBlue()) / particle->lifeTimeMax,
74  (colorEnd.getAlpha() - colorStart.getAlpha()) / particle->lifeTimeMax);
75 }
76 
78 {
79  Vector3 worldCenter;
80  Vector3 worldScale;
81  array<Vector3, 3> worldAxes;
82  array<Vector3, 3> worldAxesUnnormalized;
83  Vector3 worldHalfExtension;
84  const auto& transformMatrix = transform.getTransformMatrix();
85  // apply rotation, scale, translation
86  worldCenter = transformMatrix.multiply(obb->getCenter());
87  // apply transform rotation to axis
88  worldAxesUnnormalized[0] = transformMatrix.multiplyNoTranslation(obb->getAxes()[0]);
89  worldAxesUnnormalized[1] = transformMatrix.multiplyNoTranslation(obb->getAxes()[1]);
90  worldAxesUnnormalized[2] = transformMatrix.multiplyNoTranslation(obb->getAxes()[2]);
91  // scale
92  worldScale.set(
93  worldAxesUnnormalized[0].computeLength(),
94  worldAxesUnnormalized[1].computeLength(),
95  worldAxesUnnormalized[2].computeLength()
96  );
97  // set up axes
98  worldAxes[0].set(worldAxesUnnormalized[0]).normalize();
99  worldAxes[1].set(worldAxesUnnormalized[1]).normalize();
100  worldAxes[2].set(worldAxesUnnormalized[2]).normalize();
101  // apply scale to half extension
102  worldHalfExtension.set(obb->getHalfExtension());
103  worldHalfExtension.scale(worldScale);
104  worldObb = make_unique<OrientedBoundingBox>(worldCenter, worldAxes[0], worldAxes[1], worldAxes[2], worldHalfExtension);
105 }
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
Oriented bounding box physics primitive.
void setTransform(const Transform &transform) override
Update transform with given transform.
Standard math functions.
Definition: Math.h:19
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
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