-
Notifications
You must be signed in to change notification settings - Fork 3
/
noc_501.js
188 lines (142 loc) · 4.39 KB
/
noc_501.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
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
/*globals paper, console, $ */
/*jslint nomen: true, undef: true, sloppy: true */
var NOC = NOC || {};
// Extended Daniel Shiffman's natureofcode example to paper.js
// https://github.com/shiffman/The-Nature-of-Code-Examples-p5.js/tree/master/chp05_libraries/box2d-html5
// create local scope to avoid polluting global namespace
NOC.demo = NOC.demo || [];
NOC.demo[1] = function (canvasName) {
// put paper.js in local environment, and setup canvas and tool (for events)
this.paper = new paper.PaperScope();
this.paper.setup(canvasName);
with (this.paper) {
var tool = new Tool();
// setup objects used in the sketch:
// for the box, x and y are in pixel space already. let the B2Helper function do conversion.
var Box = function(x, y) {
this.w = getRandom(10, 30);
this.h = getRandom(10, 30);
// Define a body
var bd = new box2d.b2BodyDef();
bd.type = box2d.b2BodyType.b2_dynamicBody;
bd.position = B2Helper.scaleToWorld(x,y);
// Define a fixture
var fd = new box2d.b2FixtureDef();
// Fixture holds shape
fd.shape = new box2d.b2PolygonShape();
fd.shape.SetAsBox(B2Helper.scaleToWorld(this.w/2), B2Helper.scaleToWorld(this.h/2));
// Some physics
fd.density = 1.0;
fd.friction = 0.5;
fd.restitution = 0.2;
// Create the body
this.body = world.CreateBody(bd);
// Attach the fixture
this.body.CreateFixture(fd);
// creates the shape to be drawn by paper.js
this.shape = new Shape.Rectangle({
point: [x-this.w/2, y-this.h/2],
size: [this.w, this.h],
strokeColor: 'black',
fillColor: getRandomColor()
});
};
// update the box's locations. paper.js will handle the drawing after it is updated
// unlike p5/processing where draw function must draw the box again every time.
Box.prototype.update = function() {
// Get the body's position
var pos = B2Helper.scaleToPixels(this.body.GetPosition());
// Get its angle of rotation
var a = this.body.GetAngleDegrees();
// Draw it!
// translate:
this.shape.position.x = pos.x;
this.shape.position.y = pos.y;
// rotation:
this.shape.rotate(a-this.shape.rotation);
};
// This function removes the particle from the box2d world, and also in paper.js
Box.prototype.killBody = function() {
world.DestroyBody(this.body);
this.shape.remove();
};
// Is the particle ready for deletion?
Box.prototype.done = function() {
// Let's find the screen position of the particle
var pos = B2Helper.scaleToPixels(this.body.GetPosition());
// Is it off the bottom of the screen?
if (pos.y > height+this.w*this.h) {
this.killBody();
return true;
}
return false;
};
var mousePressed = function (event) {
if (clickMe) {
clickMe.remove();
clickMe = null;
}
// create a new box at the mouse click location
var b = new Box(event.point.x,event.point.y);
boxes.push(b);
};
// main animation loop:
var draw = function (event) {
// main simulation step for physics engine.
// 2nd and 3rd arguments are velocity and position iterations
world.Step(timeStep,10,10);
// update location/orientation for all the boxes (rather than redrawing them)
for (var i = boxes.length-1; i >= 0; i--) {
boxes[i].update();
if (boxes[i].done()) {
boxes.splice(i,1);
}
}
};
// useful helper functions
var getRandom = function (min, max) {
return Math.random() * (max - min) + min;
};
var getRandomInt = function (min, max) {
return Math.floor(Math.random() * (max - min)) + min;
};
var getRandomColor = function() {
var c = new Color(Math.random(), Math.random(), Math.random());
return c;
};
// init and setup:
// timestep (1 frame = 1 / 60 fps)
var timeStep = 1.0/60;
// screen size
var height = view.size.height;
var width = view.size.width;
// A reference to our box2d world
var world;
// A list for all of our boxes
var boxes = [];
// Initialize box2d physics and create the world
world = B2Helper.createWorld();
// set animation and event hooks:
var desc = new PointText(24, height-24);
desc.content = 'Box2D with Paper.js.';
desc.style = {
fontFamily: 'Courier New',
fontWeight: 'normal',
fontSize: 24,
fillColor: '#aaaaaa',
justification: 'left'
};
var clickMe = new PointText(width*1/2, height*1/4);
clickMe.content = 'Click on me.';
clickMe.style = {
fontFamily: 'Courier New',
fontWeight: 'normal',
fontSize: 24,
fillColor: '#eb6276',
justification: 'center'
};
view.onFrame = draw;
tool.onMouseDown = mousePressed;
tool.onMouseDrag = mousePressed;
}
};