-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotion.js
More file actions
172 lines (140 loc) · 3.5 KB
/
motion.js
File metadata and controls
172 lines (140 loc) · 3.5 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
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
// information about window and cursor
var width, height, center;
var mouseX, mouseY;
// canvas and context
var canvas, ctx;
// environment
var movers;
var air;
var liquid;
var wind;
var gravity;
// framerate
var fps = 60;
/* Initialization and Update functions */
function initVector() {
// get canvas context
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
// store height and width of canvas
width = canvas.width;
height = canvas.height;
center = new Vector(width/2, height/2);
// initialize mouse position
// in case its used immediately
mouseX = center.x;
mouseY = center.y;
// keep track of mouse position
document.addEventListener("mousemove", onMouseMove, false);
// create movers
movers = [];
// initialize movers
for(var i = 1; i <= 5; i++) {
movers[i - 1] = new Mover(randomMintoMax(20, 45), i * 120, 50);
}
// wind, gravity, liquid
liquid = new Fluid(0, height/2, width, height/2, 2.35);
air = new Fluid(0, 0, width, height/2, .06);
air.fillStyle = "rgba(255, 255, 255, 0)";
// redraw at 1000 fps
setInterval(update, 1000/fps);
};
function update() {
ctx.clearRect(0, 0, width, height);
for(var i = 0; i < movers.length; i++) {
wind = new Vector(0.0, 0);
gravity = new Vector(0, .24 * movers[i].mass);
if(movers[i].isInside(liquid)) {
movers[i].drag(liquid);
} else {
movers[i].drag(air);
}
movers[i].applyForce(wind);
movers[i].applyForce(gravity);
movers[i].update();
movers[i].checkEdges();
movers[i].display();
}
air.display();
liquid.display();
};
function Mover(m, x, y) {
this.location = new Vector(x, y);
this.velocity = new Vector(0, 0);
this.acceleration = new Vector(0, 0);
this.mass = m;
this.update = function() {
this.velocity.add(this.acceleration);
this.location.add(this.velocity);
this.acceleration.mult(0);
};
this.display = function() {
ctx.fillStyle = "rgb(200, 0, 0)";
ctx.beginPath();
ctx.arc(this.location.x, this.location.y, this.mass, 0, 2 * Math.PI, false);
ctx.fill();
ctx.lineWidth = 3;
ctx.strokeStyle = "rgb(100, 100, 100)";
ctx.stroke();
};
this.checkEdges = function() {
if(this.location.x > width) {
this.location.x = width;
this.velocity.x *= -1;
} else if(this.location.x < 0) {
this.location.x = 0;
this.velocity.x *= -1;
}
if (this.location.y > height) {
this.location.y = height;
this.velocity.y *= -1;
}
};
// net force = mass * accceleration
// or
// acceleration = net force / mass
// for now, mass = 1
this.applyForce = function(force) {
var f = Vector.div(force, this.mass);
this.acceleration.add(f);
};
// check if inside a certain fluid
this.isInside = function(fluid) {
if( this.location.x > fluid.x &&
this.location.x < fluid.x + fluid.w &&
this.location.y > fluid.y &&
this.location.y < fluid.y + fluid.h) {
return true;
} else {
return false;
}
};
this.drag = function(fluid) {
var speed = this.velocity.mag();
var dragMag = fluid.c * speed * speed;
var drag = this.velocity.deepCopy();
drag.mult(-1);
drag.normalize();
drag.mult(dragMag);
this.applyForce(drag);
};
};
function drawVector() {
ctx.clearRect(0, 0, height * 2, width * 2);
var mouse = new Vector(mouseX, mouseY);
mouse.sub(center);
mouse.normalize();
mouse.mult(50);
/*
// Display magnitude
ctx.fillStyle = "(0, 0, 0)";
var m = mouse.mag();
ctx.fillRect(0, 0, m, 10);*/
ctx.save();
ctx.translate(center.x, center.y);
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(mouse.x, mouse.y);
ctx.stroke();
ctx.restore();
}