|
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
<meta charset="utf-8"> |
|
<title>3D Breakout Game</title> |
|
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script> |
|
</head> |
|
<body> |
|
<a-scene physics="debug: true"> |
|
|
|
<a-entity camera look-controls position="0 1.6 0"></a-entity> |
|
|
|
|
|
<a-box width="10" height="10" depth="10" color="#555" static-body></a-box> |
|
|
|
|
|
<a-plane width="2" height="0.2" color="#f00" position="0 -4.9 0" kinematic-body></a-plane> |
|
|
|
|
|
<a-sphere radius="0.2" color="#00f" position="0 -4.5 0" dynamic-body></a-sphere> |
|
|
|
|
|
<a-box width="1" height="0.5" depth="0.5" color="#0f0" position="-2.5 2.5 0" static-body></a-box> |
|
<a-box width="1" height="0.5" depth="0.5" color="#0f0" position="-1.5 2.5 0" static-body></a-box> |
|
<a-box width="1" height="0.5" depth="0.5" color="#0f0" position="-0.5 2.5 0" static-body></a-box> |
|
<a-box width="1" height="0.5" depth="0.5" color="#0f0" position="0.5 2.5 0" static-body></a-box> |
|
<a-box width="1" height="0.5" depth="0.5" color="#0f0" position="1.5 2.5 0" static-body></a-box> |
|
<a-box width="1" height="0.5" depth="0.5" color="#0f0" position="2.5 2.5 0" static-body></a-box> |
|
|
|
|
|
<script> |
|
|
|
var paddle = document.querySelector('[kinematic-body]'); |
|
|
|
|
|
window.addEventListener('mousemove', function (event) { |
|
|
|
var x = event.clientX / window.innerWidth * 2 - 1; |
|
var y = event.clientY / window.innerHeight * 2 - 1; |
|
|
|
|
|
paddle.object3D.position.x = x * 5; |
|
}); |
|
</script> |
|
|
|
|
|
<script> |
|
|
|
var ball = document.querySelector('[dynamic-body]'); |
|
|
|
|
|
var velocity = new THREE.Vector3(0, 0, -5); |
|
ball.body.velocity.copy(velocity); |
|
|
|
|
|
ball.addEventListener('collide', function (event) { |
|
|
|
var object = event.detail.body.el; |
|
|
|
|
|
|
|
if (object.hasAttribute('static-body')) { |
|
object.parentNode.removeChild(object); |
|
|
|
} |
|
|
|
|
|
if (object.hasAttribute('static-body') && object.tagName === 'A-BOX') { |
|
|
|
var normal = event.detail.contact.ni; |
|
|
|
|
|
velocity.reflect(normal); |
|
|
|
|
|
ball.body.velocity.copy(velocity); |
|
} |
|
}); |
|
|
|
|
|
function updateBallPosition() { |
|
|
|
if (ball.object3D.position.y < -5) { |
|
|
|
} |
|
|
|
|
|
if (ball.object3D.position.x < -4.5 || ball.object3D.position.x > 4.5) { |
|
velocity.x = -velocity.x; |
|
ball.body.velocity.copy(velocity); |
|
} |
|
if (ball.object3D.position.y > 4.5) { |
|
velocity.y = -velocity.y; |
|
ball.body.velocity.copy(velocity); |
|
} |
|
if (ball.object3D.position.z < -4.5 || ball.object3D.position.z > 4.5) { |
|
velocity.z = -velocity.z; |
|
ball.body.velocity.copy(velocity); |
|
} |
|
|
|
|
|
ball.object3D.position.addScaledVector(velocity, 1 / 60); |
|
} |
|
|
|
|
|
var scene = document.querySelector('a-scene'); |
|
scene.addEventListener('update', updateBallPosition); |
|
</script> |
|
</a-scene> |
|
</body> |
|
</html> |