Update index.html
Browse files- index.html +82 -179
index.html
CHANGED
@@ -1,207 +1,110 @@
|
|
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 |
-
<!--
|
9 |
-
<a-entity camera look-controls position="0
|
10 |
-
|
11 |
-
<!-- Ground -->
|
12 |
-
<a-box id="ground" 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 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 ground object
|
33 |
-
var ground = document.querySelector("#ground");
|
34 |
-
|
35 |
-
// Get the ball entity
|
36 |
-
var ball = document.querySelector("#ball");
|
37 |
-
|
38 |
-
// Set the ball's initial velocity
|
39 |
-
var ballVelocity = new THREE.Vector3(0, 0, 0);
|
40 |
-
|
41 |
-
// Set the ball's mass
|
42 |
-
var ballMass = 1;
|
43 |
-
|
44 |
-
// Set the ball's friction coefficient
|
45 |
-
var ballFriction = 0.01;
|
46 |
-
|
47 |
-
// Set the ball's restitution coefficient
|
48 |
-
var ballRestitution = 0.9;
|
49 |
|
50 |
-
|
51 |
-
|
52 |
|
53 |
-
|
54 |
-
|
55 |
|
56 |
-
|
57 |
-
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
var ballMaxVelocity = 10;
|
68 |
-
|
69 |
-
// Set the ball's reset position
|
70 |
-
var resetPosition = new THREE.Vector3(0, 2, 0);
|
71 |
-
|
72 |
-
// Set the ball's collision radius
|
73 |
-
var ballRadius = 0.4;
|
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 |
-
if (ballPosition.x < -goalWidth / 2 || ballPosition.x > goalWidth / 2 || ballPosition.z < -goalHeight / 2 || ballPosition.z > goalHeight / 2) {
|
102 |
-
resetGame();
|
103 |
}
|
104 |
-
}
|
105 |
-
|
106 |
-
// Set the ball's movement logic
|
107 |
-
function moveBall() {
|
108 |
-
// Update the ball's position
|
109 |
-
ballPosition.add(ballDirection);
|
110 |
-
|
111 |
-
// Apply friction to the ball's velocity
|
112 |
-
ballVelocity.multiplyScalar(1 - ballFriction);
|
113 |
-
|
114 |
-
// Apply gravity to the ball's velocity
|
115 |
-
ballVelocity.add(new THREE.Vector3(0, -9.8 * ballMass / 1000, 0));
|
116 |
-
|
117 |
-
// Apply the ball's velocity to its position
|
118 |
-
ballPosition.add(ballVelocity);
|
119 |
-
|
120 |
-
// Apply the ground object's position to the ball's position
|
121 |
-
ballPosition.setY(Math.max(ballPosition.y, ground.object3D.position.y + ballRadius));
|
122 |
-
|
123 |
-
// Apply the ball's position to the ball entity
|
124 |
-
ball.setAttribute("position", ballPosition);
|
125 |
-
|
126 |
-
// Detect if the ball has gone into a goal
|
127 |
-
detectGoal();
|
128 |
|
129 |
-
//
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
// Reset the ball's direction
|
143 |
-
ballDirection.set(0, 0, 0);
|
144 |
-
}
|
145 |
-
|
146 |
-
// Set the ball's collision detection
|
147 |
-
function detectCollisions() {
|
148 |
-
// Get all the particle system entities
|
149 |
-
var particleSystems = document.querySelectorAll("a-entity[particle-system]");
|
150 |
-
|
151 |
-
// Loop through each particle system entity
|
152 |
-
for (var i = 0; i < particleSystems.length; i++) {
|
153 |
-
// Get the
|
154 |
-
// Get the particle system's position
|
155 |
-
var particleSystemPosition = particleSystems[i].getAttribute("position");
|
156 |
-
|
157 |
-
// Get the particle system's velocity
|
158 |
-
var particleSystemVelocity = particleSystems[i].getAttribute("particle-system").velocity;
|
159 |
-
|
160 |
-
// Get the particle system's direction
|
161 |
-
var particleSystemDirection = new THREE.Vector3(particleSystemVelocity.x, 0, particleSystemVelocity.z);
|
162 |
-
|
163 |
-
// Get the particle system's radius
|
164 |
-
var particleSystemRadius = particleSystems[i].getAttribute("particle-system").size * particleSystems[i].getAttribute("particle-system").particleCount;
|
165 |
-
|
166 |
-
// Get the particle system's offset
|
167 |
-
var particleSystemOffset = new THREE.Vector3(0, particleSystemRadius, 0);
|
168 |
-
|
169 |
-
// Get the particle system's collision detection
|
170 |
-
var particleSystemCollisionDetection = particleSystems[i].getAttribute("particle-system").collisionDetection;
|
171 |
-
|
172 |
-
// Get the particle system's collision response
|
173 |
-
var particleSystemCollisionResponse = particleSystems[i].getAttribute("particle-system").collisionResponse;
|
174 |
-
|
175 |
-
// Get the particle system's collision event listener
|
176 |
-
var particleSystemCollisionListener = particleSystems[i].getAttribute("particle-system").collisionListener;
|
177 |
-
|
178 |
-
// Calculate the distance between the particle system and the ball
|
179 |
-
var distance = particleSystemPosition.distanceTo(ballPosition);
|
180 |
-
|
181 |
-
// Check if the particle system is colliding with the ball
|
182 |
-
if (distance < particleSystemRadius + ballRadius) {
|
183 |
-
// Set the ball's direction to the particle system's direction
|
184 |
-
ballDirection.copy(particleSystemDirection);
|
185 |
-
|
186 |
-
// Emit a collision event on the particle system entity
|
187 |
-
if (particleSystemCollisionListener) {
|
188 |
-
particleSystems[i].emit("collision");
|
189 |
-
}
|
190 |
-
}
|
191 |
}
|
192 |
|
193 |
-
//
|
194 |
-
|
195 |
}
|
196 |
|
197 |
-
//
|
198 |
-
|
199 |
-
|
200 |
-
// Start the ball's movement logic
|
201 |
-
moveBall();
|
202 |
-
|
203 |
-
// Start the ball's collision detection
|
204 |
-
detectCollisions();
|
205 |
</script>
|
206 |
</a-scene>
|
207 |
</body>
|
|
|
1 |
<!DOCTYPE html>
|
2 |
<html>
|
3 |
<head>
|
4 |
+
<meta charset="utf-8">
|
5 |
+
<title>3D Breakout Game</title>
|
6 |
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
|
7 |
</head>
|
8 |
<body>
|
9 |
+
<a-scene physics="debug: true">
|
10 |
+
<!-- Add a camera to the scene -->
|
11 |
+
<a-entity camera look-controls position="0 1.6 0"></a-entity>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
<!-- Create the game room cube -->
|
14 |
+
<a-box width="10" height="10" depth="10" color="#555" static-body></a-box>
|
15 |
|
16 |
+
<!-- Create the paddle -->
|
17 |
+
<a-plane width="2" height="0.2" color="#f00" position="0 -4.9 0" kinematic-body></a-plane>
|
18 |
|
19 |
+
<!-- Create the ball -->
|
20 |
+
<a-sphere radius="0.2" color="#00f" position="0 -4.5 0" dynamic-body></a-sphere>
|
21 |
|
22 |
+
<!-- Create the bricks -->
|
23 |
+
<a-box width="1" height="0.5" depth="0.5" color="#0f0" position="-2.5 2.5 0" static-body></a-box>
|
24 |
+
<a-box width="1" height="0.5" depth="0.5" color="#0f0" position="-1.5 2.5 0" static-body></a-box>
|
25 |
+
<a-box width="1" height="0.5" depth="0.5" color="#0f0" position="-0.5 2.5 0" static-body></a-box>
|
26 |
+
<a-box width="1" height="0.5" depth="0.5" color="#0f0" position="0.5 2.5 0" static-body></a-box>
|
27 |
+
<a-box width="1" height="0.5" depth="0.5" color="#0f0" position="1.5 2.5 0" static-body></a-box>
|
28 |
+
<a-box width="1" height="0.5" depth="0.5" color="#0f0" position="2.5 2.5 0" static-body></a-box>
|
29 |
|
30 |
+
<!-- Add the mouse events to move the paddle -->
|
31 |
+
<script>
|
32 |
+
// Get the paddle element
|
33 |
+
var paddle = document.querySelector('[kinematic-body]');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
// Add an event listener for mouse movement
|
36 |
+
window.addEventListener('mousemove', function (event) {
|
37 |
+
// Get the x and y coordinates of the mouse
|
38 |
+
var x = event.clientX / window.innerWidth * 2 - 1;
|
39 |
+
var y = event.clientY / window.innerHeight * 2 - 1;
|
40 |
|
41 |
+
// Move the paddle along the x-axis based on the mouse position
|
42 |
+
paddle.object3D.position.x = x * 5;
|
43 |
+
});
|
44 |
+
</script>
|
45 |
|
46 |
+
<!-- Add the script for ball movement and collision detection -->
|
47 |
+
<script>
|
48 |
+
// Get the ball element
|
49 |
+
var ball = document.querySelector('[dynamic-body]');
|
50 |
+
|
51 |
+
// Set the initial velocity of the ball
|
52 |
+
var velocity = new THREE.Vector3(0, 0, -5);
|
53 |
+
ball.body.velocity.copy(velocity);
|
54 |
+
|
55 |
+
// Add an event listener for collision detection
|
56 |
+
ball.addEventListener('collide', function (event) {
|
57 |
+
// Get the object that the ball collided with
|
58 |
+
var object = event.detail.body.el;
|
59 |
+
|
60 |
+
// If the object is a
|
61 |
+
// brick, remove it and update the score
|
62 |
+
if (object.hasAttribute('static-body')) {
|
63 |
+
object.parentNode.removeChild(object);
|
64 |
+
// update the score here
|
65 |
+
}
|
66 |
|
67 |
+
// If the object is the game room cube, bounce the ball back
|
68 |
+
if (object.hasAttribute('static-body') && object.tagName === 'A-BOX') {
|
69 |
+
// Get the normal of the collision
|
70 |
+
var normal = event.detail.contact.ni;
|
71 |
|
72 |
+
// Reflect the velocity of the ball off the normal
|
73 |
+
velocity.reflect(normal);
|
74 |
|
75 |
+
// Set the new velocity of the ball
|
76 |
+
ball.body.velocity.copy(velocity);
|
77 |
+
}
|
78 |
});
|
79 |
|
80 |
+
// Add a function to update the position of the ball every frame
|
81 |
+
function updateBallPosition() {
|
82 |
+
// If the ball is below the paddle, reset the game
|
83 |
+
if (ball.object3D.position.y < -5) {
|
84 |
+
// reset the game here
|
|
|
|
|
85 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
+
// If the ball is outside the game room cube, bounce it back
|
88 |
+
if (ball.object3D.position.x < -4.5 || ball.object3D.position.x > 4.5) {
|
89 |
+
velocity.x = -velocity.x;
|
90 |
+
ball.body.velocity.copy(velocity);
|
91 |
+
}
|
92 |
+
if (ball.object3D.position.y > 4.5) {
|
93 |
+
velocity.y = -velocity.y;
|
94 |
+
ball.body.velocity.copy(velocity);
|
95 |
+
}
|
96 |
+
if (ball.object3D.position.z < -4.5 || ball.object3D.position.z > 4.5) {
|
97 |
+
velocity.z = -velocity.z;
|
98 |
+
ball.body.velocity.copy(velocity);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
}
|
100 |
|
101 |
+
// Update the position of the ball based on its velocity
|
102 |
+
ball.object3D.position.addScaledVector(velocity, 1 / 60);
|
103 |
}
|
104 |
|
105 |
+
// Add the update function to the scene's update loop
|
106 |
+
var scene = document.querySelector('a-scene');
|
107 |
+
scene.addEventListener('update', updateBallPosition);
|
|
|
|
|
|
|
|
|
|
|
108 |
</script>
|
109 |
</a-scene>
|
110 |
</body>
|