|
1 | 1 | function myMap(value, fromLow, fromHigh, toLow, toHigh) { |
2 | | - return ((value - fromLow) * (toHigh - toLow)) / (fromHigh - fromLow) + toLow |
| 2 | + return ((value - fromLow) * (toHigh - toLow)) / (fromHigh - fromLow) + toLow |
3 | 3 | } |
4 | | -function getDist(x1, y1, x2, y2) { |
5 | | - return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) |
| 4 | +function getDist(a, b) { |
| 5 | + return (((a.position.x - b.position.x) ** 2 + (a.position.y - b.position.y) ** 2) ** (1 / 2)) |
| 6 | +} |
| 7 | + |
| 8 | + |
| 9 | +function triangulate(colliders) { |
| 10 | + for (let i = 0; i < colliders.length; i++) { |
| 11 | + colliders[i].display() |
| 12 | + colliders[i].move() |
| 13 | + } |
| 14 | + for (let i = 0; i < colliders.length; i++) { |
| 15 | + for (let j = i + 1; j < colliders.length; j++) { |
| 16 | + colliders[i].collide(colliders[j]) |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +function particleGenerator(index){ |
| 22 | + let rnd = () => Math.random(); |
| 23 | + let w = window.innerWidth; |
| 24 | + let h = window.innerHeight; |
| 25 | + let size = 10; |
| 26 | + let position = new Vector(rnd() * w, rnd() * h); |
| 27 | + let velocity; |
| 28 | + if(position.y < 450) { |
| 29 | + velocity = new Vector(2,rnd()); |
| 30 | + } |
| 31 | + else if (position.y > 450 && position.y < 500) { |
| 32 | + // position = new Vector(position.x, position.y + 200); |
| 33 | + velocity = new Vector(20, rnd()); |
| 34 | + } |
| 35 | + else { |
| 36 | + velocity = new Vector(2, rnd()); |
| 37 | + } |
| 38 | + return new Particle(size, position, velocity); |
6 | 39 | } |
7 | 40 | class Vector { |
8 | | - constructor(x, y) { |
9 | | - this.x = x |
10 | | - this.y = y |
11 | | - } |
12 | | - add(v) { |
13 | | - this.x += v.x |
14 | | - this.y += v.y |
15 | | - } |
16 | | - sub(v) { |
17 | | - this.x -= v.x |
18 | | - this.y -= v.y |
19 | | - } |
| 41 | + constructor(x, y) { |
| 42 | + this.x = x |
| 43 | + this.y = y |
| 44 | + } |
| 45 | + add(v) { |
| 46 | + this.x += v.x |
| 47 | + this.y += v.y |
| 48 | + } |
| 49 | + sub(v) { |
| 50 | + this.x -= v.x |
| 51 | + this.y -= v.y |
| 52 | + } |
20 | 53 | } |
21 | 54 | var programCode = function (processingInstance) { |
22 | | - with (processingInstance) { |
23 | | - size(window.innerWidth, window.innerHeight) |
24 | | - frameRate(30) |
25 | | - // |
| 55 | + with (processingInstance) { |
| 56 | + size(window.innerWidth, window.innerHeight) |
| 57 | + frameRate(30) |
| 58 | + |
| 59 | + class Particle { |
| 60 | + constructor(size, position, velocity) { |
| 61 | + this.size = size |
| 62 | + this.position = position |
| 63 | + this.velocity = velocity |
| 64 | + } |
| 65 | + display() { |
| 66 | + noStroke(); |
| 67 | + colorMode(RGB, 255); |
| 68 | + let speed = Math.sqrt(this.velocity.x ** 2 + this.velocity.y ** 2); |
| 69 | + let red = myMap(speed, 0, 10, 0, 255); // later change to speedRed = myMap(this.velocity.x, 0, 10, 0, 255) |
| 70 | + let blue = myMap(speed, 0, 10, 255, 0); // later change to speedBlue = myMap(this.velocity.x, 0, 10, 0, 255) |
| 71 | + fill(red, 0, blue); |
| 72 | + ellipse( |
| 73 | + this.position.x, |
| 74 | + this.position.y, |
| 75 | + this.size, |
| 76 | + this.size |
| 77 | + ); |
| 78 | + } |
| 79 | + move() { |
| 80 | + if (this.position.x > width || this.position.x < 0) { |
| 81 | + this.position.x = 0; |
| 82 | + // this.velocity.x = -this.velocity.x; |
| 83 | + } |
| 84 | + if (this.position.y > height || this.position.y < 0) { |
| 85 | + // this.position.y = 0; |
| 86 | + this.velocity.y = -this.velocity.y; |
| 87 | + } |
| 88 | + this.position.add(this.velocity); |
| 89 | + } |
| 90 | + collide(other) { |
| 91 | + if (getDist(this, other) <= this.size / 2 + other.size / 2) { |
| 92 | + var normal = new Vector( |
| 93 | + (other.position.x - this.position.x) / |
| 94 | + Math.hypot( |
| 95 | + other.position.x - this.position.x, |
| 96 | + other.position.y - this.position.y |
| 97 | + ), |
| 98 | + (other.position.y - this.position.y) / |
| 99 | + Math.hypot( |
| 100 | + other.position.x - this.position.x, |
| 101 | + other.position.y - this.position.y |
| 102 | + ) |
| 103 | + ) |
| 104 | + |
| 105 | + |
| 106 | + var overlap = (getDist(this, other) - (this.size / 2 + other.size / 2)) / 2 |
| 107 | + var backoff = new Vector(normal.x * overlap, normal.y * overlap) |
| 108 | + this.position.add(backoff) |
| 109 | + other.position.sub(backoff) |
| 110 | + |
| 111 | + var relative = new Vector(this.velocity.x - other.velocity.x, this.velocity.y - other.velocity.y) |
| 112 | + var influence = relative.x * normal.x + relative.y * normal.y |
| 113 | + var delta = new Vector(influence * normal.x, influence * normal.y); |
| 114 | + this.velocity.sub(delta); |
| 115 | + other.velocity.add(delta); |
| 116 | + console.log("Collide"); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + let particles = []; |
26 | 122 |
|
27 | | - class Particle { |
28 | | - constructor(p, v) { |
29 | | - this.p = p |
30 | | - this.v = v |
31 | | - } |
32 | | - display() { |
33 | | - // ellipse(this.p.x, this.p.y, 10, 10) |
34 | | - textSize(this.v.x*4); |
35 | | - text(`${this.v.x}, ${this.v.y}`, this.p.x, this.p.y); |
36 | | - // fill(0,myMap(this.v.x, 0, 1, 0, 255), 0) |
37 | | - // if (this.v.x >= 0 && this.v.x <= 7) { |
38 | | - // fill(78, 0, 0) |
39 | | - // } else if (this.v.x > 7) { |
40 | | - // fill(0, 0, 255) |
41 | | - // }else{ |
42 | | - // fill(0, 255, 0) |
43 | | - // } |
44 | | - //change the color of the text based on the x value of the velocity vector |
45 | | - noStroke() |
46 | | - colorMode(RGB, 10) |
47 | | - fill(this.v.x,0, this.v.x) |
48 | | - } |
49 | | - move() { |
50 | | - this.p.x += this.v.x |
51 | | - this.p.y += this.v.y |
52 | | - if (this.p.x > width) { |
53 | | - this.p.x = 0 |
54 | | - } |
55 | | - } |
56 | | - checkCollision() { |
57 | | - for (let i = 0; i < particles.length; i++) { |
58 | | - let other = particles[i] |
59 | | - if (this !== other) { |
60 | | - if ( |
61 | | - getDist(this.p.x, this.p.y, other.p.x, other.p.y) < |
62 | | - 10 |
63 | | - ) { |
64 | | - // this.v.add(other.v); |
65 | | - // other.v.add(this.v); |
66 | | - } |
67 | | - } |
68 | | - } |
69 | | - } |
70 | | - } |
71 | | - let particles = [] |
72 | | - for (let k = 0; k < 50; k++) { |
73 | | - vector = new Vector(Math.round(Math.random() * 1000)/100, 0) |
74 | | - position = new Vector(10, Math.random() * window.innerHeight) |
75 | | - particles.push(new Particle(position, vector)) |
76 | | - } |
| 123 | + for (let k = 0; k < 500; k++) { |
| 124 | + particles.push(particleGenerator(k)); |
| 125 | + } |
77 | 126 |
|
78 | | - draw = function () { |
79 | | - background(0, 0, 0) |
80 | | - for (let myP = 0; myP < particles.length; myP++) { |
81 | | - const myParticle = particles[myP] |
82 | | - myParticle.display() |
83 | | - myParticle.move() |
84 | | - myParticle.checkCollision() |
85 | | - } |
| 127 | + draw = function () { |
| 128 | + background(0, 0, 0); |
| 129 | + triangulate(particles); |
| 130 | + } |
86 | 131 |
|
87 | | - // x = Math.random()*1000 |
88 | | - // y = Math.random()*1000 |
89 | | - } |
90 | 132 |
|
91 | | - // |
92 | | - } |
| 133 | + } |
93 | 134 | } |
94 | 135 |
|
95 | | -// Get the canvas that ProcessingJS will use |
96 | 136 | var canvas = document.getElementById("mycanvas") |
97 | | -// Pass the function to ProcessingJS constructor |
98 | 137 | var processingInstance = new Processing(canvas, programCode) |
0 commit comments