File size: 2,869 Bytes
c29f49d
 
09c7acd
facffbf
 
 
 
 
 
 
 
09c7acd
 
facffbf
09c7acd
facffbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09c7acd
facffbf
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
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>3D Card Game</title>
    <style>
        body { margin: 0; }
        canvas { display: block; }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/cannon.js/0.6.2/cannon.min.js"></script> <!-- For physics -->
</head>
<body>
    <canvas id="gameCanvas"></canvas>

    <script>
        var scene = new THREE.Scene();
        var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.set(0, 30, 100);
        var renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('gameCanvas') });
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);

        // Physics world setup
        var world = new CANNON.World();
        world.gravity.set(0, -9.82, 0); // Apply gravity in the negative y direction

        // Card geometry
        var cardGeometry = new THREE.BoxGeometry(5, 0.1, 7);
        var cardMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
        var cards = [];
        var cardBodies = [];

        // Lighting
        var ambientLight = new THREE.AmbientLight(0x404040);
        scene.add(ambientLight);
        var directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
        directionalLight.position.set(0, 1, 1);
        scene.add(directionalLight);

        // Card creation
        function addCard(x, y, z) {
            var card = new THREE.Mesh(cardGeometry, cardMaterial);
            card.position.set(x, y, z);
            scene.add(card);
            cards.push(card);

            // Physics for card
            var cardShape = new CANNON.Box(new CANNON.Vec3(2.5, 0.05, 3.5));
            var cardBody = new CANNON.Body({ mass: 1 });
            cardBody.addShape(cardShape);
            cardBody.position.set(x, y, z);
            world.addBody(cardBody);
            cardBodies.push(cardBody);
        }

        // Example: Add a stack of cards
        for (let i = 0; i < 52; i++) {
            addCard(0, i * 0.2, 0); // Adjust position as needed
        }

        // Drag and drop functionality (simplified)
        // You will need to implement raycasting to pick up and move cards

        function animate() {
            requestAnimationFrame(animate);

            // Update physics world
            world.step(1 / 60);

            // Sync Three.js objects with physics objects
            for (let i = 0; i < cardBodies.length; i++) {
                cards[i].position.copy(cardBodies[i].position);
                cards[i].quaternion.copy(cardBodies[i].quaternion);
            }

            renderer.render(scene, camera);
        }

        animate();
    </script>
</body>
</html>