-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grower.js
139 lines (111 loc) · 3.9 KB
/
Grower.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
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
function Grower() {
this.symbols = [];
this.update = function () {
this.updateForce();
this.grow();
}
this.grow = function () {
for (let i = 0; i < this.symbols.length; i++) {
if (!this.symbols[i].outOfBounds && !this.symbols[i].overalapping) {
this.symbols[i].w += growthFactor
}
}
}
this.updateForce = function () {
for (let i = 0; i < this.symbols.length; i++) {
this.symbols[i].pos.add(this.symbols[i].vel)
this.symbols[i].vel.add(this.symbols[i].acc);
this.symbols[i].acc = createVector(0, 0);
}
}
this.applyForce = function (symbol, force) {
symbol.acc.add(force);
}
this.receiveCard = function (packedCard) {
this.symbols = packedCard
}
this.initialCardGrowth = function () {
for (let i = 0; i < this.symbols.length; i++) {
let growthTries = 0;
let growthMaxTries = 50000;
while (!this.symbolIsOutOfBounds(this.symbols[i]) && !this.symbolsOverlappingDuringGrowth(this.symbols[i], this.symbols, i)) {
if (this.symbols[i].w > this.symbolW + this.symbolOffsetSize || growthTries > growthMaxTries) {
print("grown");
break;
}
this.symbols[i].w += growthFactor;
this.symbols[i].pos.add(this.symbols[i].separationForce);
growthTries += 1;
if (this.symbolIsOutOfBounds(this.symbols[i]) || this.symbolsOverlappingDuringGrowth(this.symbols[i], this.symbols, i)) {
}
}
}
}
this.symbolsOverlappingDuringGrowth = function (currentSymbol, symbolArr, idx) {
//Overalapping during growth function
for (let i = 0; i < symbolArr.length; i++) {
for (let j = 0; j < symbolArr.length; j++) {
if (j != idx) {
let d = dist(currentSymbol.pos.x, currentSymbol.pos.y, symbolArr[j].pos.x, symbolArr[j].pos.y)
if (d < ((currentSymbol.w / 2 * 1.414) + (symbolArr[j].w / 2 * 1.414))) {
symbolArr[j].d = d;
currentSymbol.overlappingSymbol = symbolArr[j]
return true
}
}
}
}
return false
}
this.symbolIsOutOfBounds = function (symbol) {
let d = dist(width / 2, height / 2, symbol.pos.x, symbol.pos.y);
if (d > (width / 2) - (symbol.w / 2 * 1.414)) {
return true
} else {
return false;
}
}
this.growthTesting = function () {
//check, move
//overlapping check
for (let i = 0; i < this.symbols.length; i++) {
if (this.symbolsOverlappingDuringGrowth(this.symbols[i], this.symbols, i)) {
this.symbols[i].overalapping = true;
let separationVector = p5.Vector.sub(this.symbols[i].pos, this.symbols[i].overlappingSymbol.pos)
separationVector.normalize()
separationVector.div(pow(this.symbols[i].overlappingSymbol.d, 2));
separationVector.normalize()
this.symbols[i].vel = separationVector
} else if (this.notTouchingAnySymbols()) {
this.symbols[i].overalapping = false;
}
//out of bounds check
if (this.symbolIsOutOfBounds(this.symbols[i])) {
this.symbols[i].outOfBounds = true;
let centre = createVector(width/2, height/2);
let centripetalForce = p5.Vector.sub(centre, this.symbols[i].pos)
centripetalForce.normalize();
this.symbols[i].vel = centripetalForce;
// this.symbols[i].vel.mult(-1, -1);
} else {
this.symbols[i].outOfBounds = false;
}
}
}
this.notTouchingAnySymbols = function () {
for (let i = 0; i < this.symbols.length; i++) {
for (let j = 0; j < this.symbols.length; j++) {
if (i != j) {
let d = dist(this.symbols[i].pos.x, this.symbols[i].pos.y, this.symbols[j].pos.x, this.symbols[j].pos.y)
if (d < this.symbols[i].w / 2 * 1.414 + this.symbols[j].w / 2 * 1.414) {
return false;
}
}
}
}
return true;
}
this.getCardSymbols = function () {
return this.symbols
}
}