Skip to content

Commit 02e83a2

Browse files
committed
Particle Emitter
1 parent 9af0f12 commit 02e83a2

4 files changed

Lines changed: 70 additions & 550 deletions

File tree

examples/testbed/box2d_Cantilever_WeldJoint.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
import com.thomasdiewald.liquidfun.java.DwWorld;
1818

19-
import java.util.Objects;
20-
2119
import org.jbox2d.collision.shapes.CircleShape;
2220
import org.jbox2d.collision.shapes.EdgeShape;
2321
import org.jbox2d.collision.shapes.PolygonShape;

examples/testbed/liquidfun_Scene.java renamed to examples/testbed/liquidfun_ParticleEmitter.java

Lines changed: 41 additions & 214 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,23 @@
1515

1616

1717
import com.thomasdiewald.liquidfun.java.DwWorld;
18+
import com.thomasdiewald.liquidfun.java.ParticleEmitter;
1819
import com.thomasdiewald.pixelflow.java.DwPixelFlow;
1920
import com.thomasdiewald.pixelflow.java.imageprocessing.filter.DwLiquidFX;
20-
import com.thomasdiewald.pixelflow.java.sampling.DwSampling;
21-
22-
import java.util.Random;
23-
24-
import org.jbox2d.collision.AABB;
25-
import org.jbox2d.collision.shapes.CircleShape;
2621
import org.jbox2d.collision.shapes.PolygonShape;
27-
import org.jbox2d.common.Color3f;
2822
import org.jbox2d.common.MathUtils;
2923
import org.jbox2d.common.Vec2;
3024
import org.jbox2d.dynamics.Body;
3125
import org.jbox2d.dynamics.BodyDef;
3226
import org.jbox2d.dynamics.BodyType;
33-
import org.jbox2d.dynamics.joints.RevoluteJoint;
3427
import org.jbox2d.dynamics.joints.RevoluteJointDef;
35-
import org.jbox2d.particle.ParticleColor;
36-
import org.jbox2d.particle.ParticleDef;
37-
import org.jbox2d.particle.ParticleGroupDef;
3828
import org.jbox2d.particle.ParticleType;
3929

4030
import processing.core.*;
4131
import processing.opengl.PGraphics2D;
4232

4333

44-
public class liquidfun_Scene extends PApplet {
34+
public class liquidfun_ParticleEmitter extends PApplet {
4535

4636
int viewport_w = 1280;
4737
int viewport_h = 720;
@@ -71,14 +61,11 @@ public void setup(){
7161
surface.setLocation(viewport_x, viewport_y);
7262
// sprite = loadImage("sprite.png");
7363

74-
7564
pixelflow = new DwPixelFlow(this);
7665
liquidfx = new DwLiquidFX(pixelflow);
7766

7867
pg_particles = (PGraphics2D) createGraphics(width, height, P2D);
7968

80-
81-
8269
reset();
8370
// frameRate(1000);
8471
frameRate(120);
@@ -96,6 +83,7 @@ public void reset(){
9683

9784
world = new DwWorld(this, 20);
9885

86+
world.setParticleRadius(0.15f);
9987
// world.particles.param.falloff_exp1 = 3;
10088
// world.particles.param.falloff_exp2 = 1;
10189
// world.particles.param.radius_scale = 2;
@@ -113,12 +101,7 @@ public void reset(){
113101
public void draw(){
114102

115103
if(UPDATE_PHYSICS){
116-
wave();
117-
// if(frameCount % 2 == 0){
118-
for(int i = 0; i < 5; i++){
119-
addParticles();
120-
}
121-
// }
104+
addParticles();
122105
world.update();
123106
}
124107

@@ -146,8 +129,8 @@ public void draw(){
146129

147130
if(APPLY_LIQUID_FX)
148131
{
149-
liquidfx.param.base_LoD = 0;
150-
liquidfx.param.base_blur_radius = 2;
132+
liquidfx.param.base_LoD = 1;
133+
liquidfx.param.base_blur_radius = 1;
151134
liquidfx.param.base_threshold = 0.7f;
152135
liquidfx.param.highlight_enabled = true;
153136
liquidfx.param.highlight_LoD = 1;
@@ -187,26 +170,19 @@ public void keyReleased(){
187170
}
188171

189172

190-
public void mouseReleased(){
191-
Vec2 mworld = new Vec2();
192-
world.transform.getScreen2box(mouseX, mouseY, mworld);
193-
System.out.println("mouse: "+mouseX+", "+mouseY+", "+mworld);
194-
}
173+
// public void mouseReleased(){
174+
// Vec2 mworld = new Vec2();
175+
// world.transform.getScreen2box(mouseX, mouseY, mworld);
176+
// System.out.println("mouse: "+mouseX+", "+mouseY+", "+mworld);
177+
// }
195178

196179

197180
//////////////////////////////////////////////////////////////////////////////
198181
// Scene Setup
199182
//////////////////////////////////////////////////////////////////////////////
200183

201-
RevoluteJoint m_joint;
202-
float m_time;
203-
204-
205-
public void wave() {
206-
// m_time += 1 / 120f;
207-
// m_joint.setMotorSpeed(0.1f * MathUtils.cos(m_time) * MathUtils.PI);
208-
}
209-
184+
185+
210186
public void initScene() {
211187

212188

@@ -219,32 +195,15 @@ public void initScene() {
219195
{
220196
BodyDef bd = new BodyDef();
221197
ground = world.createBody(bd);
222-
223-
// bd.type = BodyType.DYNAMIC;
224-
// bd.allowSleep = false;
225-
// bd.position.set(0.0f, 0.0f);
226-
// Body body = world.createBody(bd);
227198

228-
// PolygonShape shape = new PolygonShape();
229-
// shape.setAsBox(1, 9.5f, new Vec2(19.0f, 0.0f), 0.0f);
230-
// body.createFixture(shape, 5.0f);
231-
// shape.setAsBox(1, 9.5f, new Vec2(-19.0f, 0.0f), 0.0f);
232-
// body.createFixture(shape, 5.0f);
233-
// shape.setAsBox(20.0f, 1, new Vec2(0.0f, 11.0f), 0.0f);
234-
// body.createFixture(shape, 5.0f);
235-
// shape.setAsBox(20.0f, 1, new Vec2(0.0f, -11.0f), 0.0f);
236-
// body.createFixture(shape, 5.0f);
237-
// world.bodies.add(body, true, color(0), true, color(0), 1f);
238-
//
239199
PolygonShape shape = new PolygonShape();
240200
shape.setAsBox(dimx/4, thick/2, new Vec2(0, 4), 10 * PI/180);
241201
ground.createFixture(shape, 0);
242202

243203
shape.setAsBox(dimx/6, thick/2, new Vec2(-20, 8), -15 * PI/180);
244204
ground.createFixture(shape, 0);
245205

246-
247-
world.bodies.add(ground, true, color(160), true, color(0), 1f);
206+
world.bodies.add(ground, true, color(220), true, color(0), 1f);
248207
}
249208

250209

@@ -255,10 +214,10 @@ public void initScene() {
255214
Body body = world.createBody(bd);
256215

257216
PolygonShape shape = new PolygonShape();
258-
shape.setAsBox(0.5f, 5.0f);
217+
shape.setAsBox(0.5f, 7.0f);
259218
body.createFixture(shape, 1);
260219

261-
shape.setAsBox(5, 0.5f);
220+
shape.setAsBox(7, 0.5f);
262221
body.createFixture(shape, 0.5f);
263222

264223
world.bodies.add(body, true, color(220), true, color(0), 1f);
@@ -272,11 +231,10 @@ public void initScene() {
272231
jd.motorSpeed = 0.1f * MathUtils.PI;
273232
jd.maxMotorTorque = 1e7f;
274233
jd.enableMotor = !true;
275-
m_joint = (RevoluteJoint) world.createJoint(jd);
234+
world.createJoint(jd);
276235
}
277236

278-
279-
237+
280238
{
281239
BodyDef bd = new BodyDef();
282240
bd.type = BodyType.DYNAMIC;
@@ -301,185 +259,54 @@ public void initScene() {
301259
jd.motorSpeed = -0.2f * MathUtils.PI;
302260
jd.maxMotorTorque = 1e7f;
303261
jd.enableMotor = !true;
304-
m_joint = (RevoluteJoint) world.createJoint(jd);
262+
world.createJoint(jd);
305263
}
306264

307-
308-
309-
310-
311-
312-
{
313-
BodyDef bd = new BodyDef();
314-
bd.type = BodyType.DYNAMIC;
315-
bd.position.set(-2,3);
316-
Body body = world.createBody(bd);
317-
318-
PolygonShape shape = new PolygonShape();
319-
shape.setAsBox(0.5f, 8.0f);
320-
body.createFixture(shape, 1);
321-
322-
shape.setAsBox(8, 0.5f);
323-
body.createFixture(shape, 0.5f);
324-
325-
world.bodies.add(body, true, color(220), true, color(0), 1f);
326-
327-
RevoluteJointDef jd = new RevoluteJointDef();
328-
jd.bodyA = ground;
329-
jd.bodyB = body;
330-
jd.localAnchorA.set(bd.position);
331-
jd.localAnchorB.set(0.0f, 0.0f);
332-
jd.referenceAngle = 0.0f;
333-
jd.motorSpeed = -0.2f * MathUtils.PI;
334-
jd.maxMotorTorque = 100000;
335-
jd.enableMotor = true;
336-
m_joint = (RevoluteJoint) world.createJoint(jd);
337-
}
338-
339-
340-
341265

342-
343-
world.setParticleRadius(0.15f);
344-
world.setParticleDamping(0.6f);
345266

346-
{
347-
ParticleGroupDef pgroup_def = new ParticleGroupDef();
348-
pgroup_def.flags = ParticleType.b2_waterParticle | ParticleType.b2_viscousParticle;
349-
pgroup_def.setColor(new Color3f(1, 0.15f, 0.05f));
350-
pgroup_def.linearVelocity.set(0,0);
351-
pgroup_def.angularVelocity = 0f;
352-
353-
PolygonShape shape = new PolygonShape();
354-
shape.setAsBox(17.0f, 9.0f, new Vec2(0.0f, dimy/2), 0.0f);
355-
356-
pgroup_def.shape = shape;
357-
world.createParticleGroup(pgroup_def);
358-
}
359-
360-
m_time = 0;
361-
362267
int flags = 0;
363268
flags |= ParticleType.b2_waterParticle;
364269
flags |= ParticleType.b2_viscousParticle;
365270
// flags |= ParticleType.b2_colorMixingParticle;
366271

367-
emitter0 = new ParticleEmitter(world, 800, 100);
368-
emitter0.emit_angle = -170 * TO_RAD;
369-
emitter0.emit_velocity = 15;
370-
emitter0.pdef.color.set(color(32,255,0));
371-
emitter0.pdef.flags = flags;
372-
373-
emitter1 = new ParticleEmitter(world, 100, 100);
374-
emitter1.emit_angle = 0;
375-
emitter1.emit_velocity = 10;
376-
emitter1.pdef.color.set(color(255,16,0));
377-
emitter1.pdef.flags = flags;
378-
379-
emitter2 = new ParticleEmitter(world, 131, 315);
380-
emitter2.emit_angle = +40 * TO_RAD;
381-
emitter2.emit_velocity = 15;
382-
emitter2.pdef.color.set(color(255,64,0));
383-
emitter2.pdef.flags = flags;
272+
emitter0 = new ParticleEmitter(world, world.transform);
273+
emitter1 = new ParticleEmitter(world, world.transform);
274+
emitter2 = new ParticleEmitter(world, world.transform);
275+
emitter3 = new ParticleEmitter(world, world.transform);
276+
277+
emitter0.setInScreen( 131, 315, 300, 40, color(255,64,0), flags);
278+
emitter1.setInScreen( 100, 100, 300, -10, color(255,16,0), flags);
279+
emitter2.setInScreen(1000, 100, 300, -175, color(32,255,0), flags);
280+
emitter3.setInScreen(1200, 400, 300, 140, color(0,64,255), flags);
384281
}
385282

386283

387284
ParticleEmitter emitter0;
388285
ParticleEmitter emitter1;
389286
ParticleEmitter emitter2;
390-
287+
ParticleEmitter emitter3;
288+
391289
int counter = 0;
392290
public void addParticles(){
393-
if(counter % 2 == 0){
394-
emitter0.emitParticles(1);
395-
emitter1.emitParticles(1);
396-
emitter2.emitParticles(1);
397-
}
398-
counter++;
399-
}
400-
401-
402-
static final float TO_RAD = (float) (Math.PI / 180.0);
403-
404-
405-
406-
407-
static class ParticleEmitter {
408-
public PApplet papplet;
409-
public DwWorld world;
410-
411-
public ParticleDef pdef = new ParticleDef();
412-
413-
public float emit_angle = (float) (Math.PI * 0.5f);
414-
public float emit_velocity = 20;
415-
416-
public float mult_jitter_radius = 1.0f; // radius, world-space
417-
public float mult_jitter_velmag = 0.5f; // noise mult
418-
public float mult_jitter_velrot = 0.2f; // [0, PI*2]
419-
420-
421-
protected Random rand = new Random();
422-
protected int counter = 0;
423291

424-
protected Vec2 emit_pos_screen = new Vec2();
425-
protected Vec2 emit_pos_world = new Vec2();
292+
emitter2.emit_vel = 25 * (sin(counter/200f) * 0.5f + 0.5f);
293+
emitter3.emit_vel = 25 * (sin(counter/200f) * 0.5f + 0.5f);
426294

427-
428-
public ParticleEmitter(DwWorld world, float screen_x, float screen_y){
429-
this.papplet = world.papplet;
430-
this.world = world;
431-
432-
pdef.flags = ParticleType.b2_waterParticle | ParticleType.b2_viscousParticle;
433-
pdef.setColor(new Color3f(1, 0.15f, 0.05f));
434-
435-
setPosition(screen_x, screen_y);
436-
}
437-
438-
439-
public void emitParticles(int count){
440-
for(int i = 0; i < count; i++){
441-
emitSingleParticle(i);
442-
}
443-
}
444-
445-
public void setPosition(float screen_x, float screen_y){
446-
world.transform.getScreen2box(screen_x, screen_y, emit_pos_world);
447-
}
448-
449-
protected void emitSingleParticle(int idx){
450-
451-
// velocity (noise)
452-
float srandnoise = papplet.noise(counter / 100f) * 2 - 1;
453-
454-
float rot_angle = emit_angle + srandnoise * mult_jitter_velrot;
455-
float vel_mag = emit_velocity + srandnoise * mult_jitter_velmag;
456-
457-
float vel_x = (float) (Math.cos(rot_angle) * vel_mag);
458-
float vel_y = (float) (Math.sin(rot_angle) * vel_mag);
459-
460-
// position
461-
float[] jitter = DwSampling.sampleDisk_Halton(counter, 0.5f);
462-
463-
float pos_x = emit_pos_world.x + jitter[0] * mult_jitter_radius;
464-
float pos_y = emit_pos_world.y + jitter[1] * mult_jitter_radius;
465-
466-
467-
// create Particle
468-
pdef.position.set(pos_x, pos_y);
469-
pdef.velocity.set(vel_x, vel_y);
470-
world.createParticle(pdef);
471-
472-
counter++;
295+
if(counter % 1 == 0)
296+
{
297+
emitter0.emitParticles(2);
298+
emitter1.emitParticles(2);
299+
emitter2.emitParticles(2);
300+
emitter3.emitParticles(2);
473301
}
474-
475-
302+
counter++;
476303
}
477304

478-
479305

480306

307+
481308
public static void main(String args[]) {
482-
PApplet.main(new String[] { liquidfun_Scene.class.getName() });
309+
PApplet.main(new String[] { liquidfun_ParticleEmitter.class.getName() });
483310
}
484311

485312
}

0 commit comments

Comments
 (0)