-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRuggedTerrain.java
198 lines (139 loc) · 3.67 KB
/
RuggedTerrain.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
import java.util.ArrayList;
import processing.core.*;
import pbox2d.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
public class RuggedTerrain extends PApplet {
PBox2D box2d;
BuildGround surface;
ArrayList<Circle> circles = new ArrayList<Circle>();
public void setup() {
size(600, 400);
smooth();
initScene();
}
public void draw() {
background(255);
surface.display();
box2d.step();
if (mousePressed) {
circles.add(new Circle(mouseX, mouseY, 6));
}
if (keyPressed) {
circles.clear();
initScene();
}
for (Circle c: circles) {
c.display();
}
}
void initScene() {
background(255);
box2d = new PBox2D(this);
box2d.createWorld();
box2d.setGravity(0, -10);
surface = new BuildGround();
}
class Circle {
// We need to keep track of a Body and a width and height
Body body;
float r;
// Constructor
Circle(float x, float y, float r_) {
r = r_;
makeBody(x, y, r);
}
// This function removes the particle from the box2d world
void killBody() {
box2d.destroyBody(body);
}
// Is the particle ready for deletion?
boolean done() {
// Let's find the screen position of the particle
Vec2 pos = box2d.getBodyPixelCoord(body);
// Is it off the bottom of the screen?
if (pos.y > height+ r*2) {
killBody();
return true;
}
return false;
}
// Drawing the box
void display() {
// We look at each body and get its screen position
Vec2 pos = box2d.getBodyPixelCoord(body);
// Get its angle of rotation
float a = body.getAngle();
strokeWeight(1);
pushMatrix();
translate(pos.x,pos.y);
rotate(-a);
fill(175);
stroke(0);
ellipse(0, 0, r*2, r*2);
line(0, 0, r, 0);
popMatrix();
}
// This function adds the circle to the box2d world
void makeBody(float x, float y, float r) {
BodyDef bd = new BodyDef();
bd.position = box2d.coordPixelsToWorld(x, y);
body = box2d.world.createBody(bd);
CircleDef cd = new CircleDef();
cd.radius = box2d.scalarPixelsToWorld(r);
// Parameters that affect physics
cd.density = 1.0f;
cd.friction = 0.01f;
cd.restitution = 0.3f;
body.createShape(cd);
body.setMassFromShapes();
// Give it some initial random velocity
body.setLinearVelocity(new Vec2(random(-5,5),random(2,5)));
body.setAngularVelocity(random(-5,5));
}
}
public class BuildGround {
// track ground points
ArrayList<Vec2> ground;
// constructor
BuildGround() {
ground = new ArrayList<Vec2>();
// put ground in world
EdgeChainDef edges = new EdgeChainDef();
// perlin noise
float time = 0.0f;
// MUST BE BACKWARDS or they won't bounce off the surface
for (float x = width + 10; x > -10; x -= 18) {
float y;
y = noise(time) * height * 1.2f;
// edge point in window
Vec2 screenEdge = new Vec2(x, y);
ground.add(screenEdge);
// convert to box2d world
Vec2 edge = box2d.coordPixelsToWorld(screenEdge);
edges.addVertex(edge);
// increment
time += 0.1f;
}
edges.setIsLoop(false);
edges.friction = 2.0f;
edges.restitution = 0.3f;
// make the edge chain a body
BodyDef bd = new BodyDef();
bd.position.set(0.0f, 0.0f);
Body body = box2d.world.createBody(bd);
body.createShape(edges);
}
void display() {
strokeWeight(2);
stroke(0);
noFill();
beginShape();
for (Vec2 v: ground) {
vertex(v.x, v.y);
}
endShape();
}
}
}