-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels4.js
More file actions
147 lines (136 loc) · 3.43 KB
/
models4.js
File metadata and controls
147 lines (136 loc) · 3.43 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
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
var twoPI = 2 * Math.PI;
function Agent(p, r, m, v, c)
{
this.p = p;
this.r = r;
this.m = m;
this.v = v;
this.p0 = new Point(0, 0);
this.color = c;
}
Agent.prototype.reset = function ()
{
this.p = this.p0;
}
Agent.prototype.move = function (friction)
{
// calculate direction of friction
var frictionVector = new Vector(0, 0);
if (this.v.mag() > 0)
{
frictionVector = this.v.unit().scale(-1.0 * friction * this.r / this.m);
}
// augment velocity based on friction constant and radio of objects radius/mass (the idea being radius corresponds to wind resistance, and mass corresponds to inertia)
if (frictionVector.mag() > this.v.mag())
{
this.v.x = 0;
this.v.y = 0;
}
else
{
this.v = this.v.add(frictionVector);
}
// update position
this.p0 = this.p;
this.p = this.p.add(this.v);
}
Agent.prototype.draw = function(ctx)
{
ctx.beginPath();
var grd = ctx.createRadialGradient(this.p.x, this.p.y, 0, this.p.x, this.p.y, this.r);
grd.addColorStop(1, "black");
grd.addColorStop(.5, this.color);
ctx.fillStyle = grd;
ctx.arc(this.p.x, this.p.y, this.r, 0, twoPI);
ctx.closePath();
ctx.fill();
}
function Pilot(p, r, m, v, c)
{
Agent.call(this, p, r, m, v, c);
this.heading = 0.0;
}
Pilot.prototype = new Agent();
Pilot.prototype.draw = function (ctx)
{
ctx.save();
{
// transform base matrix
ctx.translate(this.p.x, this.p.y);
ctx.rotate(this.heading);
ctx.beginPath();
{
var grd = ctx.createLinearGradient(-this.r, 0, this.r, 0);
grd.addColorStop(0, "black");
grd.addColorStop(0.3, this.color);
ctx.fillStyle = grd;
ctx.moveTo(-this.r, -this.r);
ctx.lineTo(-this.r, this.r);
ctx.lineTo(this.r, 0);
}
ctx.closePath();
ctx.fill();
}
ctx.restore();
}
function Structure(points)
{
this.walls = [];
this.points = points;
walls[0] = new Wall(points[points.length-1], points[0]);
wallCount = 1;
while (wallCount < points.length - 1)
{
this.walls[wallCount] = new Wall(points[wallCount], points[wallCount-1]);
}
}
function Collision(obj, dist)
{
this.obj = obj;
this.dist = dist;
}
function Wall(p1, p2)
{
this.p1 = p1;
this.p2 = p2;
this.vector = this.p2.sub(this.p1);
this.norm = this.vector.norm();
this.length = this.vector.mag();
}
Wall.prototype.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();
}
Wall.prototype.checkCollision = function(agent)
{
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);
var dist = Math.sqrt(d1 * d1 - L1 * L1);
if (dist < agent.r)
{
return new Collision(this, dist);
}
}
else
{
// ball is outside bounds of line, so check for apex collision
if (d1 < agent.r && d1 < d2)
{
return new Collision(this.p1, d1);
}
else if (d2 < agent.r)
{
return new Collision(this.p2, d2);
}
}
return null;
}