Spaces:
Running
Running
Update index.html
Browse files- index.html +178 -19
index.html
CHANGED
@@ -31,6 +31,10 @@
|
|
31 |
|
32 |
let round = 0;
|
33 |
|
|
|
|
|
|
|
|
|
34 |
const createScene = function () {
|
35 |
const scene = new BABYLON.Scene(engine);
|
36 |
const camera = new BABYLON.ArcRotateCamera("camera", -Math.PI / 2, Math.PI / 2.5, 3, new BABYLON.Vector3(0, 0, 0), scene);
|
@@ -41,13 +45,74 @@
|
|
41 |
// Ground
|
42 |
const ground = BABYLON.MeshBuilder.CreateGround("ground", {width: 10, height: 10}, scene);
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
// Elephants
|
45 |
let elephants = [];
|
46 |
for (let i = 0; i < 5; i++) {
|
47 |
const elephant = BABYLON.MeshBuilder.CreateSphere("elephant", {diameter: 0.5}, scene);
|
48 |
elephant.position.y = 0.5;
|
49 |
-
elephant.position.x = Math.random() *
|
50 |
-
elephant.position.z =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
elephants.push(elephant);
|
52 |
}
|
53 |
|
@@ -56,8 +121,18 @@
|
|
56 |
for (let i = 0; i < 10; i++) {
|
57 |
const p = BABYLON.MeshBuilder.CreateSphere("prey", {diameter: 0.3}, scene);
|
58 |
p.position.y = 0.3;
|
59 |
-
p.position.x = Math.random() *
|
60 |
-
p.position.z =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
prey.push(p);
|
62 |
}
|
63 |
|
@@ -66,8 +141,8 @@
|
|
66 |
for (let i = 0; i < 20; i++) {
|
67 |
const g = BABYLON.MeshBuilder.CreateBox("grass", {size: 0.2}, scene);
|
68 |
g.position.y = 0.1;
|
69 |
-
g.position.x = Math.random() *
|
70 |
-
g.position.z =
|
71 |
g.material = new BABYLON.StandardMaterial("grassMat", scene);
|
72 |
g.material.diffuseColor = new BABYLON.Color3(0, 1, 0); // Green color
|
73 |
grass.push(g);
|
@@ -82,12 +157,31 @@
|
|
82 |
|
83 |
const direction = nearestPrey.position.subtract(elephant.position);
|
84 |
direction.normalize();
|
85 |
-
elephant.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
if (BABYLON.Vector3.Distance(elephant.position, nearestPrey.position) < 0.5) {
|
88 |
// Prey is eaten
|
89 |
-
nearestPrey.position.x = Math.random() *
|
90 |
-
nearestPrey.position.
|
91 |
}
|
92 |
});
|
93 |
});
|
@@ -95,8 +189,43 @@
|
|
95 |
// Prey behavior
|
96 |
scene.registerBeforeRender(function () {
|
97 |
prey.forEach(p => {
|
98 |
-
const
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
});
|
101 |
});
|
102 |
|
@@ -132,16 +261,26 @@
|
|
132 |
// Mutation
|
133 |
if (elephantFitness[i] > 10) {
|
134 |
elephantFitness[i] = 0;
|
135 |
-
elephant.position.x = Math.random() *
|
136 |
-
elephant.position.
|
|
|
|
|
|
|
|
|
|
|
137 |
}
|
138 |
});
|
139 |
|
140 |
prey.forEach((p, i) => {
|
141 |
if (preyFitness[i] < -10) {
|
142 |
preyFitness[i] = 0;
|
143 |
-
p.position.x = Math.random() *
|
144 |
-
p.position.
|
|
|
|
|
|
|
|
|
|
|
145 |
}
|
146 |
});
|
147 |
|
@@ -155,8 +294,16 @@
|
|
155 |
for (let i = 0; i < 2; i++) {
|
156 |
const newElephant = BABYLON.MeshBuilder.CreateSphere("elephant", {diameter: 0.5}, scene);
|
157 |
newElephant.position.y = 0.5;
|
158 |
-
newElephant.position.x = Math.random() *
|
159 |
-
newElephant.position.z =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
elephants.push(newElephant);
|
161 |
elephantFitness.push(0);
|
162 |
}
|
@@ -165,8 +312,18 @@
|
|
165 |
for (let i = 0; i < 2; i++) {
|
166 |
const newPrey = BABYLON.MeshBuilder.CreateSphere("prey", {diameter: 0.3}, scene);
|
167 |
newPrey.position.y = 0.3;
|
168 |
-
newPrey.position.x = Math.random() *
|
169 |
-
newPrey.position.z =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
prey.push(newPrey);
|
171 |
preyFitness.push(0);
|
172 |
}
|
@@ -186,6 +343,8 @@
|
|
186 |
});
|
187 |
|
188 |
window.addEventListener("resize", function () {
|
|
|
|
|
189 |
engine.resize();
|
190 |
});
|
191 |
</script>
|
|
|
31 |
|
32 |
let round = 0;
|
33 |
|
34 |
+
// Set canvas size to match window size
|
35 |
+
canvas.width = window.innerWidth;
|
36 |
+
canvas.height = window.innerHeight;
|
37 |
+
|
38 |
const createScene = function () {
|
39 |
const scene = new BABYLON.Scene(engine);
|
40 |
const camera = new BABYLON.ArcRotateCamera("camera", -Math.PI / 2, Math.PI / 2.5, 3, new BABYLON.Vector3(0, 0, 0), scene);
|
|
|
45 |
// Ground
|
46 |
const ground = BABYLON.MeshBuilder.CreateGround("ground", {width: 10, height: 10}, scene);
|
47 |
|
48 |
+
// Boundary
|
49 |
+
const boundary = {
|
50 |
+
minX: -5,
|
51 |
+
maxX: 5,
|
52 |
+
minZ: -5,
|
53 |
+
maxZ: 5,
|
54 |
+
minY: 0,
|
55 |
+
maxY: 2 // Ceiling height
|
56 |
+
};
|
57 |
+
|
58 |
+
// Walls
|
59 |
+
const wallHeight = 2;
|
60 |
+
const wallThickness = 0.1;
|
61 |
+
const walls = [];
|
62 |
+
|
63 |
+
// Left wall
|
64 |
+
const leftWall = BABYLON.MeshBuilder.CreateBox("leftWall", {width: wallThickness, height: wallHeight, depth: boundary.maxZ - boundary.minZ}, scene);
|
65 |
+
leftWall.position.x = boundary.minX - wallThickness / 2;
|
66 |
+
leftWall.position.y = wallHeight / 2;
|
67 |
+
leftWall.position.z = (boundary.maxZ + boundary.minZ) / 2;
|
68 |
+
walls.push(leftWall);
|
69 |
+
|
70 |
+
// Right wall
|
71 |
+
const rightWall = BABYLON.MeshBuilder.CreateBox("rightWall", {width: wallThickness, height: wallHeight, depth: boundary.maxZ - boundary.minZ}, scene);
|
72 |
+
rightWall.position.x = boundary.maxX + wallThickness / 2;
|
73 |
+
rightWall.position.y = wallHeight / 2;
|
74 |
+
rightWall.position.z = (boundary.maxZ + boundary.minZ) / 2;
|
75 |
+
walls.push(rightWall);
|
76 |
+
|
77 |
+
// Front wall
|
78 |
+
const frontWall = BABYLON.MeshBuilder.CreateBox("frontWall", {width: boundary.maxX - boundary.minX, height: wallHeight, depth: wallThickness}, scene);
|
79 |
+
frontWall.position.x = (boundary.maxX + boundary.minX) / 2;
|
80 |
+
frontWall.position.y = wallHeight / 2;
|
81 |
+
frontWall.position.z = boundary.minZ - wallThickness / 2;
|
82 |
+
walls.push(frontWall);
|
83 |
+
|
84 |
+
// Back wall
|
85 |
+
const backWall = BABYLON.MeshBuilder.CreateBox("backWall", {width: boundary.maxX - boundary.minX, height: wallHeight, depth: wallThickness}, scene);
|
86 |
+
backWall.position.x = (boundary.maxX + boundary.minX) / 2;
|
87 |
+
backWall.position.y = wallHeight / 2;
|
88 |
+
backWall.position.z = boundary.maxZ + wallThickness / 2;
|
89 |
+
walls.push(backWall);
|
90 |
+
|
91 |
+
// Ceiling
|
92 |
+
const ceiling = BABYLON.MeshBuilder.CreatePlane("ceiling", {width: boundary.maxX - boundary.minX, height: boundary.maxZ - boundary.minZ}, scene);
|
93 |
+
ceiling.position.x = (boundary.maxX + boundary.minX) / 2;
|
94 |
+
ceiling.position.y = boundary.maxY;
|
95 |
+
ceiling.position.z = (boundary.maxZ + boundary.minZ) / 2;
|
96 |
+
ceiling.rotation.x = Math.PI / 2;
|
97 |
+
const ceilingMaterial = new BABYLON.StandardMaterial("ceilingMat", scene);
|
98 |
+
ceilingMaterial.alpha = 0.5; // Transparency
|
99 |
+
ceiling.material = ceilingMaterial;
|
100 |
+
|
101 |
// Elephants
|
102 |
let elephants = [];
|
103 |
for (let i = 0; i < 5; i++) {
|
104 |
const elephant = BABYLON.MeshBuilder.CreateSphere("elephant", {diameter: 0.5}, scene);
|
105 |
elephant.position.y = 0.5;
|
106 |
+
elephant.position.x = Math.random() * (boundary.maxX - boundary.minX) + boundary.minX;
|
107 |
+
elephant.position.z = 0; // Fixed Z position
|
108 |
+
elephant.behavior = {
|
109 |
+
speed: Math.random() * 0.02,
|
110 |
+
direction: new BABYLON.Vector3(Math.random() - 0.5, Math.random() - 0.5, 0).normalize(),
|
111 |
+
size: 0.5,
|
112 |
+
color: new BABYLON.Color3(Math.random(), Math.random(), Math.random())
|
113 |
+
};
|
114 |
+
elephant.material = new BABYLON.StandardMaterial("elephantMat", scene);
|
115 |
+
elephant.material.diffuseColor = elephant.behavior.color;
|
116 |
elephants.push(elephant);
|
117 |
}
|
118 |
|
|
|
121 |
for (let i = 0; i < 10; i++) {
|
122 |
const p = BABYLON.MeshBuilder.CreateSphere("prey", {diameter: 0.3}, scene);
|
123 |
p.position.y = 0.3;
|
124 |
+
p.position.x = Math.random() * (boundary.maxX - boundary.minX) + boundary.minX;
|
125 |
+
p.position.z = 0; // Fixed Z position
|
126 |
+
p.material = new BABYLON.StandardMaterial("preyMat", scene);
|
127 |
+
p.material.diffuseColor = new BABYLON.Color3(1, 0, 0); // Red color
|
128 |
+
p.behavior = {
|
129 |
+
speed: Math.random() * 0.02,
|
130 |
+
direction: new BABYLON.Vector3(Math.random() - 0.5, Math.random() - 0.5, 0).normalize(),
|
131 |
+
size: 0.3,
|
132 |
+
color: new BABYLON.Color3(Math.random(), Math.random(), Math.random())
|
133 |
+
};
|
134 |
+
p.material = new BABYLON.StandardMaterial("preyMat", scene);
|
135 |
+
p.material.diffuseColor = p.behavior.color;
|
136 |
prey.push(p);
|
137 |
}
|
138 |
|
|
|
141 |
for (let i = 0; i < 20; i++) {
|
142 |
const g = BABYLON.MeshBuilder.CreateBox("grass", {size: 0.2}, scene);
|
143 |
g.position.y = 0.1;
|
144 |
+
g.position.x = Math.random() * (boundary.maxX - boundary.minX) + boundary.minX;
|
145 |
+
g.position.z = 0; // Fixed Z position
|
146 |
g.material = new BABYLON.StandardMaterial("grassMat", scene);
|
147 |
g.material.diffuseColor = new BABYLON.Color3(0, 1, 0); // Green color
|
148 |
grass.push(g);
|
|
|
157 |
|
158 |
const direction = nearestPrey.position.subtract(elephant.position);
|
159 |
direction.normalize();
|
160 |
+
elephant.behavior.direction = direction;
|
161 |
+
elephant.position.addInPlace(elephant.behavior.direction.scale(elephant.behavior.speed));
|
162 |
+
|
163 |
+
// Boundary check
|
164 |
+
if (elephant.position.x < boundary.minX) elephant.position.x = boundary.minX;
|
165 |
+
if (elephant.position.x > boundary.maxX) elephant.position.x = boundary.maxX;
|
166 |
+
if (elephant.position.y < boundary.minY) elephant.position.y = boundary.minY;
|
167 |
+
if (elephant.position.y > boundary.maxY) elephant.position.y = boundary.maxY;
|
168 |
+
|
169 |
+
// Collision detection with walls
|
170 |
+
walls.forEach(wall => {
|
171 |
+
const boundingBox = wall.getBoundingInfo().boundingBox;
|
172 |
+
if (boundingBox.intersectsPoint(elephant.position)) {
|
173 |
+
// Adjust position to avoid clipping
|
174 |
+
if (wall === leftWall) elephant.position.x = boundary.minX + wallThickness / 2;
|
175 |
+
if (wall === rightWall) elephant.position.x = boundary.maxX - wallThickness / 2;
|
176 |
+
if (wall === frontWall) elephant.position.z = boundary.minZ + wallThickness / 2;
|
177 |
+
if (wall === backWall) elephant.position.z = boundary.maxZ - wallThickness / 2;
|
178 |
+
}
|
179 |
+
});
|
180 |
|
181 |
if (BABYLON.Vector3.Distance(elephant.position, nearestPrey.position) < 0.5) {
|
182 |
// Prey is eaten
|
183 |
+
nearestPrey.position.x = Math.random() * (boundary.maxX - boundary.minX) + boundary.minX;
|
184 |
+
nearestPrey.position.y = Math.random() * (boundary.maxY - boundary.minY) + boundary.minY;
|
185 |
}
|
186 |
});
|
187 |
});
|
|
|
189 |
// Prey behavior
|
190 |
scene.registerBeforeRender(function () {
|
191 |
prey.forEach(p => {
|
192 |
+
const nearestElephant = elephants.reduce((nearest, e) => {
|
193 |
+
return BABYLON.Vector3.Distance(p.position, e.position) < BABYLON.Vector3.Distance(p.position, nearest.position) ? e : nearest;
|
194 |
+
}, elephants[0]);
|
195 |
+
|
196 |
+
const direction = p.position.subtract(nearestElephant.position);
|
197 |
+
direction.normalize();
|
198 |
+
p.behavior.direction = direction;
|
199 |
+
p.position.addInPlace(p.behavior.direction.scale(p.behavior.speed));
|
200 |
+
|
201 |
+
// Boundary check
|
202 |
+
if (p.position.x < boundary.minX) p.position.x = boundary.minX;
|
203 |
+
if (p.position.x > boundary.maxX) p.position.x = boundary.maxX;
|
204 |
+
if (p.position.y < boundary.minY) p.position.y = boundary.minY;
|
205 |
+
if (p.position.y > boundary.maxY) p.position.y = boundary.maxY;
|
206 |
+
|
207 |
+
// Collision detection with walls
|
208 |
+
walls.forEach(wall => {
|
209 |
+
const boundingBox = wall.getBoundingInfo().boundingBox;
|
210 |
+
if (boundingBox.intersectsPoint(p.position)) {
|
211 |
+
// Adjust position to avoid clipping
|
212 |
+
if (wall === leftWall) p.position.x = boundary.minX + wallThickness / 2;
|
213 |
+
if (wall === rightWall) p.position.x = boundary.maxX - wallThickness / 2;
|
214 |
+
if (wall === frontWall) p.position.z = boundary.minZ + wallThickness / 2;
|
215 |
+
if (wall === backWall) p.position.z = boundary.maxZ - wallThickness / 2;
|
216 |
+
}
|
217 |
+
});
|
218 |
+
|
219 |
+
// Prey eats grass
|
220 |
+
const nearestGrass = grass.reduce((nearest, g) => {
|
221 |
+
return BABYLON.Vector3.Distance(p.position, g.position) < BABYLON.Vector3.Distance(p.position, nearest.position) ? g : nearest;
|
222 |
+
}, grass[0]);
|
223 |
+
|
224 |
+
if (BABYLON.Vector3.Distance(p.position, nearestGrass.position) < 0.3) {
|
225 |
+
// Grass is eaten
|
226 |
+
nearestGrass.position.x = Math.random() * (boundary.maxX - boundary.minX) + boundary.minX;
|
227 |
+
nearestGrass.position.y = Math.random() * (boundary.maxY - boundary.minY) + boundary.minY;
|
228 |
+
}
|
229 |
});
|
230 |
});
|
231 |
|
|
|
261 |
// Mutation
|
262 |
if (elephantFitness[i] > 10) {
|
263 |
elephantFitness[i] = 0;
|
264 |
+
elephant.position.x = Math.random() * (boundary.maxX - boundary.minX) + boundary.minX;
|
265 |
+
elephant.position.y = Math.random() * (boundary.maxY - boundary.minY) + boundary.minY;
|
266 |
+
elephant.behavior.speed = Math.random() * 0.02;
|
267 |
+
elephant.behavior.size = Math.random() * 0.5 + 0.1;
|
268 |
+
elephant.behavior.color = new BABYLON.Color3(Math.random(), Math.random(), Math.random());
|
269 |
+
elephant.material.diffuseColor = elephant.behavior.color;
|
270 |
+
elephant.scaling.setAll(elephant.behavior.size);
|
271 |
}
|
272 |
});
|
273 |
|
274 |
prey.forEach((p, i) => {
|
275 |
if (preyFitness[i] < -10) {
|
276 |
preyFitness[i] = 0;
|
277 |
+
p.position.x = Math.random() * (boundary.maxX - boundary.minX) + boundary.minX;
|
278 |
+
p.position.y = Math.random() * (boundary.maxY - boundary.minY) + boundary.minY;
|
279 |
+
p.behavior.speed = Math.random() * 0.02;
|
280 |
+
p.behavior.size = Math.random() * 0.3 + 0.1;
|
281 |
+
p.behavior.color = new BABYLON.Color3(Math.random(), Math.random(), Math.random());
|
282 |
+
p.material.diffuseColor = p.behavior.color;
|
283 |
+
p.scaling.setAll(p.behavior.size);
|
284 |
}
|
285 |
});
|
286 |
|
|
|
294 |
for (let i = 0; i < 2; i++) {
|
295 |
const newElephant = BABYLON.MeshBuilder.CreateSphere("elephant", {diameter: 0.5}, scene);
|
296 |
newElephant.position.y = 0.5;
|
297 |
+
newElephant.position.x = Math.random() * (boundary.maxX - boundary.minX) + boundary.minX;
|
298 |
+
newElephant.position.z = 0; // Fixed Z position
|
299 |
+
newElephant.behavior = {
|
300 |
+
speed: Math.random() * 0.02,
|
301 |
+
direction: new BABYLON.Vector3(Math.random() - 0.5, Math.random() - 0.5, 0).normalize(),
|
302 |
+
size: 0.5,
|
303 |
+
color: new BABYLON.Color3(Math.random(), Math.random(), Math.random())
|
304 |
+
};
|
305 |
+
newElephant.material = new BABYLON.StandardMaterial("elephantMat", scene);
|
306 |
+
newElephant.material.diffuseColor = newElephant.behavior.color;
|
307 |
elephants.push(newElephant);
|
308 |
elephantFitness.push(0);
|
309 |
}
|
|
|
312 |
for (let i = 0; i < 2; i++) {
|
313 |
const newPrey = BABYLON.MeshBuilder.CreateSphere("prey", {diameter: 0.3}, scene);
|
314 |
newPrey.position.y = 0.3;
|
315 |
+
newPrey.position.x = Math.random() * (boundary.maxX - boundary.minX) + boundary.minX;
|
316 |
+
newPrey.position.z = 0; // Fixed Z position
|
317 |
+
newPrey.material = new BABYLON.StandardMaterial("preyMat", scene);
|
318 |
+
newPrey.material.diffuseColor = new BABYLON.Color3(1, 0, 0); // Red color
|
319 |
+
newPrey.behavior = {
|
320 |
+
speed: Math.random() * 0.02,
|
321 |
+
direction: new BABYLON.Vector3(Math.random() - 0.5, Math.random() - 0.5, 0).normalize(),
|
322 |
+
size: 0.3,
|
323 |
+
color: new BABYLON.Color3(Math.random(), Math.random(), Math.random())
|
324 |
+
};
|
325 |
+
newPrey.material = new BABYLON.StandardMaterial("preyMat", scene);
|
326 |
+
newPrey.material.diffuseColor = newPrey.behavior.color;
|
327 |
prey.push(newPrey);
|
328 |
preyFitness.push(0);
|
329 |
}
|
|
|
343 |
});
|
344 |
|
345 |
window.addEventListener("resize", function () {
|
346 |
+
canvas.width = window.innerWidth;
|
347 |
+
canvas.height = window.innerHeight;
|
348 |
engine.resize();
|
349 |
});
|
350 |
</script>
|