-
Notifications
You must be signed in to change notification settings - Fork 0
/
TubeD.h
634 lines (558 loc) · 19.3 KB
/
TubeD.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//
// TubeD.h
// Box2D
//
// Created by Michael J Laielli on 8/31/14.
//
//
#ifndef Box2D_TubeD_h
#define Box2D_TubeD_h
#define START_V 4.5f
#define NUM_EMITTERS 10
#include "ParticleEmitter.h"
// Emit particles from a circular region.
class DirectionalEmitter {
public:
// Initialize a particle emitter.
DirectionalEmitter() :
m_particleSystem(NULL), m_callback(NULL), m_speed(0.0f),
m_emitRate(1.0f), m_emitRemainder(0.0f), m_flags(b2_waterParticle),
m_group(NULL)
{
}
~DirectionalEmitter()
{
SetGroup(NULL);
}
// Set the center of the emitter.
void SetPosition(const b2Vec2& origin)
{
m_origin = origin;
}
// Get the center of the emitter.
const b2Vec2& GetPosition() const
{
return m_origin;
}
// Set the size of the circle which emits particles.
void SetSize(const b2Vec2& size)
{
m_halfSize = size * 0.5f;
}
// Get the size of the circle which emits particles.
b2Vec2 GetSize() const
{
return m_halfSize * 2.0f;
}
// Set the starting velocity of emitted particles.
void SetVelocity(const b2Vec2& velocity)
{
m_startingVelocity = velocity;
}
// Get the starting velocity.
const b2Vec2& GetVelocity() const
{
return m_startingVelocity;
}
// Set the speed of particles along the direction from the center of
// the emitter.
void SetSpeed(const float32 speed)
{
m_speed = speed;
}
// Get the speed of particles along the direction from the center of
// the emitter.
float32 GetSpeed() const
{
return m_speed;
}
// Set the flags for created particles.
void SetParticleFlags(uint32 flags)
{
m_flags = flags;
}
// Get the flags for created particles.
uint32 GetParticleFlags() const
{
return m_flags;
}
// Set the color of particles.
void SetColor(const b2ParticleColor& color)
{
m_color = color;
}
// Get the color of particles emitter.
const b2ParticleColor& GetColor() const
{
return m_color;
}
// Set the emit rate in particles per second.
void SetEmitRate(const float32 emitRate)
{
m_emitRate = emitRate;
}
// Get the current emit rate.
float32 GetEmitRate() const
{
return m_emitRate;
}
// Set the particle system this emitter is adding particles to.
void SetParticleSystem(b2ParticleSystem * const particleSystem)
{
m_particleSystem = particleSystem;
}
// Get the particle system this emitter is adding particle to.
b2ParticleSystem* GetParticleSystem() const
{
return m_particleSystem;
}
// Set the callback that is called on the creation of each particle.
void SetCallback(EmittedParticleCallback* const callback)
{
m_callback = callback;
}
// Get the callback that is called on the creation of each particle.
EmittedParticleCallback* GetCallback() const
{
return m_callback;
}
// This class sets the group flags to b2_particleGroupCanBeEmpty so that
// it isn't destroyed and clears the b2_particleGroupCanBeEmpty on the
// group when the emitter no longer references it so that the group
// can potentially be cleaned up.
void SetGroup(b2ParticleGroup * const group)
{
if (m_group)
{
m_group->SetGroupFlags(m_group->GetGroupFlags() &
~b2_particleGroupCanBeEmpty);
}
m_group = group;
if (m_group)
{
m_group->SetGroupFlags(m_group->GetGroupFlags() |
b2_particleGroupCanBeEmpty);
}
}
// Get the group particles should be created within.
b2ParticleGroup* GetGroup() const
{
return m_group;
}
// dt is seconds that have passed, particleIndices is an optional pointer
// to an array which tracks which particles have been created and
// particleIndicesCount is the size of the particleIndices array.
// This function returns the number of particles created during this
// simulation step.
int32 Step(const float32 dt, int32* const particleIndices,
const int32 particleIndicesCount)
{
b2Assert(m_particleSystem);
int32 numberOfParticlesCreated = 0;
// How many (fractional) particles should we have emitted this frame?
m_emitRemainder += m_emitRate * dt;
b2ParticleDef pd;
pd.color = m_color;
pd.flags = m_flags;
pd.group = m_group;
// Keep emitting particles on this frame until we only have a
// fractional particle left.
while (m_emitRemainder > 1.0f) {
m_emitRemainder -= 1.0f;
// Randomly pick a position within the emitter's radius.
// const float32 angle = Random() * 2.0f * b2_pi;
// Set direction
// const float32 angle = 0.5f * b2_pi;
// Distance from the center of the circle.
// const float32 distance = Random();
// b2Vec2 positionOnUnitCircle(sin(angle), cos(angle));
// Initial position.
pd.position.Set(
m_origin.x,
m_origin.y);
// Send it flying
pd.velocity = m_startingVelocity;
// if (m_speed != 0.0f)
// {
// pd.velocity += positionOnUnitCircle * m_speed;
// }
const int32 particleIndex = m_particleSystem->CreateParticle(pd);
if (m_callback)
{
m_callback->ParticleCreated(m_particleSystem, particleIndex);
}
if (particleIndices &&
numberOfParticlesCreated < particleIndicesCount)
{
particleIndices[numberOfParticlesCreated] = particleIndex;
}
++numberOfParticlesCreated;
}
return numberOfParticlesCreated;
}
private:
// Calculate a random number 0.0f..1.0f.
static float32 Random()
{
return ((float32)rand() / (float32)RAND_MAX);
}
private:
// Pointer to global world
b2ParticleSystem *m_particleSystem;
// Called for each created particle.
EmittedParticleCallback *m_callback;
// Center of particle emitter
b2Vec2 m_origin;
// Launch direction.
b2Vec2 m_startingVelocity;
// Speed particles are emitted
float32 m_speed;
// Half width / height of particle emitter
b2Vec2 m_halfSize;
// Particles per second
float32 m_emitRate;
// Initial color of particle emitted.
b2ParticleColor m_color;
// Number particles to emit on the next frame
float32 m_emitRemainder;
// Flags for created particles, see b2ParticleFlag.
uint32 m_flags;
// Group to put newly created particles in.
b2ParticleGroup* m_group;
};
// Faucet test creates a container from boxes and continually spawning
// particles with finite lifetimes that pour into the box.
class TubeD : public Test
{
private:
// Assigns a random lifetime to each created particle.
class ParticleLifetimeWave : public EmittedParticleCallback
{
public:
// Initialize the randomizer to set lifetimes between minLifetime to
// maxLifetime.
ParticleLifetimeWave() { }
virtual ~ParticleLifetimeWave() { }
// Called for each created particle.
virtual void ParticleCreated(b2ParticleSystem * const system,
const int32 particleIndex)
{
system->SetParticleLifetime(
particleIndex, (1.8f));
}
private:
// float32 m_minLifetime;
// float32 m_maxLifetime;
};
public:
// Construct the world.
TubeD() :
m_particleColorOffset(0.0f)
// m_lifetimeRandomizer(k_particleLifetimeMin, k_particleLifetimeMax)
{
// Configure particle system parameters.
m_particleSystem->SetRadius(0.035f);
m_particleSystem->SetMaxParticleCount(k_maxParticleCount);
m_particleSystem->SetDestructionByAge(true);
// Ground body
{
b2BodyDef bd;
b2Body* ground = m_world->CreateBody(&bd);
float32 x1 = 0.0f;
float32 y1 = -sqrtf(2-x1*x1) + 1.4f;
for (int32 i = 0; i < 56; ++i)
{
float32 x2 = x1 + 0.025f;
float32 y2 = -(sqrtf(2-x1*x1)) + 1.4f;
b2EdgeShape shape;
shape.Set(b2Vec2(x1, y1), b2Vec2(x2, y2));
ground->CreateFixture(&shape, 0.0f);
x1 = x2;
y1 = y2;
}
}
{
b2BodyDef bd;
bd.type = b2_dynamicBody;
b2Body* body = m_world->CreateBody(&bd);
b2CircleShape shape;
shape.m_p.Set(0.5f, 1);
shape.m_radius = 0.2f;
body->CreateFixture(&shape, 0.3f);
}
// b2Body* ground = NULL;
// {
// b2BodyDef bd;
// ground = m_world->CreateBody(&bd);
// }
//
// // Create the container / trough style sink.
// {
// b2PolygonShape shape;
// const float32 height = k_containerHeight + k_containerThickness;
//// shape.SetAsBox(k_containerWidth - k_containerThickness,
//// k_containerThickness, b2Vec2(0.0f, 0.0f), 0.0f);
//// ground->CreateFixture(&shape, 0.0f);
//// shape.SetAsBox(k_containerThickness, height,
//// b2Vec2(-k_containerWidth, k_containerHeight), 0.0f);
//// ground->CreateFixture(&shape, 0.0f);
// shape.SetAsBox(k_containerThickness, height,
// b2Vec2(k_containerWidth*2, k_containerHeight), 0.0f);
// ground->CreateFixture(&shape, 0.0f);
// }
// Create ground under the container to catch overflow.
// {
// b2PolygonShape shape;
// shape.SetAsBox(k_containerWidth * 5.0f, k_containerThickness,
// b2Vec2(0.0f, k_containerThickness * -2.0f), 0.0f);
// ground->CreateFixture(&shape, 0.0f);
// }
// Create the faucet spout.
// {
// b2PolygonShape shape;
// const float32 particleDiameter =
// m_particleSystem->GetRadius() * 2.0f;
// const float32 faucetLength = k_faucetLength * particleDiameter;
// // Dimensions of the faucet in world units.
// const float32 length = faucetLength * k_spoutLength;
// const float32 width = k_containerWidth * k_faucetWidth *
// k_spoutWidth;
// // Height from the bottom of the container.
// const float32 height = (k_containerHeight * k_faucetHeight) +
// (length * 0.5f);
//
// shape.SetAsBox(particleDiameter, length,
// b2Vec2(-width, height), 0.0f);
// ground->CreateFixture(&shape, 0.0f);
// shape.SetAsBox(particleDiameter, length,
// b2Vec2(width, height), 0.0f);
// ground->CreateFixture(&shape, 0.0f);
// shape.SetAsBox(width - particleDiameter, particleDiameter,
// b2Vec2(0.0f, height + length -
// particleDiameter), 0.0f);
// ground->CreateFixture(&shape, 0.0f);
// }
// Initialize the particle emitter.
{
float32 spacing = 0.05f;
float32 xpos = spacing;
const float32 faucetLength =
m_particleSystem->GetRadius() * 2.0f * k_faucetLength;
for(int n=0; n<NUM_EMITTERS; ++n)
{ xpos = spacing*float32(n+1);
m_emitter[n].SetParticleSystem(m_particleSystem);
m_emitter[n].SetCallback(&m_lifetimeRandomizer);
// m_emitter[n].SetPosition(b2Vec2(k_containerWidth * k_faucetWidth,
// k_containerHeight * k_faucetHeight +
// (faucetLength * 0.5f)));
m_emitter[n].SetPosition(b2Vec2(0.0f, xpos));
m_emitter[n].SetVelocity(b2Vec2(START_V, 0.0f));
m_emitter[n].SetSize(b2Vec2(0.0f, faucetLength));
m_emitter[n].SetColor(b2ParticleColor(50, 50, 50, 255));
m_emitter[n].SetEmitRate(50.0f);
m_emitter[n].SetParticleFlags(TestMain::GetParticleParameterValue());
}
}
// Don't restart the test when changing particle types.
TestMain::SetRestartOnParticleParameterChange(false);
// Limit the set of particle types.
TestMain::SetParticleParameters(k_paramDef, k_paramDefCount);
}
// Run a simulation step.
void Step(Settings* settings)
{
const float32 dt = 1.0f / settings->hz;
Test::Step(settings);
// const b2ParticleContact* contacts = m_particleSystem->GetContacts();
// const int32 num_contacts = m_particleSystem->GetContactCount();
// b2ParticleColor* colors = m_particleSystem->GetColorBuffer();
//// b2Vec2* velocities = m_particleSystem->GetVelocityBuffer();
//
// for( int i=0; i<num_contacts; ++i)
// {
// float32 w = contacts[i].GetWeight();
//
// //calc impact
//
// colors[contacts[i].GetIndexA()] *= 1.0f+w ;
// colors[contacts[i].GetIndexB()] *= 1.0f+w ;
// }
m_particleColorOffset += dt;
// Keep m_particleColorOffset in the range 0.0f..k_ParticleColorsCount.
if (m_particleColorOffset >= (float32)k_ParticleColorsCount)
{
m_particleColorOffset -= (float32)k_ParticleColorsCount;
}
// Propagate the currently selected particle flags.
for(int n=0; n<NUM_EMITTERS; ++n)
{
m_emitter[n].SetParticleFlags(TestMain::GetParticleParameterValue());
}
// If this is a color mixing particle, add some color.
b2ParticleColor color(255, 255, 255, 255);
if (m_emitter[0].GetParticleFlags() & b2_colorMixingParticle)
{
// Each second, select a different color.
m_emitter[0].SetColor(k_ParticleColors[(int32)m_particleColorOffset %
k_ParticleColorsCount]);
}
else
{
for(int n=0; n<NUM_EMITTERS; ++n)
{
m_emitter[n].SetColor(b2ParticleColor(0, 50, 255, 255));
}
}
// Create the particles.
for(int nEm=0; nEm<NUM_EMITTERS; ++nEm)
{
m_emitter[nEm].Step(dt, NULL, 0);
}
static const char* k_keys[] = {
"Keys: (w) water, (q) powder",
" (t) tensile, (v) viscous",
" (c) color mixing, (s) static pressure",
" (+) increase flow, (-) decrease flow",
};
for (uint32 i = 0; i < B2_ARRAY_SIZE(k_keys); ++i)
{
m_debugDraw.DrawString(5, m_textLine, k_keys[i]);
m_textLine += DRAW_STRING_NEW_LINE;
}
}
// Allows you to set particle flags on devices with keyboards
void Keyboard(unsigned char key)
{
uint32 parameter = 0;
switch (key)
{
case 'w':
parameter = b2_waterParticle;
break;
case 'q':
parameter = b2_powderParticle;
break;
case 't':
parameter = b2_tensileParticle;
break;
case 'v':
parameter = b2_viscousParticle;
break;
case 'c':
parameter = b2_colorMixingParticle;
break;
case 's':
parameter = b2_staticPressureParticle;
break;
case '+':
{
float32 emitRate = m_emitter[0].GetEmitRate();
emitRate *= k_emitRateChangeFactor;
emitRate = b2Max(emitRate, k_emitRateMin);
m_emitter[0].SetEmitRate(emitRate);
break;
}
case '-':
{
float32 emitRate = m_emitter[0].GetEmitRate();
emitRate *= 1.0f / k_emitRateChangeFactor;
emitRate = b2Min(emitRate, k_emitRateMax);
m_emitter[0].SetEmitRate(emitRate);
}
break;
default:
// Nothing.
return;
}
TestMain::SetParticleParameterValue(parameter);
}
float32 GetDefaultViewZoom() const
{
return 0.1f;
}
// Create the faucet test.
static Test* Create()
{
return new TubeD;
}
private:
// Used to cycle through particle colors.
float32 m_particleColorOffset;
// Particle emitters.
DirectionalEmitter m_emitter[NUM_EMITTERS];
// Callback which sets the lifetime of emitted particles.
ParticleLifetimeWave m_lifetimeRandomizer;
private:
// Minimum lifetime of particles in seconds.
static const float32 k_particleLifetimeMin;
// Maximum lifetime of particles in seconds.
static const float32 k_particleLifetimeMax;
// Height of the container.
static const float32 k_containerHeight;
// Width of the container.
static const float32 k_containerWidth;
// Thickness of the container's walls and bottom.
static const float32 k_containerThickness;
// Width of the faucet relative to the container width.
static const float32 k_faucetWidth;
// Height of the faucet relative to the base as a fraction of the
// container height.
static const float32 k_faucetHeight;
// Length of the faucet as a fraction of the particle diameter.
static const float32 k_faucetLength;
// Spout height as a fraction of the faucet length. This should be
// greater than 1.0f).
static const float32 k_spoutLength;
// Spout width as a fraction of the *faucet* width. This should be greater
// than 1.0f).
static const float32 k_spoutWidth;
// Maximum number of particles in the system.
static const int32 k_maxParticleCount;
// Factor that is used to increase / decrease the emit rate.
// This should be greater than 1.0f.
static const float32 k_emitRateChangeFactor;
// Minimum emit rate of the faucet in particles per second.
static const float32 k_emitRateMin;
// Maximum emit rate of the faucet in particles per second.
static const float32 k_emitRateMax;
// Selection of particle types for this test.
static const ParticleParameter::Value k_paramValues[];
static const ParticleParameter::Definition k_paramDef[];
static const uint32 k_paramDefCount;
void changeColorsAccordingToContactWeight( const b2ParticleContact* tacts )
{
}
};
const float32 TubeD::k_particleLifetimeMin = 30.0f;
const float32 TubeD::k_particleLifetimeMax = 50.0f;
const float32 TubeD::k_containerHeight = 0.2f;
const float32 TubeD::k_containerWidth = 1.0f;
const float32 TubeD::k_containerThickness = 0.05f;
const float32 TubeD::k_faucetWidth = 0.1f;
const float32 TubeD::k_faucetHeight = 15.0f;
const float32 TubeD::k_faucetLength = 2.0f;
const float32 TubeD::k_spoutWidth = 1.1f;
const float32 TubeD::k_spoutLength = 2.0f;
const int32 TubeD::k_maxParticleCount = 1000;
const float32 TubeD::k_emitRateChangeFactor = 1.05f;
const float32 TubeD::k_emitRateMin = 1.0f;
const float32 TubeD::k_emitRateMax = 240.0f;
const ParticleParameter::Value TubeD::k_paramValues[] =
{
{ b2_waterParticle, ParticleParameter::k_DefaultOptions, "water" },
{ b2_waterParticle, ParticleParameter::k_DefaultOptions |
ParticleParameter::OptionStrictContacts, "water (strict)" },
{ b2_viscousParticle, ParticleParameter::k_DefaultOptions, "viscous" },
{ b2_powderParticle, ParticleParameter::k_DefaultOptions, "powder" },
{ b2_tensileParticle, ParticleParameter::k_DefaultOptions, "tensile" },
{ b2_colorMixingParticle, ParticleParameter::k_DefaultOptions,
"color mixing" },
{ b2_staticPressureParticle, ParticleParameter::k_DefaultOptions,
"static pressure" },
};
const ParticleParameter::Definition TubeD::k_paramDef[] =
{
{ TubeD::k_paramValues, B2_ARRAY_SIZE(TubeD::k_paramValues) },
};
const uint32 TubeD::k_paramDefCount = B2_ARRAY_SIZE(TubeD::k_paramDef);
#endif