Skip to content

Commit 9af0f12

Browse files
committed
.
1 parent 766ded6 commit 9af0f12

5 files changed

Lines changed: 500 additions & 55 deletions

File tree

examples/testbed/box2d_CantileverJoint.java renamed to examples/testbed/box2d_Cantilever_WeldJoint.java

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

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

19+
import java.util.Objects;
20+
1921
import org.jbox2d.collision.shapes.CircleShape;
2022
import org.jbox2d.collision.shapes.EdgeShape;
2123
import org.jbox2d.collision.shapes.PolygonShape;
@@ -30,7 +32,7 @@
3032
import processing.opengl.PGraphics2D;
3133

3234

33-
public class box2d_CantileverJoint extends PApplet {
35+
public class box2d_Cantilever_WeldJoint extends PApplet {
3436

3537
int viewport_w = 1280;
3638
int viewport_h = 720;
@@ -151,6 +153,7 @@ public void initScene() {
151153
Vec2 anchor = new Vec2(-15.0f + 1.0f * i, 5.0f);
152154
jd.initialize(prevBody, body, anchor);
153155
world.createJoint(jd);
156+
154157

155158
prevBody = body;
156159
}
@@ -276,13 +279,15 @@ public void initScene() {
276279
Body body = world.createBody(bd);
277280
body.createFixture(fd);
278281
}
282+
283+
279284
world.bodies.addAll();
280285
}
281286

282287

283288

284289
public static void main(String args[]) {
285-
PApplet.main(new String[] { box2d_CantileverJoint.class.getName() });
290+
PApplet.main(new String[] { box2d_Cantilever_WeldJoint.class.getName() });
286291
}
287292

288293
}

examples/testbed/box2d_Chain.java renamed to examples/testbed/box2d_Chain_RevoluteJoint.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import processing.opengl.PGraphics2D;
3030

3131

32-
public class box2d_Chain extends PApplet {
32+
public class box2d_Chain_RevoluteJoint extends PApplet {
3333

3434
int viewport_w = 1280;
3535
int viewport_h = 720;
@@ -171,7 +171,7 @@ public void initScene() {
171171

172172

173173
public static void main(String args[]) {
174-
PApplet.main(new String[] { box2d_Chain.class.getName() });
174+
PApplet.main(new String[] { box2d_Chain_RevoluteJoint.class.getName() });
175175
}
176176

177177
}

examples/testbed/liquidfun_ParticleRenderGroups_LiquidFx.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,23 @@ public void initScene() {
337337
world.mouse_spawn_particles.setBoxShape(size_x, size_y);
338338
world.mouse_spawn_particles.spawn(screen_x, screen_y);
339339

340-
setParticleSpawnProperties(spawn_type);
340+
341+
342+
343+
// add some rigid particles.
344+
// those will be rendered with another render-group into another render-target.
345+
// This makes it possible to apply different post-processing effects on
346+
// different particle render-groups.
347+
int spawn_type_cpy = spawn_type;
348+
349+
setParticleSpawnProperties(4);
350+
351+
screen_x = width/4;
352+
screen_y = height/2;
353+
world.mouse_spawn_particles.setBoxShape(500, 20);
354+
world.mouse_spawn_particles.spawn(screen_x, screen_y);
355+
356+
setParticleSpawnProperties(spawn_type_cpy);
341357

342358
}
343359

@@ -363,7 +379,7 @@ public void createWall(int numx, int numy, float dimx, float dimy, float tx, flo
363379
fixture_def.shape = brick_shape;
364380
fixture_def.density = 30;
365381
fixture_def.friction = 0.50f;
366-
fixture_def.restitution = 0.5f;
382+
fixture_def.restitution = 0.2f;
367383

368384
BodyDef body_def = new BodyDef();
369385
body_def.type = BodyType.DYNAMIC;

examples/testbed/liquidfun_Scene.java

Lines changed: 140 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import com.thomasdiewald.liquidfun.java.DwWorld;
1818
import com.thomasdiewald.pixelflow.java.DwPixelFlow;
1919
import com.thomasdiewald.pixelflow.java.imageprocessing.filter.DwLiquidFX;
20+
import com.thomasdiewald.pixelflow.java.sampling.DwSampling;
21+
22+
import java.util.Random;
2023

2124
import org.jbox2d.collision.AABB;
2225
import org.jbox2d.collision.shapes.CircleShape;
@@ -77,6 +80,7 @@ public void setup(){
7780

7881

7982
reset();
83+
// frameRate(1000);
8084
frameRate(120);
8185
}
8286

@@ -98,7 +102,7 @@ public void reset(){
98102

99103
world.particles.param.falloff_exp1 = 1;
100104
world.particles.param.falloff_exp2 = 2;
101-
world.particles.param.radius_scale = 3;
105+
world.particles.param.radius_scale = 4;
102106

103107
// create scene: rigid bodies, particles, etc ...
104108
initScene();
@@ -182,6 +186,12 @@ public void keyReleased(){
182186
if(key == 'g') APPLY_LIQUID_FX = !APPLY_LIQUID_FX;
183187
}
184188

189+
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+
}
185195

186196

187197
//////////////////////////////////////////////////////////////////////////////
@@ -234,14 +244,14 @@ public void initScene() {
234244
ground.createFixture(shape, 0);
235245

236246

237-
world.bodies.add(ground, true, color(220), !true, color(0), 1f);
247+
world.bodies.add(ground, true, color(160), true, color(0), 1f);
238248
}
239249

240250

241251
{
242252
BodyDef bd = new BodyDef();
243253
bd.type = BodyType.DYNAMIC;
244-
bd.position.set(-15, 20);
254+
bd.position.set(-14, 24);
245255
Body body = world.createBody(bd);
246256

247257
PolygonShape shape = new PolygonShape();
@@ -251,7 +261,7 @@ public void initScene() {
251261
shape.setAsBox(5, 0.5f);
252262
body.createFixture(shape, 0.5f);
253263

254-
world.bodies.add(body, true, color(220), !true, color(0), 1f);
264+
world.bodies.add(body, true, color(220), true, color(0), 1f);
255265

256266
RevoluteJointDef jd = new RevoluteJointDef();
257267
jd.bodyA = ground;
@@ -270,7 +280,7 @@ public void initScene() {
270280
{
271281
BodyDef bd = new BodyDef();
272282
bd.type = BodyType.DYNAMIC;
273-
bd.position.set(-6, 22);
283+
bd.position.set(-4, 22);
274284
Body body = world.createBody(bd);
275285

276286
PolygonShape shape = new PolygonShape();
@@ -280,7 +290,7 @@ public void initScene() {
280290
shape.setAsBox(5, 0.5f);
281291
body.createFixture(shape, 0.5f);
282292

283-
world.bodies.add(body, true, color(220), !true, color(0), 1f);
293+
world.bodies.add(body, true, color(220), true, color(0), 1f);
284294

285295
RevoluteJointDef jd = new RevoluteJointDef();
286296
jd.bodyA = ground;
@@ -299,7 +309,32 @@ public void initScene() {
299309

300310

301311

312+
{
313+
BodyDef bd = new BodyDef();
314+
bd.type = BodyType.DYNAMIC;
315+
bd.position.set(-2,3);
316+
Body body = world.createBody(bd);
302317

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+
}
303338

304339

305340

@@ -309,6 +344,7 @@ public void initScene() {
309344
world.setParticleDamping(0.6f);
310345

311346
{
347+
ParticleGroupDef pgroup_def = new ParticleGroupDef();
312348
pgroup_def.flags = ParticleType.b2_waterParticle | ParticleType.b2_viscousParticle;
313349
pgroup_def.setColor(new Color3f(1, 0.15f, 0.05f));
314350
pgroup_def.linearVelocity.set(0,0);
@@ -322,69 +358,124 @@ public void initScene() {
322358
}
323359

324360
m_time = 0;
325-
361+
362+
int flags = 0;
363+
flags |= ParticleType.b2_waterParticle;
364+
flags |= ParticleType.b2_viscousParticle;
365+
// flags |= ParticleType.b2_colorMixingParticle;
366+
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;
326384
}
327385

328386

329-
ParticleGroupDef pgroup_def = new ParticleGroupDef();
330-
387+
ParticleEmitter emitter0;
388+
ParticleEmitter emitter1;
389+
ParticleEmitter emitter2;
331390

332391
int counter = 0;
333392
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();
334412

335-
float screen_scale = world.transform.screen_scale;
336-
337-
338-
float srandnoise = noise(counter / 100f) * 2 - 1;
339-
340-
float rot_angle = 0 + srandnoise * 0.1f;
341-
float vel_mag = 10 + srandnoise * 5f;
342-
343-
float velx = (float) (Math.cos(rot_angle) * vel_mag);
344-
float vely = (float) (Math.sin(rot_angle) * vel_mag);
345-
346-
Vec2 vel = new Vec2();
347-
vel.x = (float) (Math.cos(rot_angle) * vel_mag);
348-
vel.y = (float) (Math.sin(rot_angle) * vel_mag);
349-
350-
Vec2 pos = new Vec2();
351-
world.transform.getScreen2box(200, 200, pos);
352-
pos.x += random(-1,1) * 1f;
353-
pos.y += random(-1,1) * 1f;
413+
public float emit_angle = (float) (Math.PI * 0.5f);
414+
public float emit_velocity = 20;
354415

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;
355423

356-
// CircleShape shape = new CircleShape();
357-
// world.transform.getScreen2box(200, 200, shape.m_p);
358-
// shape.m_radius = 30 / screen_scale;
424+
protected Vec2 emit_pos_screen = new Vec2();
425+
protected Vec2 emit_pos_world = new Vec2();
359426

360427

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+
}
361437

362-
// PolygonShape shape = new PolygonShape();
363-
// shape.setAsBox(10 / screen_scale, 2, pos, rot);
364-
//
365-
// world.mouse_destroy_particles.destroyParticles(shape);
366-
367-
// pgroup_def.shape = shape;
368-
pgroup_def.angularVelocity = rot_angle;
369-
pgroup_def.linearVelocity.set(vel);
370-
// world.createParticleGroup(pgroup_def);
371-
372438

439+
public void emitParticles(int count){
440+
for(int i = 0; i < count; i++){
441+
emitSingleParticle(i);
442+
}
443+
}
373444

374-
ParticleDef pdef = new ParticleDef();
375-
pdef.position.set(pos);
376-
pdef.velocity.set(vel);
377-
pdef.color = pgroup_def.color;
378-
pdef.flags = pgroup_def.flags;
445+
public void setPosition(float screen_x, float screen_y){
446+
world.transform.getScreen2box(screen_x, screen_y, emit_pos_world);
447+
}
379448

380-
world.createParticle(pdef);
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++;
473+
}
381474

382475

383-
counter++;
384476
}
385477

386478

387-
388479

389480

390481
public static void main(String args[]) {

0 commit comments

Comments
 (0)