-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
223 lines (180 loc) · 5.4 KB
/
index.html
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<!doctype html>
<html>
<head>
<title>concaveShapesJS</title>
<style>
#canvas {
position: absolute;
}
#draw {
background-color: rgba(5, 5, 5, .03);
}
</style>
</head>
<body>
<canvas id="canvas" width="400" height="410"></canvas>
<canvas id="draw" width="400" height="410"></canvas>
<p>Click and drag above to draw shapes. Intersecting lines don't work well.</p>
<script src="Box2d.min.js"></script>
<script src="bourke.js"></script>
<script src="triangulate.js"></script>
<script>
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d');
var draw = document.getElementById('draw'),
ctx2 = draw.getContext('2d'),
b2Vec2 = Box2D.Common.Math.b2Vec2,
b2BodyDef = Box2D.Dynamics.b2BodyDef,
b2Body = Box2D.Dynamics.b2Body,
b2FixtureDef = Box2D.Dynamics.b2FixtureDef,
b2Fixture = Box2D.Dynamics.b2Fixture,
b2World = Box2D.Dynamics.b2World,
b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape,
b2DebugDraw = Box2D.Dynamics.b2DebugDraw,
scaledPoints = [],
_ratio = 30,
_friction = 0.8,
_restitution = 0.3,
_density = 0.7,
_pixel_dist = 20,
_objPoints = [],
_borderSize = 4,
_polyWidth, _endX, _mouseLastX, _polyHeight, _endY, _mouseLastY,
mousedown = false,
world = new b2World(new b2Vec2(0, 10.0), false),
floor = new b2BodyDef,
fixture = new b2FixtureDef;
/* setup debug draw */
var debugDraw = new b2DebugDraw;
debugDraw.SetSprite(ctx);
debugDraw.SetDrawScale(_ratio);
debugDraw.SetFillAlpha(0.3);
debugDraw.SetLineThickness(1.0);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
world.SetDebugDraw(debugDraw);
floor.type = b2Body.b2_staticBody;
floor.position.x = (0 + (400 / 2)) / _ratio;
floor.position.y = (340 + (100 / 2)) / _ratio;
fixture.shape = new b2PolygonShape;
fixture.shape.SetAsBox((390 / 2) / _ratio, (30 / 2) / _ratio);
world.CreateBody(floor).CreateFixture(fixture);
canvas.addEventListener('mousedown', mouseDown, false);
canvas.addEventListener('mousemove', mouseMove, false);
canvas.addEventListener('mouseup', mouseUp, false);
window.setInterval(function () {
world.Step(1 / 60, 10, 10);
world.ClearForces();
world.DrawDebugData();
/*
var tmpBody = world.GetBodyList();
while (tmpBody != null) {
if (!tmpBody.IsAwake() && tmpBody.GetType() === b2Body.b2_dynamicBody) {
_world.DestroyBody(tmpBody);
}
tmpBody = tmpBody.GetNext();
}
*/
}, 1000 / 60);
function mouseDown (evt) {
draw.width = draw.width;
ctx2.beginPath();
ctx2.moveTo(evt.clientX, evt.clientY);
mousedown = true;
_polyWidth = _endX = _mouseLastX = evt.clientX;
_polyHeight = _endY = _mouseLastY = evt.clientY;
scaledPoints = [];
_objPoints = [];
_objPoints.push({ x: _mouseLastX, y: _mouseLastY });
}
function mouseMove (evt) {
var dist_x = evt.clientX - _mouseLastX;
var dist_y = evt.clientY - _mouseLastY;
if (dist_x * dist_x + dist_y * dist_y > _pixel_dist * _pixel_dist) {
if(mousedown) {
ctx2.lineTo(evt.clientX, evt.clientY);
ctx2.stroke();
}
_objPoints.push({ x: evt.clientX, y: evt.clientY });
_mouseLastX = evt.clientX;
_mouseLastY = evt.clientY;
if (evt.clientX < 0 && _polyWidth <= 0) {
if (_polyWidth < evt.clientX) {
_polyWidth = evt.clientX;
}
} else {
if (_polyWidth < evt.clientX) {
_polyWidth = evt.clientX;
}
}
if(evt.clientY < 0 && _polyHeight <= 0) {
if (_polyHeight < evt.clientY) {
_polyHeight = evt.clientY;
}
} else {
if (_polyHeight < evt.clientY) {
_polyHeight = evt.clientY;
}
}
if (_endX > evt.clientX) {
_endX = evt.clientX;
}
if (_endY > evt.clientY) {
_endY = evt.clientY;
}
}
}
function mouseUp (evt) {
mousedown = false;
ctx2.closePath();
ctx2.stroke();
_polyWidth = Math.abs(_polyWidth - _endX) + _borderSize;
_polyHeight = Math.abs(_polyHeight - _endY) + _borderSize;
_endX = _endX - _borderSize / 2;
_endY = _endY - _borderSize / 2;
var cx = _endX + (_polyWidth / 2);
var cy = _endY + (_polyHeight / 2);
for (var i = 0; i < _objPoints.length; i++) {
scaledPoints.push(new b2Vec2((_objPoints[i].x - cx) / _ratio, (_objPoints[i].y - cy) / _ratio));
}
var complex = false;
var boxShape = new b2PolygonShape();
if (ClockWise(scaledPoints) === CLOCKWISE) {
scaledPoints.reverse();
}
if (Convex(scaledPoints) === CONCAVE) {
complex = true;
}
var fixDef;
var bodyDef = new b2BodyDef();
bodyDef.position.x = cx / _ratio;
bodyDef.position.y = cy / _ratio;
bodyDef.type = b2Body.b2_dynamicBody;
var obj = world.CreateBody(bodyDef);
if (complex) {
var tmp = process(scaledPoints);
if (tmp != null) {
for (i = 0; i < tmp.length; i = i + 3) {
boxShape = new b2PolygonShape();
boxShape.SetAsArray(new Array(tmp[i], tmp[i + 1], tmp[i + 2]));
fixDef = new b2FixtureDef();
fixDef.density = _density;
fixDef.restitution = _restitution;
fixDef.friction = _friction;
fixDef.shape = boxShape;
obj.CreateFixture(fixDef);
}
} else {
draw.width = draw.width;
}
} else {
boxShape.SetAsArray(scaledPoints, scaledPoints.length);
fixDef = new b2FixtureDef();
fixDef.density = _density;
fixDef.restitution = _restitution;
fixDef.friction = _friction;
fixDef.shape = boxShape;
obj.CreateFixture(fixDef);
}
}
</script>
</body></html>