Skip to content

Commit d7bd14b

Browse files
committed
Working through some velocity tests
1 parent 053eea0 commit d7bd14b

2 files changed

Lines changed: 105 additions & 39 deletions

File tree

v3/src/gameobjects/particles/Particle.js

Lines changed: 94 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var Clamp = require('../../math/Clamp');
12
var Class = require('../../utils/Class');
23
var DegToRad = require('../../math/DegToRad');
34

@@ -23,18 +24,32 @@ var Particle = new Class({
2324
this.scaleX = 1;
2425
this.scaleY = 1;
2526

27+
this.angle = 0;
28+
29+
this.alpha = 1;
30+
31+
// rads
2632
this.rotation = 0;
2733

2834
this.scrollFactorX = 1;
2935
this.scrollFactorY = 1;
3036

31-
this.color = 0xFFFFFFFF;
37+
this.color = 0xffffffff;
38+
39+
// in ms
40+
this.life = 1000;
41+
this.lifeCurrent = 1000;
3242

33-
// Floats?
34-
this.life = 1;
35-
this.lifeStep = 1;
36-
this.normLifeStep = 1;
43+
// ease data
44+
this.data = {
45+
tint: { min: 0xffffff, max: 0xffffff, current: 0xffffff },
46+
alpha: { min: 1, max: 1 },
47+
angle: { min: 0, max: 0 },
48+
scaleX: { min: 1, max: 1 },
49+
scaleY: { min: 1, max: 1 }
50+
};
3751

52+
/*
3853
this.start = {
3954
tint: 0xFFFFFF,
4055
alpha: 1,
@@ -48,6 +63,7 @@ var Particle = new Class({
4863
scale: { x: 1, y: 1 },
4964
angle: 0
5065
};
66+
*/
5167
},
5268

5369
reset: function (x, y, frame)
@@ -62,17 +78,21 @@ var Particle = new Class({
6278
this.velocityX = 0;
6379
this.velocityY = 0;
6480

65-
this.scaleX = 1;
66-
this.scaleY = 1;
81+
this.life = 1000;
82+
this.lifeCurrent = 1000;
6783

68-
this.rotation = 0;
84+
// this.scaleX = 1;
85+
// this.scaleY = 1;
86+
87+
// this.rotation = 0;
6988

70-
this.color = 0xFFFFFFFF;
89+
// this.color = 0xFFFFFFFF;
7190

72-
this.life = 1;
73-
this.lifeStep = 1;
74-
this.normLifeStep = 1;
91+
// this.life = 1;
92+
// this.lifeStep = 1;
93+
// this.normLifeStep = 1;
7594

95+
/*
7696
var start = this.start;
7797
7898
start.tint = 0xFFFFFF;
@@ -88,13 +108,14 @@ var Particle = new Class({
88108
end.scale.x = 1;
89109
end.scale.y = 1;
90110
end.angle = 0;
111+
*/
91112

92113
return this;
93114
},
94115

95116
isAlive: function ()
96117
{
97-
return (this.lifeStep > 0);
118+
return (this.lifeCurrent > 0);
98119
},
99120

100121
// var rad = DegToRad(Between(this.minEmitAngle, this.maxEmitAngle));
@@ -117,26 +138,63 @@ var Particle = new Class({
117138
// particle.color = (particle.color & 0x00FFFFFF) | (((this.startAlpha * 0xFF)|0) << 24);
118139
// particle.index = this.alive.length;
119140

141+
// this.data = {
142+
// tint: { min: 0xffffff, max: 0xffffff, current: 0xffffff },
143+
// alpha: { min: 1, max: 1, current: 1 },
144+
// angle: { min: 0, max: 0, current: 0 },
145+
// scaleX: { min: 1, max: 1, current: 1 },
146+
// scaleY: { min: 1, max: 1, current: 1 }
147+
// };
148+
120149
emit: function (emitter)
121150
{
151+
// var rad = DegToRad(emitter.angle.getRandom());
152+
153+
// this.velocityX = Math.cos(rad) * emitter.velocity.getRandomX();
154+
// this.velocityY = Math.sin(rad) * emitter.velocity.getRandomY();
155+
156+
this.velocityX = emitter.velocity.getRandomX();
157+
this.velocityY = emitter.velocity.getRandomY();
158+
122159
var rad = DegToRad(emitter.angle.getRandom());
123160

124-
this.velocityX = Math.cos(rad) * emitter.velocity.getRandomX();
125-
this.velocityY = Math.sin(rad) * emitter.velocity.getRandomY();
161+
if (rad !== 0)
162+
{
163+
this.velocityX *= Math.cos(rad);
164+
this.velocityY *= Math.sin(rad);
165+
}
126166

127-
this.life = Math.max(this.life, Number.MIN_VALUE);
128-
this.lifeStep = this.life;
167+
this.life = emitter.lifespan.getRandom();
168+
this.lifeCurrent = this.life;
129169

130-
emitter.scale.copyX(this.start.scale);
131-
emitter.scale.copyY(this.end.scale);
170+
// eased values
132171

133-
this.start.alpha = emitter.alpha.min;
134-
this.end.alpha = emitter.alpha.max;
172+
// emitter.scale.copyXToMinMax(this.data.scaleX);
173+
// emitter.scale.copyYToMinMax(this.data.scaleY);
135174

136-
this.start.rotation = emitter.particleAngle.min;
137-
this.end.rotation = emitter.particleAngle.max;
175+
// emitter.particleAngle.copyToMinMax(this.data.angle);
138176

139-
this.color = (this.color & 0x00ffffff) | (((this.start.alpha * 0xff) | 0) << 24);
177+
// emitter.alpha.copyToMinMax(this.data.alpha);
178+
179+
180+
181+
// this.scaleX = emitter.scale.xMin;
182+
// this.scaleY = emitter.scale.yMin;
183+
184+
// this.rotation = DegToRad(emitter.particleAngle.min);
185+
186+
// Set the starting ease data
187+
188+
// emitter.scale.copyX(this.start.scale);
189+
// emitter.scale.copyY(this.end.scale);
190+
191+
// this.start.alpha = emitter.alpha.min;
192+
// this.end.alpha = emitter.alpha.max;
193+
194+
// this.start.rotation = emitter.particleAngle.min;
195+
// this.end.rotation = emitter.particleAngle.max;
196+
197+
// this.color = (this.color & 0x00ffffff) | (((this.start.alpha * 0xff) | 0) << 24);
140198

141199
this.index = emitter.alive.length;
142200
},
@@ -159,19 +217,27 @@ var Particle = new Class({
159217
// particle.color = (particle.color & 0x00FFFFFF) | (((alphaf * 0xFF)|0) << 24);
160218
// particle.rotation = rotation;
161219

162-
update: function (emitter, step)
220+
// delta = ms, step = delta / 1000
221+
update: function (emitter, delta, step)
163222
{
164-
this.normLifeStep = 1 - this.lifeStep / this.life;
223+
// How far along in life is this particle? (t = 0 to 1)
224+
// var t = this.lifeCurrent / this.life, 0, 1;
165225

166226
this.velocityX += (emitter.gravity.x * step);
167227
this.velocityY += (emitter.gravity.y * step);
168228

169229
this.x += this.velocityX * step;
170230
this.y += this.velocityY * step;
171231

172-
this.lifeStep -= step;
232+
// scale
233+
234+
// rotation
235+
236+
// alpha
237+
238+
this.lifeCurrent -= delta;
173239

174-
return (this.lifeStep <= 0);
240+
return (this.lifeCurrent <= 0);
175241
}
176242

177243
});

v3/src/gameobjects/particles/ParticleEmitter.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ var ParticleEmitter = new Class({
4545

4646
this.scale = new MinMax4(1);
4747

48-
this.gravity = new MinMax4();
48+
this.gravity = new MinMax2();
4949

5050
this.alpha = new MinMax2(1);
5151

52-
this.angle = new MinMax2(0, 360);
52+
this.angle = new MinMax2();
5353

5454
this.particleAngle = new MinMax2();
5555

@@ -165,9 +165,9 @@ var ParticleEmitter = new Class({
165165
return this;
166166
},
167167

168-
setGravity: function (xMin, xMax, yMin, yMax)
168+
setGravity: function (x, y)
169169
{
170-
this.gravity.set(xMin, xMax, yMin, yMax);
170+
this.gravity.set(x, y);
171171

172172
return this;
173173
},
@@ -359,7 +359,7 @@ var ParticleEmitter = new Class({
359359
var y = this.y;
360360
// var shape = this.emitShape;
361361
var dead = this.dead;
362-
// var allowCreation = this.allowCreation;
362+
var allowCreation = true;
363363

364364
for (var index = 0; index < count; index++)
365365
{
@@ -428,7 +428,7 @@ var ParticleEmitter = new Class({
428428
var particle = particles[index];
429429

430430
// update returns `true` if the particle is now dead (lifeStep < 0)
431-
if (particle.update(this, step))
431+
if (particle.update(this, delta, step))
432432
{
433433
// Moves the dead particle to the end of the particles array (ready for splicing out later)
434434
var last = particles[length - 1];
@@ -486,13 +486,13 @@ var ParticleEmitter = new Class({
486486
StableSort(particles, this.indexSort);
487487
}
488488

489-
this.delayCounter -= emitterStep;
489+
// this.delayCounter -= emitterStep;
490490

491-
if (this.delayCounter <= 0 && this.enabled)
492-
{
491+
// if (this.delayCounter <= 0 && this.enabled)
492+
// {
493493
this.emit(this.emitCount);
494-
this.delayCounter = this.delay / 1000;
495-
}
494+
// this.delayCounter = this.delay / 1000;
495+
// }
496496
},
497497

498498
indexSort: function (a, b)

0 commit comments

Comments
 (0)