Spaces:
Running
Running
Update index.html
Browse files- index.html +49 -58
index.html
CHANGED
|
@@ -2,13 +2,12 @@
|
|
| 2 |
<html>
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
-
<title>3D Card Game with
|
| 6 |
<style>
|
| 7 |
body { margin: 0; }
|
| 8 |
canvas { display: block; }
|
| 9 |
</style>
|
| 10 |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"></script>
|
| 11 |
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/cannon.js/0.6.2/cannon.min.js"></script>
|
| 12 |
</head>
|
| 13 |
<body>
|
| 14 |
<canvas id="gameCanvas"></canvas>
|
|
@@ -16,14 +15,11 @@
|
|
| 16 |
<script>
|
| 17 |
var scene = new THREE.Scene();
|
| 18 |
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
|
| 19 |
-
camera.position.set(0,
|
|
|
|
| 20 |
var renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('gameCanvas') });
|
| 21 |
renderer.setSize(window.innerWidth, window.innerHeight);
|
| 22 |
|
| 23 |
-
// Physics world setup
|
| 24 |
-
var world = new CANNON.World();
|
| 25 |
-
world.gravity.set(0, -9.82, 0); // Apply gravity in the negative y direction
|
| 26 |
-
|
| 27 |
// Lighting
|
| 28 |
var ambientLight = new THREE.AmbientLight(0x404040); // Soft white light
|
| 29 |
scene.add(ambientLight);
|
|
@@ -31,77 +27,72 @@
|
|
| 31 |
directionalLight.position.set(0, 1, 1);
|
| 32 |
scene.add(directionalLight);
|
| 33 |
|
| 34 |
-
// Table
|
| 35 |
var tableGeometry = new THREE.BoxGeometry(100, 1, 100);
|
| 36 |
-
var tableMaterial = new THREE.MeshLambertMaterial({ color: 0x8B4513 });
|
| 37 |
var table = new THREE.Mesh(tableGeometry, tableMaterial);
|
| 38 |
-
table.position.set(0, -0.5, 0);
|
| 39 |
scene.add(table);
|
| 40 |
|
| 41 |
-
//
|
| 42 |
-
var
|
| 43 |
-
var tableBody = new CANNON.Body({
|
| 44 |
-
mass: 0, // Mass of 0 makes the body static
|
| 45 |
-
position: new CANNON.Vec3(0, -0.5, 0), // Match the position of the Three.js mesh
|
| 46 |
-
shape: tableShape
|
| 47 |
-
});
|
| 48 |
-
world.addBody(tableBody);
|
| 49 |
-
|
| 50 |
-
// Card geometry and material
|
| 51 |
-
var cardGeometry = new THREE.BoxGeometry(5, 0.1, 7); // Generic card size
|
| 52 |
var cards = [];
|
| 53 |
-
var cardBodies = []; // For physics bodies
|
| 54 |
|
| 55 |
-
// Generate
|
| 56 |
-
var colors = [];
|
| 57 |
for (let i = 0; i < 52; i++) {
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
}
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
}
|
| 67 |
}
|
| 68 |
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
card.position.set(x, y, z);
|
| 76 |
-
scene.add(card);
|
| 77 |
-
cards.push(card);
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
cardBodies.push(cardBody);
|
| 86 |
}
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
addCard(0, i * 0.2 + 1, 0, i); // Position cards in a stack
|
| 91 |
}
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
function animate() {
|
| 94 |
requestAnimationFrame(animate);
|
| 95 |
-
|
| 96 |
-
// Update physics world
|
| 97 |
-
world.step(1 / 60);
|
| 98 |
-
|
| 99 |
-
// Sync Three.js objects with physics objects
|
| 100 |
-
for (let i = 0; i < cardBodies.length; i++) {
|
| 101 |
-
cards[i].position.copy(cardBodies[i].position);
|
| 102 |
-
cards[i].quaternion.copy(cardBodies[i].quaternion);
|
| 103 |
-
}
|
| 104 |
-
|
| 105 |
renderer.render(scene, camera);
|
| 106 |
}
|
| 107 |
|
|
|
|
| 2 |
<html>
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
+
<title>3D Card Game with Interactive Drag and Drop</title>
|
| 6 |
<style>
|
| 7 |
body { margin: 0; }
|
| 8 |
canvas { display: block; }
|
| 9 |
</style>
|
| 10 |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"></script>
|
|
|
|
| 11 |
</head>
|
| 12 |
<body>
|
| 13 |
<canvas id="gameCanvas"></canvas>
|
|
|
|
| 15 |
<script>
|
| 16 |
var scene = new THREE.Scene();
|
| 17 |
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
|
| 18 |
+
camera.position.set(0, 20, 50);
|
| 19 |
+
|
| 20 |
var renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('gameCanvas') });
|
| 21 |
renderer.setSize(window.innerWidth, window.innerHeight);
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
// Lighting
|
| 24 |
var ambientLight = new THREE.AmbientLight(0x404040); // Soft white light
|
| 25 |
scene.add(ambientLight);
|
|
|
|
| 27 |
directionalLight.position.set(0, 1, 1);
|
| 28 |
scene.add(directionalLight);
|
| 29 |
|
| 30 |
+
// Table
|
| 31 |
var tableGeometry = new THREE.BoxGeometry(100, 1, 100);
|
| 32 |
+
var tableMaterial = new THREE.MeshLambertMaterial({ color: 0x8B4513 });
|
| 33 |
var table = new THREE.Mesh(tableGeometry, tableMaterial);
|
| 34 |
+
table.position.set(0, -0.5, 0);
|
| 35 |
scene.add(table);
|
| 36 |
|
| 37 |
+
// Cards
|
| 38 |
+
var cardGeometry = new THREE.BoxGeometry(5, 0.1, 7);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
var cards = [];
|
|
|
|
| 40 |
|
| 41 |
+
// Generate and place cards
|
|
|
|
| 42 |
for (let i = 0; i < 52; i++) {
|
| 43 |
+
var cardMaterial = new THREE.MeshLambertMaterial({ color: Math.random() * 0xffffff });
|
| 44 |
+
var card = new THREE.Mesh(cardGeometry, cardMaterial);
|
| 45 |
+
card.position.set(Math.random() * 10 - 5, 0.1 * i, Math.random() * 10 - 5);
|
| 46 |
+
scene.add(card);
|
| 47 |
+
cards.push(card);
|
| 48 |
}
|
| 49 |
|
| 50 |
+
var raycaster = new THREE.Raycaster();
|
| 51 |
+
var mouse = new THREE.Vector2();
|
| 52 |
+
var selectedCard = null;
|
| 53 |
+
var offset = new THREE.Vector3();
|
| 54 |
+
|
| 55 |
+
function onMouseMove(event) {
|
| 56 |
+
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
|
| 57 |
+
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
|
| 58 |
+
|
| 59 |
+
if (selectedCard) {
|
| 60 |
+
raycaster.setFromCamera(mouse, camera);
|
| 61 |
+
var intersects = raycaster.intersectObject(table, true);
|
| 62 |
+
if (intersects.length > 0) {
|
| 63 |
+
var intersect = intersects[0];
|
| 64 |
+
selectedCard.position.copy(intersect.point.add(offset));
|
| 65 |
+
}
|
| 66 |
}
|
| 67 |
}
|
| 68 |
|
| 69 |
+
function onMouseDown(event) {
|
| 70 |
+
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
|
| 71 |
+
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
|
| 72 |
|
| 73 |
+
raycaster.setFromCamera(mouse, camera);
|
| 74 |
+
var intersects = raycaster.intersectObjects(cards, true);
|
| 75 |
+
if (intersects.length > 0) {
|
| 76 |
+
selectedCard = intersects[0].object;
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
var intersects = raycaster.intersectObject(table, true);
|
| 79 |
+
if (intersects.length > 0) {
|
| 80 |
+
var intersect = intersects[0];
|
| 81 |
+
offset.subVectors(selectedCard.position, intersect.point);
|
| 82 |
+
}
|
| 83 |
+
}
|
|
|
|
| 84 |
}
|
| 85 |
|
| 86 |
+
function onMouseUp(event) {
|
| 87 |
+
selectedCard = null;
|
|
|
|
| 88 |
}
|
| 89 |
|
| 90 |
+
window.addEventListener('mousemove', onMouseMove, false);
|
| 91 |
+
window.addEventListener('mousedown', onMouseDown, false);
|
| 92 |
+
window.addEventListener('mouseup', onMouseUp, false);
|
| 93 |
+
|
| 94 |
function animate() {
|
| 95 |
requestAnimationFrame(animate);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
renderer.render(scene, camera);
|
| 97 |
}
|
| 98 |
|