-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
251 lines (220 loc) · 8.22 KB
/
index.html
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Molecular Dynamics Simulation</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.min.js"></script>
<script>
// Define global variables
var particles = [];
var numParticles = 20;
var wallFriction = 0.5;
var particleFriction = 0.5;
// Define DNA class
class DNA {
constructor(type, bondStrength, bondLength, maxConnections) {
this.type = type;
this.bondStrength = bondStrength;
this.bondLength = bondLength;
this.maxConnections = maxConnections;
}
}
// Define Particle class
class Particle {
constructor(position, velocity, mass, radius, dna) {
this.position = position;
this.velocity = velocity;
this.mass = mass;
this.radius = radius;
this.dna = dna;
this.bonds = []; // Array to store bonded particles
this.collisionForce = createVector(0, 0);
}
// Define update method to update position and velocity of particle
update() {
// Update position based on velocity
this.position.add(this.velocity);
// Handle wall collisions
if (this.position.x < this.radius) {
this.position.x = this.radius;
this.velocity.x *= -wallFriction;
this.position.x += random(0.1, 0.5); // Add random displacement to nudge away from the wall
} else if (this.position.x > width - this.radius) {
this.position.x = width - this.radius;
this.velocity.x *= -wallFriction;
this.position.x -= random(0.1, 0.5); // Add random displacement to nudge away from the wall
}
if (this.position.y < this.radius) {
this.position.y = this.radius;
this.velocity.y *= -wallFriction;
this.position.y += random(0.1, 0.5); // Add random displacement to nudge away from the wall
} else if (this.position.y > height - this.radius) {
this.position.y = height - this.radius;
this.velocity.y *= -wallFriction;
this.position.y -= random(0.1, 0.5); // Add random displacement to nudge away from the wall
}
// Update collision forces
this.collisionForce.mult(particleFriction);
this.velocity.add(this.collisionForce);
this.collisionForce.set(0, 0);
// Handle particle-particle interactions
for (var i = 0; i < particles.length; i++) {
if (particles[i] !== this) {
var distance = p5.Vector.dist(
this.position,
particles[i].position
);
// Check if particles are within bonding distance
if (
distance < this.radius + particles[i].radius &&
!this.isBonded(particles[i])
) {
// Handle chemical reactions based on DNA
if (this.canFormBond(particles[i])) {
this.formBond(particles[i]);
} else {
this.handleCollision(particles[i]);
}
}
}
}
}
// Check if the particle can form a bond with the given particle based on their DNA
canFormBond(particle) {
if (
(this.dna.type === "A" && particle.dna.type === "B") ||
(this.dna.type === "B" && particle.dna.type === "A")
) {
return true;
}
return false;
}
// Check if the particle is already bonded with the given particle
isBonded(particle) {
return this.bonds.includes(particle);
}
// Form a bond with the given particle
formBond(particle) {
if (
this.bonds.length < this.dna.maxConnections &&
particle.bonds.length < particle.dna.maxConnections &&
this.bonds.length + particle.bonds.length < this.dna.maxConnections
) {
this.bonds.push(particle);
particle.bonds.push(this);
}
}
// Handle collision with another particle
handleCollision(particle) {
var force = p5.Vector.sub(particle.position, this.position);
var distance = force.mag();
force.normalize();
// Calculate collision force based on masses and distances
var collisionForce =
(this.mass * particle.mass) / (distance * distance);
force.mult(collisionForce);
// Apply forces to particles
this.collisionForce.add(force);
particle.collisionForce.sub(force);
}
// Define draw method to draw particle on canvas
draw() {
if (this.dna.type === "A") {
fill(255, 0, 0);
} else {
fill(0, 255, 0);
}
ellipse(
this.position.x,
this.position.y,
this.radius * 2,
this.radius * 2
);
// Draw bonds
stroke(255);
for (var i = 0; i < this.bonds.length; i++) {
line(
this.position.x,
this.position.y,
this.bonds[i].position.x,
this.bonds[i].position.y
);
}
}
}
// Define setup function to initialize simulation
function setup() {
createCanvas(400, 400);
// Create initial particles and add to array
for (var i = 0; i < numParticles; i++) {
var position = createVector(random(width), random(height));
var velocity = createVector(random(-1, 1), random(-1, 1));
var mass = random(5, 10);
var radius = mass / 2;
var dna = createDNA();
particles.push(new Particle(position, velocity, mass, radius, dna));
}
}
// Create a random DNA for each particle
function createDNA() {
var type = random() < 0.5 ? "A" : "B";
var bondStrength = random(0.001, 0.01);
var bondLength = random(5, 10);
var maxConnections = floor(random(1, 4)); // Maximum number of connections for a particle
return new DNA(type, bondStrength, bondLength, maxConnections);
}
// Define draw function to update simulation at each frame
function draw() {
background(0);
// Update and draw each particle
for (var i = 0; i < particles.length; i++) {
particles[i].update();
particles[i].draw();
}
// Check bond collisions
for (var i = 0; i < particles.length; i++) {
for (var j = 0; j < particles[i].bonds.length; j++) {
var bond = particles[i].bonds[j];
bondCollision(particles[i], bond);
}
}
}
// Handle bond collisions
for (var i = 0; i < particles.length; i++) {
for (var j = 0; j < particles[i].bonds.length; j++) {
var bond = particles[i].bonds[j];
bondCollision(particles[i], bond);
}
}
// Handle collision between bonded particles
function bondCollision(p1, p2) {
var force = p5.Vector.sub(p2.position, p1.position);
var distance = force.mag();
var maxBondLength = p1.dna.bondLength; // Maximum bond length from particle's DNA
if (distance < p1.radius + p2.radius) {
force.normalize();
var overlap = (p1.radius + p2.radius) - distance;
force.mult(overlap);
// Apply forces to particles
p1.position.sub(p5.Vector.mult(force, p1.mass / (p1.mass + p2.mass)));
p2.position.add(p5.Vector.mult(force, p2.mass / (p1.mass + p2.mass)));
}
// Limit bond length if it exceeds maximum bond length
if (distance > maxBondLength) {
var displacement = force.mult((distance - maxBondLength) / distance);
// Apply restorative force to bring particles closer
p1.position.add(displacement.mult(p1.mass / (p1.mass + p2.mass)));
p2.position.sub(displacement.mult(p2.mass / (p1.mass + p2.mass)));
}
// Update mass of the bonded structure
var totalMass = p1.mass + p2.mass;
p1.bondedMass = totalMass;
p2.bondedMass = totalMass;
}
</script>
</head>
<body>
<h1>Evolving Life</h1>
<p>This is a simulation of evolving life in HTML with JavaScript.</p>
</body>
</html>