-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bar4.pde
71 lines (58 loc) · 1.63 KB
/
Bar4.pde
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
class Bar4 {
Body body;
float r = 16; // Size
PImage fort2;
Vec2 vel;
Bar4(Vec2 p) {
makeBody(p,0,new Vec2(0, 0),0);
body.setUserData(this);
}
void makeBody(Vec2 position, float angle, Vec2 vel,float omega) {
// Define a polygon
PolygonShape ps = new PolygonShape();
float w = box2d.scalarPixelsToWorld(6);
float h = box2d.scalarPixelsToWorld(40);
ps.setAsBox(w, h);
BodyDef bd = new BodyDef();
bd.type = BodyType.DYNAMIC;
bd.position.set(box2d.coordPixelsToWorld(position));
// next line would be: |> for angle=0
bd.angle = angle;
// If we draw ship pointing UP at the beggining: ^
// There is an angular difference of PI/2 (see below)
body = box2d.createBody(bd);
FixtureDef fd = new FixtureDef();
fd.shape = ps;
fd.density = 8;
fd.friction = 3;
body.createFixture(fd);
body.setLinearVelocity(vel);
body.setAngularVelocity(omega);
}
void killBody() {
box2d.destroyBody(body);
}
// Draw the ship
void display() {
Vec2 pos = box2d.getBodyPixelCoord(body);
float a = body.getAngle();
Fixture f = body.getFixtureList();
PolygonShape ps = (PolygonShape) f.getShape();
rectMode(CENTER);
pushMatrix();
translate(pos.x, pos.y);
rotate(-a);
stroke(0);
strokeWeight(0.5);
fill(222,142,47);
/*beginShape();
for (int i = 0; i < ps.getVertexCount(); i++) {
Vec2 v = box2d.vectorWorldToPixels(ps.getVertex(i));
vertex(v.x, v.y);
}
endShape(CLOSE);*/
fort2 = loadImage("fort2.png");
image(fort2, -6, -40, 12, 80);
popMatrix();
}
}