-
Notifications
You must be signed in to change notification settings - Fork 0
/
jenga.html
279 lines (235 loc) · 11 KB
/
jenga.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Jenga Game with Realistic Block Physics</title>
<style>
body { margin: 0; }
canvas { display: block; }
#info {
position: absolute;
top: 10px;
width: 100%;
text-align: center;
color: white;
font-family: Arial, sans-serif;
background-color: rgba(0,0,0,0.5);
padding: 10px;
}
</style>
</head>
<body>
<div id="info">Touch a block to select it (turns green), then swipe to remove it.</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cannon.js/0.6.2/cannon.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
if (typeof THREE === 'undefined' || typeof CANNON === 'undefined') {
document.getElementById('info').textContent = 'Error: Required libraries not loaded. Please check your internet connection and reload the page.';
return;
}
let scene, camera, renderer, world;
let blocks = [];
let selectedBlock = null;
let isDragging = false;
let mouseStart = new THREE.Vector2();
let mouseEnd = new THREE.Vector2();
let lastUpdateTime = 0;
const timeStep = 1 / 60;
let isSettling = true;
let settlingFrames = 0;
const BLOCK_COLOR = 0xF5DEB3; // Beige color
const SELECTED_COLOR = 0x00FF00; // Green color
function init() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0x87CEEB);
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
scene.add(new THREE.AmbientLight(0x404040, 0.5));
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
camera.position.set(0, 5, 10);
camera.lookAt(0, 5, 0);
world = new CANNON.World();
world.gravity.set(0, -9.82, 0);
world.solver.iterations = 50;
world.solver.tolerance = 0.001;
createGround();
createTower();
window.addEventListener('resize', onWindowResize, false);
renderer.domElement.addEventListener('mousedown', onMouseDown, false);
renderer.domElement.addEventListener('mousemove', onMouseMove, false);
renderer.domElement.addEventListener('mouseup', onMouseUp, false);
renderer.domElement.addEventListener('touchstart', onTouchStart, false);
renderer.domElement.addEventListener('touchmove', onTouchMove, false);
renderer.domElement.addEventListener('touchend', onTouchEnd, false);
animate();
}
function createGround() {
const groundBody = new CANNON.Body({ mass: 0 });
groundBody.addShape(new CANNON.Plane());
groundBody.quaternion.setFromAxisAngle(new CANNON.Vec3(1, 0, 0), -Math.PI / 2);
world.addBody(groundBody);
const groundMesh = new THREE.Mesh(
new THREE.PlaneGeometry(10, 10),
new THREE.MeshPhongMaterial({ color: 0xcccccc })
);
groundMesh.rotation.x = -Math.PI / 2;
scene.add(groundMesh);
}
function createTower() {
const blockShape = new CANNON.Box(new CANNON.Vec3(0.75, 0.25, 0.25));
const blockGeometry = new THREE.BoxGeometry(1.5, 0.5, 0.5);
const blockMaterial = new THREE.MeshPhongMaterial({ color: BLOCK_COLOR });
for (let i = 0; i < 18; i++) {
const y = i * 0.5 + 0.25;
for (let j = 0; j < 3; j++) {
const blockBody = new CANNON.Body({
mass: 5,
linearDamping: 0.4,
angularDamping: 0.4,
material: new CANNON.Material({ friction: 0.5, restitution: 0.3 })
});
blockBody.addShape(blockShape);
const blockMesh = new THREE.Mesh(blockGeometry, blockMaterial.clone());
if (i % 2 === 0) {
blockBody.position.set(j * 0.5 - 0.5, y, 0);
blockMesh.position.copy(blockBody.position);
} else {
blockBody.position.set(0, y, j * 0.5 - 0.5);
blockBody.quaternion.setFromAxisAngle(new CANNON.Vec3(0, 1, 0), Math.PI / 2);
blockMesh.position.copy(blockBody.position);
blockMesh.quaternion.copy(blockBody.quaternion);
}
blockBody.sleepSpeedLimit = 0.1;
blockBody.sleepTimeLimit = 0.1;
world.addBody(blockBody);
scene.add(blockMesh);
blocks.push({ mesh: blockMesh, body: blockBody });
}
}
}
function onMouseDown(event) {
event.preventDefault();
handleStart(event.clientX, event.clientY);
}
function onMouseMove(event) {
event.preventDefault();
handleMove(event.clientX, event.clientY);
}
function onMouseUp(event) {
event.preventDefault();
handleEnd();
}
function onTouchStart(event) {
event.preventDefault();
if (event.touches.length > 0) {
handleStart(event.touches[0].clientX, event.touches[0].clientY);
}
}
function onTouchMove(event) {
event.preventDefault();
if (event.touches.length > 0) {
handleMove(event.touches[0].clientX, event.touches[0].clientY);
}
}
function onTouchEnd(event) {
event.preventDefault();
handleEnd();
}
function handleStart(clientX, clientY) {
if (isSettling) return;
mouseStart.set(
(clientX / window.innerWidth) * 2 - 1,
-(clientY / window.innerHeight) * 2 + 1
);
isDragging = true;
lastUpdateTime = Date.now();
checkBlockSelection(mouseStart);
}
function handleMove(clientX, clientY) {
if (!isDragging || isSettling) return;
mouseEnd.set(
(clientX / window.innerWidth) * 2 - 1,
-(clientY / window.innerHeight) * 2 + 1
);
if (selectedBlock && !selectedBlock.isRemoving) {
const currentTime = Date.now();
const deltaTime = (currentTime - lastUpdateTime) / 1000; // Convert to seconds
lastUpdateTime = currentTime;
const swipeDirection = new THREE.Vector2().subVectors(mouseEnd, mouseStart);
const swipeVelocity = swipeDirection.divideScalar(deltaTime);
if (swipeVelocity.length() > 0.1) {
startRemovingBlock(selectedBlock, swipeVelocity);
}
}
mouseStart.copy(mouseEnd);
}
function handleEnd() {
isDragging = false;
if (selectedBlock && !selectedBlock.isRemoving) {
selectedBlock.mesh.material.color.setHex(BLOCK_COLOR);
}
selectedBlock = null;
}
function checkBlockSelection(mouse) {
const raycaster = new THREE.Raycaster();
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects(blocks.map(b => b.mesh));
if (intersects.length > 0) {
selectedBlock = blocks.find(b => b.mesh === intersects[0].object);
selectedBlock.mesh.material.color.setHex(SELECTED_COLOR);
}
}
function startRemovingBlock(block, swipeVelocity) {
block.isRemoving = true;
// Calculate the 3D velocity based on the 2D swipe
const velocity = new CANNON.Vec3(
swipeVelocity.x * 10, // Scale factor for x (left/right)
Math.abs(swipeVelocity.y) * 5, // Scale factor for y (up), always positive
swipeVelocity.y * 10 // Scale factor for z (in/out of screen)
);
// Apply the velocity to the block
block.body.velocity.copy(velocity);
// Add some angular velocity for more dynamic motion
block.body.angularVelocity.set(
Math.random() - 0.5,
Math.random() - 0.5,
Math.random() - 0.5
);
// Ensure the block can now move freely
block.body.collisionFilterGroup = 2; // New collision group
block.body.collisionFilterMask = 1; // Only collide with the ground
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
if (isSettling) {
settlingFrames++;
if (settlingFrames > 60) { // Allow 1 second for settling
isSettling = false;
blocks.forEach(block => {
block.body.type = CANNON.Body.DYNAMIC;
});
}
}
world.step(timeStep);
blocks.forEach(block => {
block.mesh.position.copy(block.body.position);
block.mesh.quaternion.copy(block.body.quaternion);
});
renderer.render(scene, camera);
}
init();
});
</script>
</body>
</html>