|
<!DOCTYPE html> |
|
<html> |
|
<head> |
|
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script> |
|
</head> |
|
<body> |
|
<a-scene> |
|
|
|
<a-entity camera look-controls position="0 2 6"></a-entity> |
|
|
|
|
|
<a-box id="ground" color="green" position="0 -0.5 -10" width="16" height="0.5" depth="26"></a-box> |
|
|
|
|
|
<a-sphere id="ball" color="white" radius="0.2" position="0 2 0"></a-sphere> |
|
|
|
|
|
|
|
<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> |
|
<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> |
|
<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> |
|
<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> |
|
|
|
|
|
<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> |
|
<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> |
|
<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> |
|
<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> |
|
|
|
|
|
<script> |
|
|
|
var ground = document.querySelector("#ground"); |
|
|
|
|
|
var ball = document.querySelector("#ball"); |
|
|
|
|
|
var ballVelocity = new THREE.Vector3(0, 0, 0); |
|
|
|
|
|
var ballMass = 1; |
|
|
|
|
|
var ballFriction = 0.01; |
|
|
|
|
|
var ballRestitution = 0.9; |
|
|
|
|
|
var ballInterval = 20; |
|
|
|
|
|
var ballPosition = new THREE.Vector3(0, 2, 0); |
|
|
|
|
|
var ballSize = 0.2; |
|
|
|
|
|
var goalWidth = 5; |
|
var goalHeight = 3; |
|
|
|
|
|
var ballSpeed = 0.2; |
|
|
|
|
|
var ballMaxVelocity = 10; |
|
|
|
|
|
var resetPosition = new THREE.Vector3(0, 2, 0); |
|
|
|
|
|
var ballRadius = 0.4; |
|
|
|
|
|
var ballOffset = new THREE.Vector3(0, ballRadius, 0); |
|
|
|
|
|
var ballCollisionDetection = true; |
|
|
|
|
|
var ballCollisionResponse = true; |
|
|
|
|
|
ball.addEventListener("collide", function(event) { |
|
|
|
var collider = event.detail.body.el; |
|
|
|
|
|
var colliderVelocity = collider.getAttribute("particle-system").velocity; |
|
|
|
|
|
ballVelocity.copy(colliderVelocity); |
|
}); |
|
|
|
|
|
var ballDirection = new THREE.Vector3(0, 0, 0); |
|
|
|
|
|
function detectGoal() { |
|
if (ballPosition.x < -goalWidth / 2 || ballPosition.x > goalWidth / 2 || ballPosition.z < -goalHeight / 2 || ballPosition.z > goalHeight / 2) { |
|
resetGame(); |
|
} |
|
} |
|
|
|
|
|
function moveBall() { |
|
|
|
ballPosition.add(ballDirection); |
|
|
|
|
|
ballVelocity.multiplyScalar(1 - ballFriction); |
|
|
|
|
|
ballVelocity.add(new THREE.Vector3(0, -9.8 * ballMass / 1000, 0)); |
|
|
|
|
|
ballPosition.add(ballVelocity); |
|
|
|
|
|
ballPosition.setY(Math.max(ballPosition.y, ground.object3D.position.y + ballRadius)); |
|
|
|
|
|
ball.setAttribute("position", ballPosition); |
|
|
|
|
|
detectGoal(); |
|
|
|
|
|
setTimeout(moveBall, ballInterval); |
|
} |
|
|
|
|
|
function resetGame() { |
|
|
|
ballPosition.copy(resetPosition); |
|
ball.setAttribute("position", ballPosition); |
|
|
|
|
|
ballVelocity.set(0, 0, 0); |
|
|
|
|
|
ballDirection.set(0, 0, 0); |
|
} |
|
|
|
|
|
function detectCollisions() { |
|
|
|
var particleSystems = document.querySelectorAll("a-entity[particle-system]"); |
|
|
|
|
|
for (var i = 0; i < particleSystems.length; i++) { |
|
|
|
|
|
var particleSystemPosition = particleSystems[i].getAttribute("position"); |
|
|
|
|
|
var particleSystemVelocity = particleSystems[i].getAttribute("particle-system").velocity; |
|
|
|
|
|
var particleSystemDirection = new THREE.Vector3(particleSystemVelocity.x, 0, particleSystemVelocity.z); |
|
|
|
|
|
var particleSystemRadius = particleSystems[i].getAttribute("particle-system").size * particleSystems[i].getAttribute("particle-system").particleCount; |
|
|
|
|
|
var particleSystemOffset = new THREE.Vector3(0, particleSystemRadius, 0); |
|
|
|
|
|
var particleSystemCollisionDetection = particleSystems[i].getAttribute("particle-system").collisionDetection; |
|
|
|
|
|
var particleSystemCollisionResponse = particleSystems[i].getAttribute("particle-system").collisionResponse; |
|
|
|
|
|
var particleSystemCollisionListener = particleSystems[i].getAttribute("particle-system").collisionListener; |
|
|
|
|
|
var distance = particleSystemPosition.distanceTo(ballPosition); |
|
|
|
|
|
if (distance < particleSystemRadius + ballRadius) { |
|
|
|
ballDirection.copy(particleSystemDirection); |
|
|
|
|
|
if (particleSystemCollisionListener) { |
|
particleSystems[i].emit("collision"); |
|
} |
|
} |
|
} |
|
|
|
|
|
setTimeout(detectCollisions, 20); |
|
} |
|
|
|
|
|
ballDirection.set(0, 0, -ballSpeed); |
|
|
|
|
|
moveBall(); |
|
|
|
|
|
detectCollisions(); |
|
</script> |
|
</a-scene> |
|
</body> |
|
</html> |