Skip to content

Commit e3ee534

Browse files
committed
PShape Particle renderer example
1 parent 5a01d41 commit e3ee534

2 files changed

Lines changed: 222 additions & 0 deletions

File tree

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/**
2+
*
3+
* LiquidFunProcessing | Copyright 2017 Thomas Diewald - www.thomasdiewald.com
4+
*
5+
* https://github.com/diwi/LiquidFunProcessing.git
6+
*
7+
* Box2d / LiquidFun Library for Processing.
8+
* MIT License: https://opensource.org/licenses/MIT
9+
*
10+
*/
11+
12+
13+
14+
package liquidfun_DamBreak_PShapeParticles;
15+
16+
17+
import com.thomasdiewald.liquidfun.java.DwWorld;
18+
import com.thomasdiewald.liquidfun.java.render.DwParticleRenderP5;
19+
20+
import org.jbox2d.collision.shapes.ChainShape;
21+
import org.jbox2d.collision.shapes.PolygonShape;
22+
import org.jbox2d.common.Color3f;
23+
import org.jbox2d.common.Vec2;
24+
import org.jbox2d.dynamics.Body;
25+
import org.jbox2d.dynamics.BodyDef;
26+
import org.jbox2d.particle.ParticleGroupDef;
27+
import org.jbox2d.particle.ParticleType;
28+
29+
import processing.core.*;
30+
import processing.opengl.PGraphics2D;
31+
32+
33+
public class liquidfun_DamBreak_PShapeParticles extends PApplet {
34+
35+
36+
//
37+
// Simulation of a clash of two big particle-groups.
38+
//
39+
//
40+
// Controls:
41+
//
42+
// LMB ... drag bodies
43+
// LMB + SHIFT ... shoot bullet
44+
// MMB ... add particles
45+
// RMB ... remove particles
46+
// 'r' ... reset
47+
// 't' ... update/pause physics
48+
// 'f' ... toggle debug draw
49+
//
50+
51+
52+
int viewport_w = 1280;
53+
int viewport_h = 720;
54+
int viewport_x = 230;
55+
int viewport_y = 0;
56+
57+
boolean UPDATE_PHYSICS = true;
58+
boolean USE_DEBUG_DRAW = false;
59+
60+
DwWorld world;
61+
62+
// PImage sprite;
63+
64+
public void settings(){
65+
size(viewport_w, viewport_h, P2D);
66+
smooth(8);
67+
}
68+
69+
70+
public void setup(){
71+
surface.setLocation(viewport_x, viewport_y);
72+
// sprite = loadImage("sprite.png");
73+
74+
75+
reset();
76+
frameRate(120);
77+
}
78+
79+
80+
public void release(){
81+
if(world != null) world.release(); world = null;
82+
}
83+
84+
85+
public void reset(){
86+
// release old resources
87+
release();
88+
89+
world = new DwWorld(this, 18);
90+
91+
92+
// this replaces the default particle renderer (OpenGL) by a alternative
93+
// renderer, where particles are rendered s PShape Quads.
94+
// It renders a bit slower then the OpenGL version and grouped rendering
95+
// is not possible.
96+
world.setParticleRender(new DwParticleRenderP5(this, world, world.transform));
97+
98+
// create scene: rigid bodies, particles, etc ...
99+
initScene();
100+
}
101+
102+
103+
104+
public void draw(){
105+
if(UPDATE_PHYSICS){
106+
world.update();
107+
}
108+
109+
PGraphics2D canvas = (PGraphics2D) this.g;
110+
canvas.background(32);
111+
canvas.pushMatrix();
112+
world.applyTransform(canvas);
113+
world.drawBulletSpawnTrack(canvas);
114+
if(USE_DEBUG_DRAW){
115+
world.displayDebugDraw(canvas);
116+
// DwDebugDraw.display(canvas, world);
117+
} else {
118+
world.display(canvas);
119+
}
120+
canvas.popMatrix();
121+
122+
// info
123+
int num_bodies = world.getBodyCount();
124+
int num_particles = world.getParticleCount();
125+
String txt_fps = String.format(getClass().getName()+ " [bodies: %d] [particles: %d] [fps %6.2f]", num_bodies, num_particles, frameRate);
126+
surface.setTitle(txt_fps);
127+
}
128+
129+
130+
131+
//////////////////////////////////////////////////////////////////////////////
132+
// User Interaction
133+
//////////////////////////////////////////////////////////////////////////////
134+
public void keyReleased(){
135+
if(key == 't') UPDATE_PHYSICS = !UPDATE_PHYSICS;
136+
if(key == 'r') reset();
137+
if(key == 'f') USE_DEBUG_DRAW = !USE_DEBUG_DRAW;
138+
}
139+
140+
141+
142+
//////////////////////////////////////////////////////////////////////////////
143+
// Scene Setup
144+
//////////////////////////////////////////////////////////////////////////////
145+
146+
// https://github.com/jbox2d/jbox2d/blob/master/jbox2d-testbed/src/main/java/org/jbox2d/testbed/tests/DamBreak.java
147+
public void initScene() {
148+
149+
float dimx = world.transform.box2d_dimx;
150+
float dimy = world.transform.box2d_dimy;
151+
152+
float dimxh = dimx/2;
153+
float dimyh = dimy/2;
154+
155+
{
156+
BodyDef bd = new BodyDef();
157+
Body ground = world.createBody(bd);
158+
159+
ChainShape shape = new ChainShape();
160+
Vec2[] vertices = {new Vec2(-dimxh, 0), new Vec2(dimxh, 0), new Vec2(dimxh, dimy), new Vec2(-dimxh, dimy)};
161+
shape.createLoop(vertices, 4);
162+
ground.createFixture(shape, 0.0f);
163+
164+
world.bodies.add(ground, false, color(0), true, color(0), 1f);
165+
}
166+
167+
168+
169+
{
170+
PolygonShape shape = new PolygonShape();
171+
ParticleGroupDef pd = new ParticleGroupDef();
172+
173+
pd.flags = 0
174+
| ParticleType.b2_waterParticle
175+
| ParticleType.b2_viscousParticle
176+
| ParticleType.b2_colorMixingParticle
177+
// | ParticleType.b2_powderParticle
178+
// | ParticleType.b2_springParticle
179+
// | ParticleType.b2_tensileParticle
180+
;
181+
182+
float sx = dimxh * 0.25f;
183+
float sy = dimyh * 0.95f;
184+
185+
shape.setAsBox(sx, sy, new Vec2(-dimxh/2, dimyh), 0);
186+
pd.shape = shape;
187+
pd.setColor(new Color3f(0.00f, 0.2f, 1));
188+
world.createParticleGroup(pd);
189+
190+
shape.setAsBox(sx, sy, new Vec2(+dimxh/2, dimyh), 0);
191+
pd.shape = shape;
192+
pd.setColor(new Color3f(1.00f, 0.2f, 0.00f));
193+
world.createParticleGroup(pd);
194+
}
195+
}
196+
197+
198+
199+
public static void main(String args[]) {
200+
PApplet.main(new String[] { liquidfun_DamBreak_PShapeParticles.class.getName() });
201+
}
202+
203+
}

src/com/thomasdiewald/liquidfun/java/DwWorld.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,25 @@ public DwWorld(PApplet papplet, float scale){
136136
}
137137

138138

139+
public void setParticleRender(DwParticleRender particle_render){
140+
if(particle_render == null){
141+
return;
142+
}
143+
if(particles != null){
144+
particles.release();
145+
}
146+
particles = particle_render;
147+
}
148+
149+
public void setBodyRender(DwBodyGroup body_render){
150+
if(body_render == null){
151+
return;
152+
}
153+
if(bodies != null){
154+
bodies.release();
155+
}
156+
bodies = body_render;
157+
}
139158

140159
/**
141160
* called by processing

0 commit comments

Comments
 (0)