Skip to content

Commit 8ce00af

Browse files
committed
liquid fun example
1 parent 7e5f863 commit 8ce00af

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#
2+
# LiquidFunProcessing | Copyright 2017 Thomas Diewald - www.thomasdiewald.com
3+
#
4+
# https://github.com/diwi/LiquidFunProcessing.git
5+
#
6+
# Box2d / LiquidFun Library for Processing.
7+
# MIT License: https:#opensource.org/licenses/MIT
8+
#
9+
load_library :LiquidFunProcessing
10+
java_import 'com.thomasdiewald.liquidfun.java.DwWorld'
11+
java_import 'com.thomasdiewald.liquidfun.java.render.DwParticleRenderGL'
12+
java_import 'com.thomasdiewald.liquidfun.java.render.DwParticleRenderP5'
13+
java_import 'org.jbox2d.collision.shapes.ChainShape'
14+
java_import 'org.jbox2d.collision.shapes.PolygonShape'
15+
java_import 'org.jbox2d.common.Color3f'
16+
java_import 'org.jbox2d.common.Vec2'
17+
java_import 'org.jbox2d.dynamics.Body'
18+
java_import 'org.jbox2d.dynamics.BodyDef'
19+
java_import 'org.jbox2d.particle.ParticleGroupDef'
20+
java_import 'org.jbox2d.particle.ParticleType'
21+
22+
# Simulation of a clash of two big particle-groups.
23+
#
24+
#
25+
# Controls:
26+
#
27+
# 'r' ... reset
28+
# 't' ... update/pause physics
29+
# 'f' ... toggle debug draw
30+
# 'd' ... display particle renderer type
31+
VIEWPORT_W = 1280
32+
VIEWPORT_H = 720
33+
VIEWPORT_X = 230
34+
VIEWPORT_Y = 0
35+
attr_reader :world, :update_physics, :use_debug_draw
36+
37+
def settings
38+
size(VIEWPORT_W, VIEWPORT_H, P2D)
39+
smooth(8)
40+
end
41+
42+
def setup
43+
surface.set_location(VIEWPORT_X, VIEWPORT_Y)
44+
sketch_title 'Liquid fun dambreak particles'
45+
@update_physics = true
46+
@use_debug_draw = false
47+
reset
48+
frame_rate(120)
49+
end
50+
51+
def release
52+
return if world.nil?
53+
world.release
54+
@world = nil
55+
end
56+
57+
def reset
58+
# release old resources
59+
release
60+
# if false, PShape-Quads are used for particles, instead of openGL points.
61+
# this replaces the default particle renderer (OpenGL) with an alternative
62+
# renderer where particles are rendered as PShape-Quads.
63+
# It renders a bit slower then the OpenGL version and grouped rendering
64+
# is also not possible.
65+
DwWorld.INIT_GL_PARTICLES = true
66+
# create world
67+
@world = DwWorld.new(self, 18)
68+
# create scene: rigid bodies, particles, etc ...
69+
init_scene
70+
end
71+
72+
def draw
73+
world.update if update_physics
74+
canvas = g
75+
canvas.background(32)
76+
canvas.push_matrix
77+
world.apply_transform(canvas)
78+
world.draw_bullet_spawn_track(canvas)
79+
if use_debug_draw
80+
world.display_debug_draw(canvas)
81+
# DwDebugDraw.display(canvas, world)
82+
else
83+
world.display(canvas)
84+
end
85+
canvas.pop_matrix
86+
# info
87+
num_bodies = world.get_body_count
88+
num_particles = world.get_particle_count
89+
string_format = 'liquid fun [bodies: %d] [particles: %d] [fps: %6.2f]'
90+
txt_fps = format(string_format, num_bodies, num_particles, frame_rate)
91+
surface.set_title(txt_fps)
92+
end
93+
94+
def particle_renderer(particles)
95+
return 'GL Particle Renderer' if particles.java_kind_of? DwParticleRenderGL
96+
return 'P5 Particle Renderer' if particles.java_kind_of? DwParticleRenderP5
97+
'Something wrong with particle renderer'
98+
end
99+
100+
#######################################
101+
# User Interaction
102+
#######################################
103+
def key_released
104+
case key
105+
when 't'
106+
@update_physics = !update_physics
107+
when 'r'
108+
reset
109+
when 'f'
110+
@use_debug_draw = !use_debug_draw
111+
when 'd'
112+
puts particle_renderer(world.particles)
113+
end
114+
end
115+
116+
###############init_scene########################
117+
# Scene Setup
118+
# https://github.com/jbox2d/jbox2d/blob/master/jbox2d-testbed/src/main/java/org/jbox2d/testbed/tests/DamBreak.java
119+
#######################################
120+
def init_scene
121+
dimx = world.transform.box2d_dimx
122+
dimy = world.transform.box2d_dimy
123+
dimxh = dimx / 2
124+
dimyh = dimy / 2
125+
bd = BodyDef.new
126+
ground = world.create_body(bd)
127+
shape = ChainShape.new
128+
vertices = [
129+
Vec2.new(-dimxh, 0),
130+
Vec2.new(dimxh, 0),
131+
Vec2.new(dimxh, dimy),
132+
Vec2.new(-dimxh, dimy)
133+
]
134+
shape.create_loop(vertices, 4)
135+
ground.create_fixture(shape, 0.0)
136+
world.bodies.add(ground, false, color(0), true, color(0), 1)
137+
pshape = PolygonShape.new
138+
pd = ParticleGroupDef.new
139+
pd.flags = 0
140+
sx = dimxh * 0.25
141+
sy = dimyh * 0.95
142+
pshape.set_as_box(sx, sy, Vec2.new(-dimxh / 2, dimyh), 0)
143+
pd.shape = pshape
144+
pd.set_color(Color3f.new(0.00, 0.2, 1))
145+
world.create_particle_group(pd)
146+
pshape.set_as_box(sx, sy, Vec2.new(+dimxh / 2, dimyh), 0)
147+
pd.shape = pshape
148+
pd.set_color(Color3f.new(1.00, 0.2, 0.00))
149+
world.create_particle_group(pd)
150+
end

0 commit comments

Comments
 (0)