Mercurial > love-experiments
annotate src/modules/graphics/opengl/ParticleSystem.cpp @ 1352:470a47bebfb3 particlesystem-quads
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
| author | Alex Szpakowski <slime73@gmail.com> |
|---|---|
| date | Sun, 10 Mar 2013 03:40:25 -0300 |
| parents | 730ed5f01ab0 |
| children | c3c9d6ed3e21 |
| rev | line source |
|---|---|
| 1096 | 1 /** |
|
1282
b7f097053d74
Updated copyright text for the new year
Alex Szpakowski <slime73@gmail.com>
parents:
1230
diff
changeset
|
2 * Copyright (c) 2006-2013 LOVE Development Team |
| 1096 | 3 * |
| 4 * This software is provided 'as-is', without any express or implied | |
| 5 * warranty. In no event will the authors be held liable for any damages | |
| 6 * arising from the use of this software. | |
| 7 * | |
| 8 * Permission is granted to anyone to use this software for any purpose, | |
| 9 * including commercial applications, and to alter it and redistribute it | |
| 10 * freely, subject to the following restrictions: | |
| 11 * | |
| 12 * 1. The origin of this software must not be misrepresented; you must not | |
| 13 * claim that you wrote the original software. If you use this software | |
| 14 * in a product, an acknowledgment in the product documentation would be | |
| 15 * appreciated but is not required. | |
| 16 * 2. Altered source versions must be plainly marked as such, and must not be | |
| 17 * misrepresented as being the original software. | |
| 18 * 3. This notice may not be removed or altered from any source distribution. | |
| 19 **/ | |
| 20 | |
| 21 #include "ParticleSystem.h" | |
| 22 | |
| 23 #include "common/math.h" | |
| 24 | |
|
1216
8ae3ebeeda85
opengl graphics module files now always include OpenGL.h rather than GLee.h
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
25 #include "OpenGL.h" |
| 1096 | 26 #include <cmath> |
| 27 #include <cstdlib> | |
| 28 | |
| 29 namespace love | |
| 30 { | |
| 31 namespace graphics | |
| 32 { | |
| 33 namespace opengl | |
| 34 { | |
| 35 | |
| 36 namespace | |
| 37 { | |
| 38 Colorf colorToFloat(const Color &c) | |
| 39 { | |
| 40 return Colorf((GLfloat)c.r/255.0f, (GLfloat)c.g/255.0f, (GLfloat)c.b/255.0f, (GLfloat)c.a/255.0f); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 float calculate_variation(float inner, float outer, float var) | |
| 45 { | |
| 46 float low = inner - (outer/2.0f)*var; | |
| 47 float high = inner + (outer/2.0f)*var; | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
48 float r = random(); |
| 1096 | 49 return low*(1-r)+high*r; |
| 50 } | |
| 51 | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
52 StringMap<ParticleSystem::AreaSpreadDistribution, ParticleSystem::DISTRIBUTION_MAX_ENUM>::Entry ParticleSystem::distributionsEntries[] = { |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
53 { "none", ParticleSystem::DISTRIBUTION_NONE }, |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
54 { "uniform", ParticleSystem::DISTRIBUTION_UNIFORM }, |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
55 { "normal", ParticleSystem::DISTRIBUTION_NORMAL }, |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
56 }; |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
57 |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
58 StringMap<ParticleSystem::AreaSpreadDistribution, ParticleSystem::DISTRIBUTION_MAX_ENUM> ParticleSystem::distributions(ParticleSystem::distributionsEntries, sizeof(ParticleSystem::distributionsEntries)); |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
59 |
| 1096 | 60 |
| 61 ParticleSystem::ParticleSystem(Image *sprite, unsigned int buffer) | |
| 62 : pStart(0) | |
| 63 , pLast(0) | |
| 64 , pEnd(0) | |
| 1147 | 65 , particleVerts(0) |
|
1307
1c61424c6024
Cleaned up exception handling code when creating new fonts, spritebatches, and shaders
Alex Szpakowski <slime73@gmail.com>
parents:
1282
diff
changeset
|
66 , sprite(sprite) |
| 1096 | 67 , active(true) |
| 68 , emissionRate(0) | |
| 69 , emitCounter(0) | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
70 , areaSpreadDistribution(DISTRIBUTION_NONE) |
| 1096 | 71 , lifetime(-1) |
| 72 , life(0) | |
| 73 , particleLifeMin(0) | |
| 74 , particleLifeMax(0) | |
| 75 , direction(0) | |
| 76 , spread(0) | |
| 77 , relative(false) | |
| 78 , speedMin(0) | |
| 79 , speedMax(0) | |
| 80 , gravityMin(0) | |
| 81 , gravityMax(0) | |
| 82 , radialAccelerationMin(0) | |
| 83 , radialAccelerationMax(0) | |
| 84 , tangentialAccelerationMin(0) | |
| 85 , tangentialAccelerationMax(0) | |
| 86 , sizeVariation(0) | |
| 87 , rotationMin(0) | |
| 88 , rotationMax(0) | |
| 89 , spinStart(0) | |
| 90 , spinEnd(0) | |
| 91 , spinVariation(0) | |
| 92 , offsetX(sprite->getWidth()*0.5f) | |
| 93 , offsetY(sprite->getHeight()*0.5f) | |
| 94 { | |
| 95 sizes.push_back(1.0f); | |
| 96 colors.push_back(Colorf(1.0f, 1.0f, 1.0f, 1.0f)); | |
| 97 setBufferSize(buffer); | |
|
1307
1c61424c6024
Cleaned up exception handling code when creating new fonts, spritebatches, and shaders
Alex Szpakowski <slime73@gmail.com>
parents:
1282
diff
changeset
|
98 sprite->retain(); |
| 1096 | 99 } |
| 100 | |
| 101 ParticleSystem::~ParticleSystem() | |
| 102 { | |
| 103 if (this->sprite != 0) | |
| 104 this->sprite->release(); | |
| 105 | |
| 106 if (pStart != 0) | |
| 107 delete [] pStart; | |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
108 |
| 1147 | 109 if (particleVerts != 0) |
| 110 delete [] particleVerts; | |
| 1096 | 111 } |
| 112 | |
| 113 void ParticleSystem::add() | |
| 114 { | |
| 115 if (isFull()) return; | |
| 116 | |
| 117 float min,max; | |
| 118 | |
| 119 min = particleLifeMin; | |
| 120 max = particleLifeMax; | |
| 121 if (min == max) | |
| 122 pLast->life = min; | |
| 123 else | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
124 pLast->life = random(min, max); |
| 1096 | 125 pLast->lifetime = pLast->life; |
| 126 | |
| 127 pLast->position[0] = position.getX(); | |
| 128 pLast->position[1] = position.getY(); | |
| 129 | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
130 switch (areaSpreadDistribution) |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
131 { |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
132 case DISTRIBUTION_UNIFORM: |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
133 pLast->position[0] += random(-areaSpread.getX(), areaSpread.getX()); |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
134 pLast->position[1] += random(-areaSpread.getY(), areaSpread.getY()); |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
135 break; |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
136 case DISTRIBUTION_NORMAL: |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
137 pLast->position[0] += random_normal(areaSpread.getX()); |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
138 pLast->position[1] += random_normal(areaSpread.getY()); |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
139 break; |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
140 case DISTRIBUTION_NONE: |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
141 default: |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
142 break; |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
143 } |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
144 |
| 1096 | 145 min = direction - spread/2.0f; |
| 146 max = direction + spread/2.0f; | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
147 pLast->direction = random(min, max); |
| 1096 | 148 |
| 149 pLast->origin = position; | |
| 150 | |
| 151 min = speedMin; | |
| 152 max = speedMax; | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
153 float speed = random(min, max); |
| 1096 | 154 pLast->speed = love::Vector(cos(pLast->direction), sin(pLast->direction)); |
| 155 pLast->speed *= speed; | |
| 156 | |
| 157 min = gravityMin; | |
| 158 max = gravityMax; | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
159 pLast->gravity = random(min, max); |
| 1096 | 160 |
| 161 min = radialAccelerationMin; | |
| 162 max = radialAccelerationMax; | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
163 pLast->radialAcceleration = random(min, max); |
| 1096 | 164 |
| 165 min = tangentialAccelerationMin; | |
| 166 max = tangentialAccelerationMax; | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
167 pLast->tangentialAcceleration = random(min, max); |
| 1096 | 168 |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
169 pLast->sizeOffset = random(sizeVariation); // time offset for size change |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
170 pLast->sizeIntervalSize = (1.0f - random(sizeVariation)) - pLast->sizeOffset; |
| 1096 | 171 pLast->size = sizes[(size_t)(pLast->sizeOffset - .5f) * (sizes.size() - 1)]; |
| 172 | |
| 173 min = rotationMin; | |
| 174 max = rotationMax; | |
| 175 pLast->spinStart = calculate_variation(spinStart, spinEnd, spinVariation); | |
| 176 pLast->spinEnd = calculate_variation(spinEnd, spinStart, spinVariation); | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
177 pLast->rotation = random(min, max); |
| 1096 | 178 |
| 179 pLast->color = colors[0]; | |
| 180 | |
| 181 pLast++; | |
| 182 } | |
| 183 | |
| 184 void ParticleSystem::remove(particle *p) | |
| 185 { | |
| 186 if (!isEmpty()) | |
| 187 { | |
| 188 *p = *(--pLast); | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 void ParticleSystem::setSprite(Image *image) | |
| 193 { | |
| 194 if (sprite != 0) | |
| 195 sprite->release(); | |
| 196 | |
| 197 sprite = image; | |
| 198 sprite->retain(); | |
| 199 } | |
| 200 | |
| 201 void ParticleSystem::setBufferSize(unsigned int size) | |
| 202 { | |
| 203 // delete previous data | |
| 1147 | 204 if (pStart != 0) |
| 205 delete [] pStart; | |
| 1096 | 206 |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
207 pLast = pStart = new particle[size](); |
| 1096 | 208 |
| 209 pEnd = pStart + size; | |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
210 |
| 1147 | 211 if (particleVerts != 0) |
| 212 delete [] particleVerts; | |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
213 |
| 1147 | 214 // each particle has 4 vertices |
| 215 particleVerts = new vertex[size*4]; | |
| 1096 | 216 } |
| 217 | |
| 218 void ParticleSystem::setEmissionRate(int rate) | |
| 219 { | |
| 220 emissionRate = rate; | |
| 221 } | |
| 222 | |
| 223 void ParticleSystem::setLifetime(float life) | |
| 224 { | |
| 225 this->life = lifetime = life; | |
| 226 } | |
| 227 | |
| 228 void ParticleSystem::setParticleLife(float min, float max) | |
| 229 { | |
| 230 particleLifeMin = min; | |
| 231 if (max == 0) | |
| 232 particleLifeMax = min; | |
| 233 else | |
| 234 particleLifeMax = max; | |
| 235 } | |
| 236 | |
| 237 void ParticleSystem::setPosition(float x, float y) | |
| 238 { | |
| 239 position = love::Vector(x, y); | |
| 240 } | |
| 241 | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
242 |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
243 void ParticleSystem::setAreaSpread(AreaSpreadDistribution distribution, float x, float y) |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
244 { |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
245 areaSpread = love::Vector(x, y); |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
246 areaSpreadDistribution = distribution; |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
247 } |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
248 |
| 1096 | 249 void ParticleSystem::setDirection(float direction) |
| 250 { | |
| 251 this->direction = direction; | |
| 252 } | |
| 253 | |
| 254 void ParticleSystem::setSpread(float spread) | |
| 255 { | |
| 256 this->spread = spread; | |
| 257 } | |
| 258 | |
| 259 void ParticleSystem::setRelativeDirection(bool relative) | |
| 260 { | |
| 261 this->relative = relative; | |
| 262 } | |
| 263 | |
| 264 void ParticleSystem::setSpeed(float speed) | |
| 265 { | |
| 266 speedMin = speedMax = speed; | |
| 267 } | |
| 268 | |
| 269 void ParticleSystem::setSpeed(float min, float max) | |
| 270 { | |
| 271 speedMin = min; | |
| 272 speedMax = max; | |
| 273 } | |
| 274 | |
| 275 void ParticleSystem::setGravity(float gravity) | |
| 276 { | |
| 277 gravityMin = gravityMax = gravity; | |
| 278 } | |
| 279 | |
| 280 void ParticleSystem::setGravity(float min, float max) | |
| 281 { | |
| 282 gravityMin = min; | |
| 283 gravityMax = max; | |
| 284 } | |
| 285 | |
| 286 void ParticleSystem::setRadialAcceleration(float acceleration) | |
| 287 { | |
| 288 radialAccelerationMin = radialAccelerationMax = acceleration; | |
| 289 } | |
| 290 | |
| 291 void ParticleSystem::setRadialAcceleration(float min, float max) | |
| 292 { | |
| 293 radialAccelerationMin = min; | |
| 294 radialAccelerationMax = max; | |
| 295 } | |
| 296 | |
| 297 void ParticleSystem::setTangentialAcceleration(float acceleration) | |
| 298 { | |
| 299 tangentialAccelerationMin = tangentialAccelerationMax = acceleration; | |
| 300 } | |
| 301 | |
| 302 void ParticleSystem::setTangentialAcceleration(float min, float max) | |
| 303 { | |
| 304 tangentialAccelerationMin = min; | |
| 305 tangentialAccelerationMax = max; | |
| 306 } | |
| 307 | |
| 308 void ParticleSystem::setSize(float size) | |
| 309 { | |
| 310 sizes.resize(1); | |
| 311 sizes[0] = size; | |
| 312 } | |
| 313 | |
| 314 void ParticleSystem::setSize(const std::vector<float> &newSizes, float variation) | |
| 315 { | |
| 316 sizes = newSizes; | |
| 317 sizeVariation = variation; | |
| 318 } | |
| 319 | |
| 320 void ParticleSystem::setSizeVariation(float variation) | |
| 321 { | |
| 322 sizeVariation = variation; | |
| 323 } | |
| 324 | |
| 325 void ParticleSystem::setRotation(float rotation) | |
| 326 { | |
| 327 rotationMin = rotationMax = rotation; | |
| 328 } | |
| 329 | |
| 330 void ParticleSystem::setRotation(float min, float max) | |
| 331 { | |
| 332 rotationMin = min; | |
| 333 rotationMax = max; | |
| 334 } | |
| 335 | |
| 336 void ParticleSystem::setSpin(float spin) | |
| 337 { | |
| 338 spinStart = spin; | |
| 339 spinEnd = spin; | |
| 340 } | |
| 341 | |
| 342 void ParticleSystem::setSpin(float start, float end) | |
| 343 { | |
| 344 spinStart = start; | |
| 345 spinEnd = end; | |
| 346 } | |
| 347 | |
| 348 void ParticleSystem::setSpin(float start, float end, float variation) | |
| 349 { | |
| 350 spinStart = start; | |
| 351 spinEnd = end; | |
| 352 spinVariation = variation; | |
| 353 } | |
| 354 | |
| 355 void ParticleSystem::setSpinVariation(float variation) | |
| 356 { | |
| 357 spinVariation = variation; | |
| 358 } | |
| 359 | |
| 360 void ParticleSystem::setColor(const Color &color) | |
| 361 { | |
| 362 colors.resize(1); | |
| 363 colors[0] = colorToFloat(color); | |
| 364 } | |
| 365 | |
| 366 void ParticleSystem::setColor(const std::vector<Color> &newColors) | |
| 367 { | |
| 368 colors.resize(newColors.size()); | |
| 369 for (size_t i = 0; i < newColors.size(); ++i) | |
| 370 colors[i] = colorToFloat(newColors[i]); | |
| 371 } | |
| 372 | |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
373 void ParticleSystem::setQuad(love::graphics::Quad *quad) |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
374 { |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
375 |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
376 for (size_t i = 0; i < quads.size(); i++) |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
377 quads[i]->release(); |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
378 |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
379 quads.resize(1); |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
380 |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
381 quads[0] = quad; |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
382 quad->retain(); |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
383 } |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
384 |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
385 void ParticleSystem::setQuad(const std::vector<love::graphics::Quad *> &newQuads) |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
386 { |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
387 for (size_t i = 0; i < quads.size(); i++) |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
388 quads[i]->release(); |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
389 |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
390 quads.resize(newQuads.size()); |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
391 |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
392 for (size_t i = 0; i < newQuads.size(); i++) |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
393 { |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
394 quads[i] = newQuads[i]; |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
395 quads[i]->retain(); |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
396 } |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
397 } |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
398 |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
399 void ParticleSystem::setQuad() |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
400 { |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
401 for (size_t i = 0; i < quads.size(); i++) |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
402 quads[i]->release(); |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
403 |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
404 quads.resize(0); |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
405 } |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
406 |
| 1096 | 407 void ParticleSystem::setOffset(float x, float y) |
| 408 { | |
| 409 offsetX = x; | |
| 410 offsetY = y; | |
| 411 } | |
| 412 | |
| 413 float ParticleSystem::getX() const | |
| 414 { | |
| 415 return position.getX(); | |
| 416 } | |
| 417 | |
| 418 float ParticleSystem::getY() const | |
| 419 { | |
| 420 return position.getY(); | |
| 421 } | |
| 422 | |
| 423 const love::Vector &ParticleSystem::getPosition() const | |
| 424 { | |
| 425 return position; | |
| 426 } | |
| 427 | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
428 ParticleSystem::AreaSpreadDistribution ParticleSystem::getAreaSpreadDistribution() const |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
429 { |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
430 return areaSpreadDistribution; |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
431 } |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
432 |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
433 const love::Vector &ParticleSystem::getAreaSpreadParameters() const |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
434 { |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
435 return areaSpread; |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
436 } |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
437 |
| 1096 | 438 float ParticleSystem::getDirection() const |
| 439 { | |
| 440 return direction; | |
| 441 } | |
| 442 | |
| 443 float ParticleSystem::getSpread() const | |
| 444 { | |
| 445 return spread; | |
| 446 } | |
| 447 | |
| 448 float ParticleSystem::getOffsetX() const | |
| 449 { | |
| 450 return offsetX; | |
| 451 } | |
| 452 | |
| 453 float ParticleSystem::getOffsetY() const | |
| 454 { | |
| 455 return offsetY; | |
| 456 } | |
| 457 | |
| 458 int ParticleSystem::count() const | |
| 459 { | |
| 460 return (int)(pLast - pStart); | |
| 461 } | |
| 462 | |
| 463 void ParticleSystem::start() | |
| 464 { | |
| 465 active = true; | |
| 466 } | |
| 467 | |
| 468 void ParticleSystem::stop() | |
| 469 { | |
| 470 active = false; | |
| 471 life = lifetime; | |
| 472 emitCounter = 0; | |
| 473 } | |
| 474 | |
| 475 void ParticleSystem::pause() | |
| 476 { | |
| 477 active = false; | |
| 478 } | |
| 479 | |
| 480 void ParticleSystem::reset() | |
| 481 { | |
| 482 pLast = pStart; | |
| 483 life = lifetime; | |
| 484 emitCounter = 0; | |
| 485 } | |
| 486 | |
| 487 bool ParticleSystem::isActive() const | |
| 488 { | |
| 489 return active; | |
| 490 } | |
| 491 | |
| 492 bool ParticleSystem::isEmpty() const | |
| 493 { | |
| 494 return pStart == pLast; | |
| 495 } | |
| 496 | |
| 497 bool ParticleSystem::isFull() const | |
| 498 { | |
| 499 return pLast == pEnd; | |
| 500 } | |
| 501 | |
| 502 void ParticleSystem::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const | |
| 503 { | |
| 504 if (sprite == 0) return; // just in case of failure | |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
505 |
| 1147 | 506 int numParticles = count(); |
| 507 if (numParticles == 0) return; // don't bother if there's nothing to do | |
| 1096 | 508 |
| 509 glPushMatrix(); | |
| 510 glPushAttrib(GL_CURRENT_BIT); | |
| 511 | |
| 1147 | 512 static Matrix t; |
| 1096 | 513 t.setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky); |
| 514 glMultMatrixf((const GLfloat *)t.getElements()); | |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
515 |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
516 const vertex *imageVerts = sprite->getVertices(); |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
517 const vertex *tVerts; |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
518 size_t numQuads = quads.size(); |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
519 size_t quadIndex = 0; |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
520 |
| 1147 | 521 // set the vertex data for each particle (transformation, texcoords, color) |
| 522 for (int i = 0; i < numParticles; i++) | |
| 1096 | 523 { |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
524 particle *p = pStart + i; |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
525 |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
526 if (numQuads > 0) |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
527 { |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
528 // Make sure the quad index is valid |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
529 quadIndex = (p->quadIndex >= numQuads) ? numQuads - 1 : p->quadIndex; |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
530 tVerts = quads[quadIndex]->getVertices(); |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
531 } |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
532 else |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
533 tVerts = imageVerts; |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
534 |
| 1147 | 535 // particle vertices are sprite vertices transformed by particle information |
| 536 t.setTransformation(p->position[0], p->position[1], p->rotation, p->size, p->size, offsetX, offsetY, 0.0f, 0.0f); | |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
537 t.transform(&particleVerts[i*4], &tVerts[0], 4); |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
538 |
| 1147 | 539 // set the texture coordinate and color data for particle vertices |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
540 for (int v = 0; v < 4; v++) |
|
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
541 { |
| 1147 | 542 int vi = (i * 4) + v; // current vertex index for particle |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
543 |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
544 particleVerts[vi].s = tVerts[v].s; |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
545 particleVerts[vi].t = tVerts[v].t; |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
546 |
| 1147 | 547 // particle colors are stored as floats (0-1) but vertex colors are stored as unsigned bytes (0-255) |
| 548 particleVerts[vi].r = p->color.r*255; | |
| 549 particleVerts[vi].g = p->color.g*255; | |
| 550 particleVerts[vi].b = p->color.b*255; | |
| 551 particleVerts[vi].a = p->color.a*255; | |
| 552 } | |
| 1096 | 553 } |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
554 |
| 1147 | 555 sprite->bind(); |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
556 |
| 1147 | 557 glEnableClientState(GL_COLOR_ARRAY); |
| 558 glEnableClientState(GL_VERTEX_ARRAY); | |
| 559 glEnableClientState(GL_TEXTURE_COORD_ARRAY); | |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
560 |
| 1147 | 561 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(vertex), (GLvoid *)&particleVerts[0].r); |
| 562 glVertexPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid *)&particleVerts[0].x); | |
| 563 glTexCoordPointer(2, GL_FLOAT, sizeof(vertex), (GLvoid *)&particleVerts[0].s); | |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
564 |
| 1147 | 565 glDrawArrays(GL_QUADS, 0, numParticles*4); |
|
1180
1e9aa846f624
Removed unnecessary tabs in empty lines, fixed texture filtering when the minification and mipmap filter are the same
Alexander Szpakowski <amaranth72@gmail.com>
parents:
1147
diff
changeset
|
566 |
| 1147 | 567 glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 568 glDisableClientState(GL_VERTEX_ARRAY); | |
| 569 glDisableClientState(GL_COLOR_ARRAY); | |
| 1096 | 570 |
| 571 glPopAttrib(); | |
| 572 glPopMatrix(); | |
| 573 } | |
| 574 | |
| 575 void ParticleSystem::update(float dt) | |
| 576 { | |
| 577 // Traverse all particles and update. | |
| 578 particle *p = pStart; | |
| 579 | |
| 580 // Make some more particles. | |
| 581 if (active) | |
| 582 { | |
| 583 float rate = 1.0f / emissionRate; // the amount of time between each particle emit | |
| 584 emitCounter += dt; | |
| 585 while (emitCounter > rate) | |
| 586 { | |
| 587 add(); | |
| 588 emitCounter -= rate; | |
| 589 } | |
| 590 /*int particles = (int)(emissionRate * dt); | |
| 591 for (int i = 0; i != particles; i++) | |
| 592 add();*/ | |
| 593 | |
| 594 life -= dt; | |
| 595 if (lifetime != -1 && life < 0) | |
| 596 stop(); | |
| 597 } | |
| 598 | |
| 599 while (p != pLast) | |
| 600 { | |
| 601 // Decrease lifespan. | |
| 602 p->life -= dt; | |
| 603 | |
| 604 if (p->life > 0) | |
| 605 { | |
| 606 | |
| 607 // Temp variables. | |
| 608 love::Vector radial, tangential, gravity(0, p->gravity); | |
| 609 love::Vector ppos(p->position[0], p->position[1]); | |
| 610 | |
| 611 // Get vector from particle center to particle. | |
| 612 radial = ppos - p->origin; | |
| 613 radial.normalize(); | |
| 614 tangential = radial; | |
| 615 | |
| 616 // Resize radial acceleration. | |
| 617 radial *= p->radialAcceleration; | |
| 618 | |
| 619 // Calculate tangential acceleration. | |
| 620 { | |
| 621 float a = tangential.getX(); | |
| 622 tangential.setX(-tangential.getY()); | |
| 623 tangential.setY(a); | |
| 624 } | |
| 625 | |
| 626 // Resize tangential. | |
| 627 tangential *= p->tangentialAcceleration; | |
| 628 | |
| 629 // Update position. | |
| 630 p->speed += (radial+tangential+gravity)*dt; | |
| 631 | |
| 632 // Modify position. | |
| 633 ppos += p->speed * dt; | |
| 634 | |
| 635 p->position[0] = ppos.getX(); | |
| 636 p->position[1] = ppos.getY(); | |
| 637 | |
| 638 const float t = 1.0f - p->life / p->lifetime; | |
| 639 | |
| 640 // Rotate. | |
| 641 p->rotation += (p->spinStart * (1.0f - t) + p->spinEnd * t)*dt; | |
| 642 | |
| 643 // Change size according to given intervals: | |
| 644 // i = 0 1 2 3 n-1 | |
| 645 // |-------|-------|------|--- ... ---| | |
| 646 // t = 0 1/(n-1) 3/(n-1) 1 | |
| 647 // | |
| 648 // `s' is the interpolation variable scaled to the current | |
| 649 // interval width, e.g. if n = 5 and t = 0.3, then the current | |
| 650 // indices are 1,2 and s = 0.3 - 0.25 = 0.05 | |
| 651 float s = p->sizeOffset + t * p->sizeIntervalSize; // size variation | |
| 652 s *= (float)(sizes.size() - 1); // 0 <= s < sizes.size() | |
| 653 size_t i = (size_t)s; | |
| 654 size_t k = (i == sizes.size() - 1) ? i : i + 1; // boundary check (prevents failing on t = 1.0f) | |
| 655 s -= (float)i; // transpose s to be in interval [0:1]: i <= s < i + 1 ~> 0 <= s < 1 | |
| 656 p->size = sizes[i] * (1.0f - s) + sizes[k] * s; | |
| 657 | |
| 658 // Update color according to given intervals (as above) | |
| 659 s = t * (float)(colors.size() - 1); | |
| 660 i = (size_t)s; | |
| 661 k = (i == colors.size() - 1) ? i : i + 1; | |
| 662 s -= (float)i; // 0 <= s <= 1 | |
| 663 p->color = colors[i] * (1.0f - s) + colors[k] * s; | |
| 664 | |
|
1352
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
665 // Update quad index |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
666 if (quads.size() > 0) |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
667 { |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
668 k = quads.size() - 1; |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
669 s = t * (float) k; |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
670 i = (s > 0) ? (size_t) s : 0; |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
671 p->quadIndex = (i <= k) ? i : k; |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
672 } |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
673 else |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
674 p->quadIndex = 0; |
|
470a47bebfb3
Added ParticleSystem:setQuads(q1, q2, ...), quads are chosen based on current particle lifetime in a similar style to ParticleSystem:setColors
Alex Szpakowski <slime73@gmail.com>
parents:
1333
diff
changeset
|
675 |
| 1096 | 676 // Next particle. |
| 677 p++; | |
| 678 } | |
| 679 else | |
| 680 { | |
| 681 remove(p); | |
| 682 | |
| 683 if (p >= pLast) | |
| 684 return; | |
| 685 } // else | |
| 686 } // while | |
| 687 } | |
| 688 | |
|
1127
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
689 bool ParticleSystem::getConstant(const char *in, AreaSpreadDistribution &out) |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
690 { |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
691 return distributions.find(in, out); |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
692 } |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
693 |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
694 bool ParticleSystem::getConstant(AreaSpreadDistribution in, const char *&out) |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
695 { |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
696 return distributions.find(in, out); |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
697 } |
|
13e6ca38c28c
Fix issue #437: Area-based particle spawn.
vrld <vrld@vrld.org>
parents:
1096
diff
changeset
|
698 |
| 1096 | 699 } // opengl |
| 700 } // graphics | |
| 701 } // love |
