-
-
Notifications
You must be signed in to change notification settings - Fork 542
/
PhysicsPlaygroundSample.java
221 lines (188 loc) · 6.87 KB
/
PhysicsPlaygroundSample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* FXGL - JavaFX Game Library. The MIT License (MIT).
* Copyright (c) AlmasB (almaslvl@gmail.com).
* See LICENSE for details.
*/
package intermediate;
import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.input.UserAction;
import com.almasb.fxgl.physics.BoundingShape;
import com.almasb.fxgl.physics.HitBox;
import com.almasb.fxgl.physics.PhysicsComponent;
import com.almasb.fxgl.physics.box2d.dynamics.BodyType;
import com.almasb.fxgl.physics.box2d.dynamics.FixtureDef;
import javafx.collections.FXCollections;
import javafx.geometry.Point2D;
import javafx.geometry.Rectangle2D;
import javafx.scene.Node;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import static com.almasb.fxgl.dsl.FXGL.*;
/**
* Shows how set up physics properties of entities controlled with PhysicsComponent.
*
* @author Almas Baimagambetov (almaslvl@gmail.com)
*/
public class PhysicsPlaygroundSample extends GameApplication {
private FloatTextField fieldFriction;
private FloatTextField fieldDensity;
private FloatTextField fieldRestitution;
private ChoiceBox<ShapeType> cb;
private enum ShapeType {
BOX, CIRCLE, TRIANGLE
}
@Override
protected void initSettings(GameSettings settings) {
settings.setWidth(1280);
settings.setHeight(720);
}
@Override
protected void initInput() {
onBtnDown(MouseButton.SECONDARY, () -> {
spawnBlock(getInput().getMouseXWorld(), getInput().getMouseYWorld());
});
getInput().addAction(new UserAction("Drag") {
private Entity selected = null;
private double offsetX = 0;
private double offsetY = 0;
@Override
protected void onActionBegin() {
var selectionSize = 4;
getGameWorld().getEntitiesInRange(
new Rectangle2D(
getInput().getMouseXWorld() - selectionSize / 2.0,
getInput().getMouseYWorld() - selectionSize / 2.0,
selectionSize,
selectionSize
)
)
.stream()
.filter(e -> e.isType(ShapeType.BOX) || e.isType(ShapeType.CIRCLE) || e.isType(ShapeType.TRIANGLE))
.findFirst()
.ifPresent(e -> {
selected = e;
offsetX = getInput().getMouseXWorld() - selected.getX();
offsetY = getInput().getMouseYWorld() - selected.getY();
});
}
@Override
protected void onAction() {
if (selected != null) {
var p1 = getInput().getMousePositionWorld().subtract(offsetX, offsetY);
var p0 = selected.getPosition();
var scale = 2.5;
selected.getComponent(PhysicsComponent.class).setLinearVelocity(
(p1.getX() - p0.getX()) * scale,
(p1.getY() - p0.getY()) * scale
);
}
}
@Override
protected void onActionEnd() {
selected = null;
}
}, MouseButton.PRIMARY);
}
@Override
protected void initGame() {
getGameScene().setBackgroundColor(Color.BLACK);
entityBuilder()
.buildScreenBoundsAndAttach(40);
}
@Override
protected void initUI() {
fieldFriction = new FloatTextField();
fieldDensity = new FloatTextField();
fieldRestitution = new FloatTextField();
cb = new ChoiceBox<>(FXCollections.observableArrayList(ShapeType.BOX, ShapeType.CIRCLE, ShapeType.TRIANGLE));
cb.setValue(ShapeType.BOX);
VBox box = new VBox(5,
new Text("Friction"),
fieldFriction,
new Text("Density"),
fieldDensity,
new Text("Restitution"),
fieldRestitution,
new Text("Shape type"),
cb
);
addUINode(new Rectangle(1280 - 1100, getAppHeight() - 250, Color.LIGHTGREY), 1100, 0);
addUINode(box, 1100, 0);
// make a static physics body around UI
entityBuilder()
.at(1100, 0)
.bbox(new HitBox(BoundingShape.box(1280 - 1100, getAppHeight() - 250)))
.with(new PhysicsComponent())
.buildAndAttach();
}
private void spawnBlock(double x, double y) {
var p = new PhysicsComponent();
p.setBodyType(BodyType.DYNAMIC);
p.setFixtureDef(new FixtureDef()
.friction(fieldFriction.getFloat())
.density(fieldDensity.getFloat())
.restitution(fieldRestitution.getFloat())
);
BoundingShape shape;
Node view;
switch (cb.getValue()) {
case BOX:
shape = BoundingShape.box(40, 40);
break;
case CIRCLE:
shape = BoundingShape.circle(20);
break;
case TRIANGLE:
default:
shape = BoundingShape.polygon(new Point2D(0, 40), new Point2D(20, 0), new Point2D(40, 40));
break;
}
switch (cb.getValue()) {
case BOX:
view = new Rectangle(40, 40, Color.BLUE);
((Rectangle) view).setStroke(Color.DARKBLUE);
break;
case CIRCLE:
view = new Circle(20, 20, 20, Color.YELLOW);
((Circle) view).setStroke(Color.ORANGE);
break;
case TRIANGLE:
default:
view = new Polygon(0, 40, 20, 0, 40, 40);
((Polygon) view).setFill(Color.RED);
((Polygon) view).setStroke(Color.DARKRED);
break;
}
entityBuilder()
.type(cb.getValue())
.at(x, y)
.bbox(shape)
.view(view)
.with(p)
.buildAndAttach();
}
private static class FloatTextField extends TextField {
FloatTextField() {
setText("0.2");
}
float getFloat() {
try {
return Float.parseFloat(getText());
} catch (Exception e) {
return 0.2f;
}
}
}
public static void main(String[] args) {
launch(args);
}
}