-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaroa_passo_5.js
executable file
·116 lines (93 loc) · 2.48 KB
/
garoa_passo_5.js
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
var world;
var boxes = [];
var boundaries = [];
function setup() {
// canvas size
createCanvas(800,600);
// world and gravity
world = createWorld();
boundaries.push( new Boundary(width/4, height-10, 100, 10) );
boundaries.push( new Boundary(3*width/4, height-50, 100, 10) );
// frameRate(30);
}
function draw() {
background(63);
// fracao de tempo que devemos avancar na simulacao
var step = 1.0/60;
// step, velocity and position iterations
world.Step(step, 10, 10);
if (isMousePressed) {
var b = new Box(mouseX, mouseY);
boxes.push(b);
}
for (var i = 0; i < boundaries.length; i++) {
boundaries[i].display();
}
for (var i = 0; i < boxes.length; i++) {
boxes[i].display();
}
}
function Box(x, y) {
this.w = 16;
this.h = 16;
this.c = random(255);
this.r = random(-30,30);
this.g = random(-30,30);
this.b = random(-30,30);
var bd = new box2d.b2BodyDef();
bd.type = box2d.b2BodyType.b2_dynamicBody;
bd.position = scaleToWorld(x,y);
// criar a moldura do corpo e juntar com uma fixture
var fd = new box2d.b2FixtureDef();
fd.shape = new box2d.b2PolygonShape();
fd.shape.SetAsBox(scaleToWorld(this.w/2), scaleToWorld(this.h/2));
fd.density = 1.0;
fd.friction = 0.5;
fd.restitution = 0.2;
// cria o corpo no mundo
this.body = world.CreateBody(bd);
// junta com o shape box
this.body.CreateFixture(fd);
// desenha o resultado
this.display = function() {
// position
var pos = scaleToPixels(this.body.GetPosition());
// rotation
var a = this.body.GetAngleRadians();
rectMode(CENTER);
push();
translate(pos.x, pos.y);
rotate(a);
fill(this.c + this.r, this.c + this.g, this.c + this.b);
stroke(200);
strokeWeight(2);
rect(0, 0, this.w, this.h);
pop();
}
}
function Boundary(x_,y_,w_,h_) {
// seta as propriedades do limite
this.x = x_;
this.y = y_;
this.w = w_;
this.h = h_;
// cria um corpo
var bd = new box2d.b2BodyDef();
bd.type = box2d.b2BodyType.b2_staticBody;
bd.position.x = scaleToWorld(this.x);
bd.position.y = scaleToWorld(this.y);
// cria uma fixture
var fd = new box2d.b2FixtureDef();
fd.density = 1.0;
fd.friction = 0.5;
fd.restitution = 0.2;
fd.shape = new box2d.b2PolygonShape();
fd.shape.SetAsBox(this.w/(scaleFactor*2), this.h/(scaleFactor*2));
this.body = world.CreateBody(bd).CreateFixture(fd);
this.display = function() {
fill(127);
stroke(0);
rectMode(CENTER);
rect(this.x,this.y,this.w,this.h);
}
}