57 lines
1.5 KiB
HTML
57 lines
1.5 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>Shapes - Collision</title>
|
|
<style>
|
|
* {
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
html {
|
|
height: 100%;
|
|
}
|
|
|
|
body {
|
|
min-height: 100%;
|
|
position: relative;
|
|
margin: 0;
|
|
}
|
|
|
|
folk-shape {
|
|
border: 2px solid black;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<folk-shape x="100" y="100" width="50" height="50"></folk-shape>
|
|
<folk-shape x="200" y="200" width="50" height="50"></folk-shape>
|
|
<folk-shape x="200" y="275" width="50" height="50"></folk-shape>
|
|
<folk-shape x="200" y="150" width="50" height="50"></folk-shape>
|
|
<folk-shape x="200" y="100" width="50" height="50"></folk-shape>
|
|
|
|
<script type="module">
|
|
import '../src/standalone/folk-shape.ts';
|
|
import { aabbHitDetection } from '../src/common/collision.ts';
|
|
|
|
const shapes = Array.from(document.querySelectorAll('folk-shape'));
|
|
|
|
function handleCollision(e) {
|
|
for (const shape of shapes) {
|
|
if (shape === e.target) continue;
|
|
|
|
const hit = aabbHitDetection(shape.getTransformDOMRect(), e.target.getTransformDOMRect());
|
|
|
|
if (hit === null) continue;
|
|
|
|
shape.x -= hit.delta.x;
|
|
shape.y -= hit.delta.y;
|
|
}
|
|
}
|
|
|
|
document.addEventListener('transform', handleCollision);
|
|
</script>
|
|
</body>
|
|
</html>
|