1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: Patrick Boenzli |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS |
---|
17 | |
---|
18 | #include "plane_emitter.h" |
---|
19 | |
---|
20 | #include "particle_system.h" |
---|
21 | |
---|
22 | #include "util/loading/load_param.h" |
---|
23 | #include "util/loading/factory.h" |
---|
24 | #include "debug.h" |
---|
25 | #include "stdlibincl.h" |
---|
26 | |
---|
27 | |
---|
28 | |
---|
29 | |
---|
30 | CREATE_FACTORY(PlaneEmitter, CL_PLANE_EMITTER); |
---|
31 | |
---|
32 | /** |
---|
33 | * standard constructor |
---|
34 | */ |
---|
35 | PlaneEmitter::PlaneEmitter(const Vector2D& size, float emissionRate, float velocity, float angle) |
---|
36 | : ParticleEmitter(emissionRate, velocity, angle) |
---|
37 | { |
---|
38 | this->init(); |
---|
39 | this->setSize(size); |
---|
40 | } |
---|
41 | |
---|
42 | /** |
---|
43 | * constructs and loads a PlaneEmitter from a XML-element |
---|
44 | * @param root the XML-element to load from |
---|
45 | */ |
---|
46 | PlaneEmitter::PlaneEmitter(const TiXmlElement* root) |
---|
47 | : ParticleEmitter() |
---|
48 | { |
---|
49 | this->init(); |
---|
50 | |
---|
51 | if (root != NULL) |
---|
52 | this->loadParams(root); |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | * standard destructor |
---|
57 | |
---|
58 | removes the EmitterSystem from the ParticleEngine |
---|
59 | */ |
---|
60 | PlaneEmitter::~PlaneEmitter () |
---|
61 | { |
---|
62 | this->setSystem(NULL); |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | @brief initializes default values of a ParitcleEmitter |
---|
67 | */ |
---|
68 | void PlaneEmitter::init() |
---|
69 | { |
---|
70 | this->setClassID(CL_PLANE_EMITTER, "PlaneEmitter"); |
---|
71 | this->setSize(1.0f, 1.0f); |
---|
72 | } |
---|
73 | |
---|
74 | void PlaneEmitter::loadParams(const TiXmlElement* root) |
---|
75 | { |
---|
76 | ParticleEmitter::loadParams(root); |
---|
77 | |
---|
78 | LoadParam(root, "size", this, PlaneEmitter, setSize) |
---|
79 | .describe("The Size of the PlaneEmitter: x, y") |
---|
80 | .defaultValues(1.0f,1.0f); |
---|
81 | } |
---|
82 | |
---|
83 | void PlaneEmitter::setSize(float x, float y) |
---|
84 | { |
---|
85 | this->size = Vector2D(x, y); |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | void PlaneEmitter::emitParticles(unsigned int count) const |
---|
90 | { |
---|
91 | Vector inheritVelocity = this->getVelocity() * this->inheritSpeed; |
---|
92 | |
---|
93 | Vector xDir = this->getAbsDirX() * this->size.x; |
---|
94 | Vector yDir = this->getAbsDirZ() * this->size.y; |
---|
95 | Vector zDir = this->getAbsDirY(); |
---|
96 | |
---|
97 | for (unsigned int i = 0; i < count; i++) |
---|
98 | { |
---|
99 | Vector randDir = Vector(rand()-RAND_MAX/2, rand()-RAND_MAX/2, rand()-RAND_MAX/2); |
---|
100 | randDir.normalize(); |
---|
101 | randDir = (Quaternion(angle + randomAngle *((float)rand()/RAND_MAX -.5), randDir)).apply(zDir); |
---|
102 | Vector velocityV = randDir.getNormalized()*this->velocity + inheritVelocity; |
---|
103 | |
---|
104 | Vector plane = this->getAbsCoor() + |
---|
105 | xDir * ((float)rand()/RAND_MAX -.5) + |
---|
106 | yDir * ((float)rand()/RAND_MAX -.5); |
---|
107 | |
---|
108 | // ROTATIONAL CALCULATION (this must not be done for all types of particles.) |
---|
109 | randDir = Vector(rand()-RAND_MAX/2, rand()-RAND_MAX/2, rand()-RAND_MAX/2); |
---|
110 | randDir.normalize(); |
---|
111 | Quaternion orient = Quaternion(M_PI, randDir); |
---|
112 | Quaternion moment = Quaternion(this->momentum + this->momentumRandom, randDir); |
---|
113 | |
---|
114 | this->getSystem()->addParticle(plane, velocityV, orient, moment); |
---|
115 | |
---|
116 | } |
---|
117 | } |
---|