summaryrefslogtreecommitdiff
path: root/test_physijs.ts
blob: dc3fd2e7e60481ece5558765b422830acb8a8f35 (plain)
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
///<reference path="three_js/ts/three.d.ts"/>
///<reference path="physi_js/physijs.d.ts"/>

Physijs.scripts.ammo = "../ammo_js/ammo.js";
Physijs.scripts.worker = "physi_js/physijs_worker.js";

let renderer, camera, scene, box, plane;

function init() {
    renderer = new THREE.WebGLRenderer();
    renderer.setClearColor(0xffffff);
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    scene = new Physijs.Scene;
    scene.setGravity(10);


    scene.addEventListener("update", function () {
        scene.simulate(undefined, 1);
    });

    camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1000);
    scene.add(camera);

    let boxMaterial = Physijs.createMaterial(new THREE.MeshLambertMaterial({
        color: 0xaf0000
    }));

    box = new Physijs.BoxMesh(
        new THREE.BoxGeometry(4, 4, 4),
        boxMaterial,
        1
    );
    box.position.set(0,5,0);
    scene.add(box);

    let groundMaterial = Physijs.createMaterial(new THREE.MeshLambertMaterial({
        color: 0xfafafa
    }));

    plane = new Physijs.BoxMesh(
        new THREE.BoxGeometry(100, 1, 100),
        groundMaterial,
        0
    );
    scene.add(plane);

    requestAnimationFrame(render);
    scene.simulate();
}

function render() {
    requestAnimationFrame(render);
    box.applyCentralForce(new THREE.Vector3(1, 0, 0));
    renderer.render(scene, camera);
}

window.onload = init;