awacke1 commited on
Commit
8ed626a
·
verified ·
1 Parent(s): 8437049

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +19 -11
index.html CHANGED
@@ -2,13 +2,13 @@
2
  <html>
3
  <head>
4
  <meta charset="UTF-8">
5
- <title>3D Card Game</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
- <script src="https://cdnjs.cloudflare.com/ajax/libs/cannon.js/0.6.2/cannon.min.js"></script> <!-- For physics -->
12
  </head>
13
  <body>
14
  <canvas id="gameCanvas"></canvas>
@@ -19,7 +19,6 @@
19
  camera.position.set(0, 30, 100);
20
  var renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('gameCanvas') });
21
  renderer.setSize(window.innerWidth, window.innerHeight);
22
- document.body.appendChild(renderer.domElement);
23
 
24
  // Physics world setup
25
  var world = new CANNON.World();
@@ -32,25 +31,34 @@
32
  directionalLight.position.set(0, 1, 1);
33
  scene.add(directionalLight);
34
 
35
- // Table surface
36
  var tableGeometry = new THREE.BoxGeometry(100, 1, 100);
37
  var tableMaterial = new THREE.MeshLambertMaterial({ color: 0x8B4513 }); // Brown color for the table
38
  var table = new THREE.Mesh(tableGeometry, tableMaterial);
39
- table.position.set(0, -1, 0); // Position the table slightly below where the cards will be
40
  scene.add(table);
41
 
42
- // Card geometry
 
 
 
 
 
 
 
 
 
43
  var cardGeometry = new THREE.BoxGeometry(5, 0.1, 7); // Generic card size
44
  var cards = [];
45
  var cardBodies = []; // For physics bodies
46
 
47
- // Shuffle and color assignment
48
- var colors = []; // Array to hold colors
49
- // Generate unique colors for each card
50
  for (let i = 0; i < 52; i++) {
51
  colors.push(Math.random() * 0xffffff); // Generate random color
52
  }
53
 
 
54
  function shuffleArray(array) {
55
  for (let i = array.length - 1; i > 0; i--) {
56
  const j = Math.floor(Math.random() * (i + 1));
@@ -70,7 +78,7 @@
70
 
71
  // Physics for card
72
  var cardShape = new CANNON.Box(new CANNON.Vec3(2.5, 0.05, 3.5));
73
- var cardBody = new CANNON.Body({ mass: 0.1 }); // Cards are lightweight
74
  cardBody.addShape(cardShape);
75
  cardBody.position.set(x, y, z);
76
  world.addBody(cardBody);
@@ -79,7 +87,7 @@
79
 
80
  // Create a stack of cards
81
  for (let i = 0; i < 52; i++) {
82
- addCard(0, i * 0.2 + 1, 0, i); // Position cards in a stack, slightly offset in the y direction
83
  }
84
 
85
  function animate() {
 
2
  <html>
3
  <head>
4
  <meta charset="UTF-8">
5
+ <title>3D Card Game with Physics</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
+ <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>
 
19
  camera.position.set(0, 30, 100);
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();
 
31
  directionalLight.position.set(0, 1, 1);
32
  scene.add(directionalLight);
33
 
34
+ // Table surface with physics
35
  var tableGeometry = new THREE.BoxGeometry(100, 1, 100);
36
  var tableMaterial = new THREE.MeshLambertMaterial({ color: 0x8B4513 }); // Brown color for the table
37
  var table = new THREE.Mesh(tableGeometry, tableMaterial);
38
+ table.position.set(0, -0.5, 0); // Adjust position to align with the physics body
39
  scene.add(table);
40
 
41
+ // Create physics body for the table
42
+ var tableShape = new CANNON.Box(new CANNON.Vec3(50, 0.5, 50)); // Half extents match the Three.js geometry
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 colors for cards
56
+ var colors = [];
 
57
  for (let i = 0; i < 52; i++) {
58
  colors.push(Math.random() * 0xffffff); // Generate random color
59
  }
60
 
61
+ // Shuffle colors
62
  function shuffleArray(array) {
63
  for (let i = array.length - 1; i > 0; i--) {
64
  const j = Math.floor(Math.random() * (i + 1));
 
78
 
79
  // Physics for card
80
  var cardShape = new CANNON.Box(new CANNON.Vec3(2.5, 0.05, 3.5));
81
+ var cardBody = new CANNON.Body({ mass: 0.1 });
82
  cardBody.addShape(cardShape);
83
  cardBody.position.set(x, y, z);
84
  world.addBody(cardBody);
 
87
 
88
  // Create a stack of cards
89
  for (let i = 0; i < 52; i++) {
90
+ addCard(0, i * 0.2 + 1, 0, i); // Position cards in a stack
91
  }
92
 
93
  function animate() {