-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisual-cannon.js
55 lines (45 loc) · 1.28 KB
/
visual-cannon.js
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
import { point } from './point.js';
import { vector } from './vector.js';
import { canvas } from './canvas.js';
import { color } from './color.js';
class Projectile {
position; //point
velocity; //vector
constructor(position, velocity) {
this.position = position;
this.velocity = velocity;
}
}
class Environment {
gravity; //vector
wind; //vector
constructor(gravity, wind) {
this.gravity = gravity;
this.wind = wind;
}
}
function tick(env, proj) {
let position = proj.position.add(proj.velocity);
let velocity = proj.velocity.add(env.gravity).add(env.wind);
return new Projectile(position, velocity);
}
// projectile starts one unit above the origin.
// velocity is normalized to 1 unit/tick
let start = point(0, 1, 0);
let velocity = vector(1, 1.8, 0).normalize().multiplyBy(11.25) ;
let p = new Projectile(start, velocity);
// gravity -0.1 unit/tick, and wind is -0.01 unit/tick
let gravity = vector(0, -0.1, 0);
let wind = vector(-0.01, 0, 0);
let e = new Environment(gravity, wind);
let c = canvas(900, 500);
let x = 0;
let y = 500 - Math.round(p.position.y);
c.write_pixel(x, y, color(1, 0, 0));
while(p.position.y >= 0) {
p = tick(e, p);
x += 1;
y = 500 - Math.round(p.position.y);
c.write_pixel(x, y, color(1, 0, 0));
}
console.log(c.canvas_to_ppm())