Skip to content

Commit f6ce8ce

Browse files
committed
feat: Silly debug draw demo
1 parent f22f6c5 commit f6ce8ce

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.badlogic.gdx.box2d.test;
2+
3+
import com.badlogic.gdx.box2d.Box2d;
4+
import com.badlogic.gdx.box2d.enums.b2BodyType;
5+
import com.badlogic.gdx.box2d.structs.b2BodyDef;
6+
import com.badlogic.gdx.box2d.structs.b2BodyId;
7+
import com.badlogic.gdx.box2d.structs.b2DebugDraw;
8+
import com.badlogic.gdx.box2d.structs.b2Polygon;
9+
import com.badlogic.gdx.box2d.structs.b2ShapeDef;
10+
import com.badlogic.gdx.box2d.structs.b2Vec2;
11+
import com.badlogic.gdx.box2d.structs.b2WorldDef;
12+
import com.badlogic.gdx.box2d.structs.b2WorldId;
13+
import com.badlogic.gdx.jnigen.runtime.closure.ClosureObject;
14+
15+
import javax.swing.*;
16+
import java.awt.*;
17+
import java.awt.image.BufferStrategy;
18+
19+
import static com.badlogic.gdx.box2d.Box2d.*;
20+
21+
public class Box2dDrawTest {
22+
23+
private static final int e_columns = 100;
24+
private static final int e_rows = 100;
25+
26+
public static void main (String[] args) {
27+
Box2d.initialize();
28+
29+
b2WorldDef worldDef = b2DefaultWorldDef();
30+
b2WorldId worldId = b2CreateWorld(worldDef.asPointer());
31+
32+
{
33+
b2BodyDef bd = b2DefaultBodyDef();
34+
bd.position().x(0.0f);
35+
bd.position().y(-1.0f);
36+
b2BodyId groundId = b2CreateBody(worldId, bd.asPointer());
37+
38+
b2Polygon box = b2MakeBox(1000.0f, 1.0f);
39+
b2ShapeDef sd = b2DefaultShapeDef();
40+
b2CreatePolygonShape(groundId, sd.asPointer(), box.asPointer());
41+
}
42+
43+
b2Polygon box = b2MakeRoundedBox(0.45f, 0.45f, 0.05f);
44+
b2ShapeDef sd = b2DefaultShapeDef();
45+
sd.density(1.0f);
46+
sd.friction(0.3f);
47+
48+
float offset = 0.2f;
49+
float dx = 5.0f;
50+
float xroot = -0.5f * dx * (e_columns - 1.0f);
51+
52+
for (int j = 0; j < e_columns; ++j) {
53+
float x = xroot + j * dx;
54+
for (int i = 0; i < e_rows; ++i) {
55+
b2BodyDef bd = b2DefaultBodyDef();
56+
bd.type(b2BodyType.b2_dynamicBody);
57+
bd.position().x(x + offset * i);
58+
bd.position().y(0.5f + 1.0f * i);
59+
60+
b2BodyId bodyId = b2CreateBody(worldId, bd.asPointer());
61+
b2CreatePolygonShape(bodyId, sd.asPointer(), box.asPointer());
62+
}
63+
}
64+
65+
66+
startLoop(worldId);
67+
}
68+
69+
public static void startLoop(b2WorldId worldId) {
70+
JFrame frame = new JFrame("Example");
71+
frame.setSize(800, 600);
72+
frame.setVisible(true);
73+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
74+
frame.setResizable(false);
75+
76+
Canvas canvas = new Canvas();
77+
frame.add(canvas);
78+
canvas.createBufferStrategy(2);
79+
80+
b2DebugDraw draw = b2DefaultDebugDraw();
81+
draw.drawAABBs(true);
82+
83+
draw.DrawPolygon(ClosureObject.fromClosure((vertices, vertexCount, color, context) -> {
84+
BufferStrategy bs = canvas.getBufferStrategy();
85+
Graphics g = bs.getDrawGraphics();
86+
87+
88+
Polygon polygon = new Polygon();
89+
for (int i = 0; i < vertexCount; i++) {
90+
b2Vec2 vec2 = vertices.get(i);
91+
polygon.addPoint((int)vec2.x() * 10, (int)vec2.y() * 10);
92+
}
93+
94+
g.setColor(new Color(color.getIndex()));
95+
g.drawPolygon(polygon);
96+
97+
}));
98+
99+
Timer timer = new Timer(1000 / 60, e -> {
100+
float timeStep = 1.0f / 60.0f;
101+
int subStepCount = 4;
102+
b2World_Step(worldId, timeStep, subStepCount);
103+
104+
BufferStrategy bs = canvas.getBufferStrategy();
105+
Graphics g = bs.getDrawGraphics();
106+
g.setColor(Color.BLACK);
107+
g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
108+
109+
b2World_Draw(worldId, draw.asPointer());
110+
111+
g.dispose();
112+
bs.show();
113+
});
114+
115+
timer.start();
116+
}
117+
}

0 commit comments

Comments
 (0)