-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc10.html
276 lines (193 loc) · 6.05 KB
/
c10.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<style>
body {
background: #dddddd;
}
#canvas {
background: #ffffff;
border: thin inset #aaaaaa;
}
</style>
</head>
<body>
<canvas id='canvas' width="800" height="600">
Canvas not supported
</canvas>
<script>
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
function windowToCanvas(x, y) {
var bbox = canvas.getBoundingClientRect();
return { x : x - bbox.left, y : y - bbox.top }
}
function Vector2d(x, y) {
this.x = x;
this.y = y;
}
Vector2d.prototype = {
add : function (vec) {
return new Vector2d(this.x + vec.x, this.y + vec.y);
},
sub : function (vec) {
return new Vector2d(this.x - vec.x, this.y - vec.y);
},
scale : function (scale) {
return new Vector2d(this.x * scale, this.y * scale);
},
length : function () {
return Math.sqrt(this.x * this.x + this.y * this.y);
},
normalize : function () {
var len = this.length();
return new Vector2d(this.x / len, this.y / len);
},
perp : function (cw) {
if(cw)
return new Vector2d(-this.y , this.x);
else return new Vector2d( this.y, -this.x);
},
dot : function (vec) {
return this.x * vec.x + this.y * vec.y;
}
};
function Point(x, y) {
this.x = x;
this.y = y;
}
function Line(sPt, ePt) {
this.sPt = sPt;
this.ePt = ePt;
}
Line.prototype = {
draw : function () {
context.save();
context.beginPath();
context.moveTo(this.sPt.x, this.sPt.y);
context.lineTo(this.ePt.x, this.ePt.y);
context.stroke();
context.restore();
}
};
function Ball() {
this.x = 0;
this.y = 0;
this.radius = 10;
this.color = "red";
this.angle = 0;
}
Ball.prototype = {
draw : function() {
context.save();
context.beginPath();
context.translate(this.x, this.y);
context.rotate(this.angle);
context.arc(0, 0, this.radius, 0, Math.PI * 2);
// context.fillRect(-30, -5, 30, 10);
context.fillStyle = this.color;
context.fill();
context.restore();
}
}
var ball = new Ball();
var v = new Vector2d(0, 0);
var line1 = new Line(new Point(500, 400), new Point(800, 300));
var line2 = new Line(new Point(400, 600), new Point(500, 400));
var line3 = new Line(new Point(0, 600), new Point(200, 400));
var line4 = new Line(new Point(200, 400), new Point(450, 600));
var leftWall = new Line(new Point(10, 10), new Point(10, canvas.height - 10));
var rightWall = new Line( new Point(canvas.width - 10, canvas.height - 10), new Point(canvas.width - 10, 10));
var bottomWall = new Line(new Point(10, canvas.height - 10), new Point(canvas.width - 10, canvas.height - 10));
var topWall = new Line(new Point(canvas.width - 10, 10), new Point(10, 10));
var lines = [line1, line2, leftWall, rightWall, bottomWall, topWall];
var speed = 100;
var friction = 0.98;
var gravity = 0.4;
var startX = 100;
var startY = 500;
ball.x = startX;
ball.y = startY;
//ball.angle = 45 * Math.PI / 180;
canvas.onmousedown = function(e) {
var loc = windowToCanvas(e.clientX, e.clientY);
var dx = loc.x - startX;
var dy = loc.y - startY;
var m = new Vector2d(dx, dy);
m = m.normalize().scale(speed);
ball.x = startX;
ball.y = startY;
v.x = m.x;
v.y = m.y;
};
requestAnimationFrame(loop);
function hitTestByDistance(line) {
var a = new Vector2d(line.ePt.x - line.sPt.x, line.ePt.y - line.sPt.y);
var b = new Vector2d(line.sPt.x - ball.x, line.sPt.y - ball.y);
var t = b.dot(a) / a.dot(a) * -1;
if(t < 0 || t > 1) return false;
var p = a.scale(t).add(b);
if(p.length() <= ball.radius) {
b = b.scale(-1);
var c = a.perp(false);
var d = c.normalize();
var len = b.dot(d);
var e = d.scale(ball.radius - len);
ball.x += e.x;
ball.y += e.y;
v = reflectVector(a);
return true;
}
return false;
}
function hitTestByIntersect(line) {
var a = new Vector2d(line.ePt.x - line.sPt.x, line.ePt.y - line.sPt.y);
var b = new Vector2d(line.sPt.x - (ball.x - v.x), line.sPt.y - (ball.y - v.y));
var c = v.perp(false);
var t = c.dot(b) / a.dot(c) * -1;
if( t < 0 || t > 1) return false;
var d = b.scale(-1);
var e = a.perp(false);
var s = d.dot(e) / v.dot(e) * -1;
if(s < 0 || s > 1) return false;
var p = a.scale(t).add(b);
var f = v.sub(p);
ball.x -= f.x;
ball.y -= f.y;
var g = e.normalize().scale(ball.radius);
ball.x += g.x;
ball.y += g.y;
v = reflectVector(a);
return true;
}
function reflectVector(a) {
var b = a.perp(false).normalize();
var len = v.scale(-1).dot(b);
var n = b.scale(len);
var r = v.add(n).add(n).scale(friction);
return r;
}
function loop() {
context.clearRect(0, 0, canvas.width, canvas.height);
v.y += gravity;
ball.x += v.x;
ball.y += v.y;
for(var i = 0; i < lines.length; i++) {
if(!hitTestByDistance(lines[i])) {
if(hitTestByIntersect(lines[i])) {
}
}
}
render();
requestAnimationFrame(loop);
}
function render() {
ball.draw();
for(var i = 0; i < lines.length; i++) {
lines[i].draw();
}
}
</script>
</body>
</html>