-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.js
More file actions
67 lines (64 loc) · 1.56 KB
/
models.js
File metadata and controls
67 lines (64 loc) · 1.56 KB
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
var twoPI = 2 * Math.PI;
function ball(x, y, r, m, vx, vy, c)
{
this.p = new Vector(x, y);
this.r = r;
this.m = m;
this.v = new Vector(vx, vy);
this.p0 = new Vector(0, 0);
this.color = c;
}
ball.prototype = {
reset: function ()
{
this.p = this.p0;
},
move: function ()
{
this.p0 = this.p;
this.p = this.p.add(this.v);
},
draw: function(ctx)
{
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.arc(this.p.x, this.p.y, this.r, 0, twoPI);
ctx.closePath();
ctx.fill();
}
}
function wall(x1, y1, x2, y2)
{
this.p1 = new Vector(x1, y1);
this.p2 = new Vector(x2, y2);
this.vector = this.p2.sub(this.p1);
this.norm = this.vector.norm();
this.length = this.vector.mag();
}
wall.prototype = {
distance: function (p)
{
var d1 = this.p1.sub(p).mag();
var d2 = this.p2.sub(p).mag();
if (d1 < this.length && d2 < this.length)
{
// ball is within range, so check vertical distance
var L1 = (d1 * d1 - d2 * d2 + this.length * this.length) / (2 * this.length);
return Math.sqrt(d1 * d1 - L1 * L1);
}
else
{
// ball is outside bounds of line, so just take min of distances
return Math.min(d1, d2);
}
},
draw: function (ctx)
{
ctx.beginPath();
ctx.strokeStyle = 'black';
ctx.moveTo(this.p1.x, this.p1.y);
ctx.lineTo(this.p2.x, this.p2.y);
ctx.closePath();
ctx.stroke();
}
}