Update index.html
Browse files- index.html +192 -157
index.html
CHANGED
@@ -1,167 +1,202 @@
|
|
1 |
<!DOCTYPE html>
|
2 |
<html>
|
3 |
<head>
|
4 |
-
<title>Finite State Machine Demo with 3D Bird Swarm</title>
|
5 |
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
|
6 |
</head>
|
7 |
<body>
|
8 |
<a-scene>
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
<a-
|
14 |
-
|
15 |
-
|
16 |
-
<a-
|
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 |
-
var target = event.target;
|
109 |
-
target.setAttribute('animation__click', 'property: position; to: 0 2.5 -5; dur: 1000');
|
110 |
-
},
|
111 |
-
onRelease: function (event, from, to) {
|
112 |
-
var target = event.target;
|
113 |
-
target.removeAttribute('animation__click');
|
114 |
-
var birdInterval = setInterval(function () {
|
115 |
-
var birdPosition = target.getAttribute('position');
|
116 |
-
var randomX = Math.random() * 0.4 - 0.2;
|
117 |
-
var randomY = Math.random() * 0.4 - 0.2;
|
118 |
-
var randomZ = Math.random() * 0.4 - 0.2;
|
119 |
-
target.setAttribute('animation', 'property: position; to: ' + (birdPosition.x + randomX) + ' ' + (birdPosition.y + randomY) + ' ' + (birdPosition.z + randomZ) + '; dur: 1000');
|
120 |
-
}, 1000);
|
121 |
-
}
|
122 |
-
}
|
123 |
-
});
|
124 |
-
|
125 |
-
function updateBirdPosition () {
|
126 |
-
var playerPosition = el.getAttribute('position');
|
127 |
-
birdSwarm.setAttribute('position', playerPosition.x + ' ' + playerPosition.y + ' ' + (playerPosition.z - 5));
|
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 |
</body>
|
167 |
-
</html>
|
|
|
1 |
<!DOCTYPE html>
|
2 |
<html>
|
3 |
<head>
|
|
|
4 |
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
|
5 |
</head>
|
6 |
<body>
|
7 |
<a-scene>
|
8 |
+
<!-- Camera -->
|
9 |
+
<a-entity camera look-controls position="0 2 6"></a-entity>
|
10 |
+
|
11 |
+
<!-- Field -->
|
12 |
+
<a-box color="green" position="0 -0.5 -10" width="16" height="0.5" depth="26"></a-box>
|
13 |
+
|
14 |
+
<!-- Ball -->
|
15 |
+
<a-sphere id="ball" color="white" radius="0.2" position="0 0.2 0"></a-sphere>
|
16 |
+
|
17 |
+
<!-- Particle Systems -->
|
18 |
+
<!-- Team 1 -->
|
19 |
+
<a-entity particle-system="color: #FFC107; accelerationValue: -9.8; accelerationSpread: 2; maxAge: 2; size: 0.1; particleCount: 4;" position="-2 0.5 -3"></a-entity>
|
20 |
+
<a-entity particle-system="color: #FFC107; accelerationValue: -9.8; accelerationSpread: 2; maxAge: 2; size: 0.1; particleCount: 4;" position="2 0.5 -3"></a-entity>
|
21 |
+
<a-entity particle-system="color: #FFC107; accelerationValue: -9.8; accelerationSpread: 2; maxAge: 2; size: 0.1; particleCount: 4;" position="-2 0.5 -7"></a-entity>
|
22 |
+
<a-entity particle-system="color: #FFC107; accelerationValue: -9.8; accelerationSpread: 2; maxAge: 2; size: 0.1; particleCount: 4;" position="2 0.5 -7"></a-entity>
|
23 |
+
|
24 |
+
<!-- Team 2 -->
|
25 |
+
<a-entity particle-system="color: #2196F3; accelerationValue: -9.8; accelerationSpread: 2; maxAge: 2; size: 0.1; particleCount: 4;" position="-2 0.5 3"></a-entity>
|
26 |
+
<a-entity particle-system="color: #2196F3; accelerationValue: -9.8; accelerationSpread: 2; maxAge: 2; size: 0.1; particleCount: 4;" position="2 0.5 3"></a-entity>
|
27 |
+
<a-entity particle-system="color: #2196F3; accelerationValue: -9.8; accelerationSpread: 2; maxAge: 2; size: 0.1; particleCount: 4;" position="-2 0.5 7"></a-entity>
|
28 |
+
<a-entity particle-system="color: #2196F3; accelerationValue: -9.8; accelerationSpread: 2; maxAge: 2; size: 0.1; particleCount: 4;" position="2 0.5 7"></a-entity>
|
29 |
+
|
30 |
+
<!-- Script to move the ball -->
|
31 |
+
<script>
|
32 |
+
// Get the ball entity
|
33 |
+
var ball = document.querySelector("#ball");
|
34 |
+
|
35 |
+
// Set the ball's initial velocity
|
36 |
+
var ballVelocity = new THREE.Vector3(0, 0, 0);
|
37 |
+
|
38 |
+
// Set the ball's mass
|
39 |
+
var ballMass = 1;
|
40 |
+
|
41 |
+
// Set the ball's friction coefficient
|
42 |
+
var ballFriction = 0.01;
|
43 |
+
|
44 |
+
// Set the ball's restitution coefficient
|
45 |
+
var ballRestitution = 0.9;
|
46 |
+
|
47 |
+
// Set the ball's position update interval
|
48 |
+
var ballInterval = 20; // milliseconds
|
49 |
+
|
50 |
+
// Set the ball's initial position
|
51 |
+
var ballPosition = new THREE.Vector3(0, 0.2, 0);
|
52 |
+
|
53 |
+
// Set the ball's size
|
54 |
+
var ballSize = 0.2;
|
55 |
+
|
56 |
+
// Set the ball's goal width and height
|
57 |
+
var goalWidth = 5;
|
58 |
+
var goalHeight = 3;
|
59 |
+
|
60 |
+
// Set the ball's movement speed
|
61 |
+
var ballSpeed = 0.2;
|
62 |
+
|
63 |
+
// Set the ball's maximum velocity
|
64 |
+
var ballMaxVelocity = 10;
|
65 |
+
|
66 |
+
// Set the ball's reset position
|
67 |
+
var resetPosition = new THREE.Vector3(0, 0.2, 0);
|
68 |
+
|
69 |
+
// Set the ball's collision radius
|
70 |
+
var ballRadius = 0.4;
|
71 |
+
|
72 |
+
// Set the ball's collision offset
|
73 |
+
var ballOffset = new THREE.Vector3(0, ballRadius, 0);
|
74 |
+
|
75 |
+
// Set the ball's collision detection
|
76 |
+
var ballCollisionDetection = true;
|
77 |
+
|
78 |
+
// Set the ball's collision response
|
79 |
+
var ballCollisionResponse = true;
|
80 |
+
|
81 |
+
// Set the ball's collision event listener
|
82 |
+
ball.addEventListener("collide", function(event) {
|
83 |
+
// Get the collider object
|
84 |
+
var collider = event.detail.body.el;
|
85 |
+
|
86 |
+
// Get the collider's velocity
|
87 |
+
var colliderVelocity = collider.getAttribute("particle-system").velocity;
|
88 |
+
|
89 |
+
// Set the ball's velocity to the collider's velocity
|
90 |
+
ballVelocity.copy(colliderVelocity);
|
91 |
+
});
|
92 |
+
|
93 |
+
// Set the ball's movement direction
|
94 |
+
var ballDirection = new THREE.Vector3(0, 0, 0);
|
95 |
+
|
96 |
+
// Set the ball's goal detection
|
97 |
+
function detectGoal() {
|
98 |
+
if (ballPosition.x < -goalWidth / 2 || ballPosition.x > goalWidth / 2 || ballPosition.z < -goalHeight / 2 || ballPosition.z > goalHeight / 2) {
|
99 |
+
resetGame();
|
100 |
+
}
|
101 |
+
}
|
102 |
+
|
103 |
+
// Set the ball's movement logic
|
104 |
+
function moveBall() {
|
105 |
+
// Update the ball's position
|
106 |
+
ballPosition.add(ballDirection);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
+
// Apply friction to the ball's velocity
|
109 |
+
ballVelocity.multiplyScalar(1 - ballFriction);
|
110 |
+
|
111 |
+
// Apply gravity to the ball's velocity
|
112 |
+
ballVelocity.add(new THREE.Vector3(0, -9.8 * ballMass / 1000, 0));
|
113 |
+
|
114 |
+
// Apply the ball's velocity to its position
|
115 |
+
ballPosition.add(ballVelocity);
|
116 |
+
|
117 |
+
// Apply the ball's position to the ball entity
|
118 |
+
ball.setAttribute("position", ballPosition);
|
119 |
+
|
120 |
+
// Detect if the ball has gone into a goal
|
121 |
+
detectGoal();
|
122 |
+
|
123 |
+
// Update the ball's position every interval
|
124 |
+
setTimeout(moveBall, ballInterval);
|
125 |
+
}
|
126 |
+
|
127 |
+
// Set the ball's reset logic
|
128 |
+
function resetGame() {
|
129 |
+
// Reset the ball's position
|
130 |
+
ballPosition.copy(resetPosition);
|
131 |
+
ball.setAttribute("position", ballPosition);
|
132 |
+
|
133 |
+
// Reset the ball's velocity
|
134 |
+
ballVelocity.set(0, 0, 0);
|
135 |
+
|
136 |
+
// Reset the ball's direction
|
137 |
+
ballDirection.set(0, 0, 0);
|
138 |
+
}
|
139 |
+
|
140 |
+
// Set the ball's collision detection
|
141 |
+
function detectCollisions() {
|
142 |
+
// Get all the particle system entities
|
143 |
+
var particleSystems = document.querySelectorAll("a-entity[particle-system]");
|
144 |
+
|
145 |
+
// Loop through each particle system entity
|
146 |
+
for (var i = 0; i < particleSystems.length; i++) {
|
147 |
+
// Get the particle system's position
|
148 |
+
var particleSystemPosition = particleSystems[i].getAttribute("position");
|
149 |
+
|
150 |
+
// Get the particle system's velocity
|
151 |
+
var particleSystemVelocity = particleSystems[i].getAttribute("particle-system").velocity;
|
152 |
+
|
153 |
+
// Get the particle system's direction
|
154 |
+
var particleSystemDirection = new THREE.Vector3(particleSystemVelocity.x, 0, particleSystemVelocity.z);
|
155 |
+
|
156 |
+
// Get the particle system's radius
|
157 |
+
var particleSystemRadius = particleSystems[i].getAttribute("particle-system").size * particleSystems[i].getAttribute("particle-system").particleCount;
|
158 |
+
|
159 |
+
// Get the particle system's offset
|
160 |
+
var particleSystemOffset = new THREE.Vector3(0, particleSystemRadius, 0);
|
161 |
+
|
162 |
+
// Get the particle system's collision detection
|
163 |
+
var particleSystemCollisionDetection = particleSystems[i].getAttribute("particle-system").collisionDetection;
|
164 |
+
|
165 |
+
// Get the particle system's collision response
|
166 |
+
var particleSystemCollisionResponse = particleSystems[i].getAttribute("particle-system").collisionResponse;
|
167 |
+
|
168 |
+
// Get the particle system's collision event listener
|
169 |
+
var particleSystemCollisionListener = particleSystems[i].getAttribute("particle-system").collisionListener;
|
170 |
+
|
171 |
+
// Calculate the distance between the particle system and the ball
|
172 |
+
var distance = particleSystemPosition.distanceTo(ballPosition);
|
173 |
+
|
174 |
+
// Check if the particle system is colliding with the ball
|
175 |
+
if (distance < particleSystemRadius + ballRadius) {
|
176 |
+
// Set the ball's direction to the particle system's direction
|
177 |
+
ballDirection.copy(particleSystemDirection);
|
178 |
+
|
179 |
+
// Emit a collision event on the particle system entity
|
180 |
+
if (particleSystemCollisionListener) {
|
181 |
+
particleSystems[i].emit("collision");
|
182 |
+
}
|
183 |
}
|
184 |
+
}
|
185 |
+
|
186 |
+
// Check for collisions every interval
|
187 |
+
setTimeout(detectCollisions, 20);
|
188 |
+
}
|
189 |
+
|
190 |
+
// Set the ball's movement direction to the ball's speed
|
191 |
+
ballDirection.set(0, 0, -ballSpeed);
|
192 |
+
|
193 |
+
// Start the ball's movement logic
|
194 |
+
moveBall();
|
195 |
+
|
196 |
+
// Start the ball's collision detection
|
197 |
+
detectCollisions();
|
198 |
+
</script>
|
199 |
+
</a-scene>
|
200 |
+
|
201 |
</body>
|
202 |
+
</html>
|