-
-
Notifications
You must be signed in to change notification settings - Fork 696
Expand file tree
/
Copy pathMassSample.java
More file actions
213 lines (148 loc) · 5.32 KB
/
MassSample.java
File metadata and controls
213 lines (148 loc) · 5.32 KB
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
/*
* FXGL - JavaFX Game Library. The MIT License (MIT).
* Copyright (c) AlmasB (almaslvl@gmail.com).
* See LICENSE for details.
*/
package basics;
import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.dsl.FXGL;
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.collision.shapes.MassData;
import com.almasb.fxgl.physics.box2d.dynamics.BodyType;
import com.almasb.fxgl.physics.box2d.dynamics.FixtureDef;
import javafx.geometry.Point2D;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseButton;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import static com.almasb.fxgl.dsl.FXGL.*;
/**
* Shows how to change mass within a body directly
* and the affect this has on general physics.
*
* demonstrated using a seesaw, you can see
* how objects of different masses interact with
* their environment differently.
*
* @author Issy Kelly @IssyKelly
*/
public class MassSample extends GameApplication {
int mass = 1;
Text uiText;
@Override
protected void initSettings(GameSettings settings) {
settings.setWidth(1280);
settings.setHeight(720);
}
@Override
protected void initInput() {
// click to add a new ball of the shown (on screen) mass
getInput().addAction(new UserAction("LMB") {
private double x;
private double y;
@Override
protected void onActionBegin() {
x = getInput().getMouseXWorld();
y = getInput().getMouseYWorld();
newBall(mass, x, y);
}
}, MouseButton.PRIMARY);
// press E to increase the mass of new balls by 1
onKeyDown(KeyCode.E, () -> {
mass += 1;
uiText.setText(mass + " mass");
});
// press P to increase the mass of new balls by 10
onKeyDown(KeyCode.P, () -> {
mass += 10;
uiText.setText(mass + " mass");
});
// press Q to decrease the mass of new balls by 1
onKeyDown(KeyCode.Q, () -> {
mass -= 1;
uiText.setText(mass + " mass");
});
// press O to decrease the mass of new balls by 10
onKeyDown(KeyCode.O, () -> {
mass -= 10;
uiText.setText(mass + " mass");
});
}
@Override
protected void initGame() {
getGameScene().setBackgroundColor(Color.LIGHTGRAY);
uiText = new Text(mass + " mass");
Font uifont = new Font(40);
uiText.setFont(uifont);
FXGL.addUINode(uiText, 600, 100);
//floor
Entity floor = entityBuilder()
.at(0, 700)
.viewWithBBox(new Rectangle(1450, 10, Color.BLACK))
.with(new PhysicsComponent())
.buildAndAttach();
// balls for right hand seesaw
newBall(100, 1050, 300);
newBall(10, 700,300);
// balls for left hand side seesaw
newBall(20, 170, 300);
newBall(10, 300,300);
newSeesaw(250,600, 1);
newSeesaw(900,550, 1.4);
}
public void newSeesaw(int x, int y, double size){
int circum = (int) (10 * size);
int rectx = (int) (400 * size);
Entity circle = entityBuilder()
.at(x, y)
.viewWithBBox(new Circle(circum, Color.BLACK))
.with(new PhysicsComponent())
.buildAndAttach();
circle.getComponent(PhysicsComponent.class)
.setBodyType(BodyType.STATIC);
PhysicsComponent plankPhysics = new PhysicsComponent();
plankPhysics.setBodyType(BodyType.DYNAMIC);
FixtureDef fd = new FixtureDef();
fd.setDensity(5.0f);
plankPhysics.setFixtureDef(fd);
Entity plank = entityBuilder()
.at(x, y)
.viewWithBBox(new Rectangle(rectx, 20, Color.BROWN))
.with(plankPhysics)
.buildAndAttach();
Point2D anchor = new Point2D((circum/2), (circum/2));
Point2D anchor2 = new Point2D((rectx/2), (20/2));
getPhysicsWorld().addRevoluteJoint(
circle,
plank,
anchor,
anchor2
);
}
public void newBall(int mass, double x, double y){
var physics = new PhysicsComponent();
physics.setFixtureDef(new FixtureDef().density(25.5f).restitution(0.5f));
physics.setBodyType(BodyType.DYNAMIC);
MassData massValue = new MassData();
// the code to directly change the mass of a body
massValue.mass = mass;
physics.setOnPhysicsInitialized(() -> physics.getBody().setMassData(massValue));
entityBuilder()
.at(x, y)
.bbox(new HitBox(BoundingShape.circle(450 / 15.0 / 2.0)))
.view(texture("ball.png", 450 / 15.0, 449 / 15.0).multiplyColor(Color.BLUE))
.with(physics)
.buildAndAttach();
}
static void main(String[] args) {
launch(args);
}
}