三次元物理演算エンジン、Cannon.jsを使ってみた。


Physijs 使three.jsPhysijs使Three.jsCannon.js使

20141027_img
GitHub20149v0.6.010Cannon.jsv0.6.0

/demos/使/build/cannon.demo.jsJS/build/cannon.demo.js

/sample01.html


20141027_img2






CANNON.js使3D
2013cannon.js





/sample01.html 32
// 物理世界を生成
world = new CANNON.World();
// 重力を設定
world.gravity.set(0, -9.82, 0);
// ぶつかっている「可能性のある」剛体同士を見つける作業
world.broadphase = new CANNON.NaiveBroadphase();
// 反復計算回数
world.solver.iterations = 10;
// 許容値
world.solver.tolerance = 0.1;


01

/sample01.html 43
// 地面用にPlaneの剛体を質量0で生成
phyPlane = new CANNON.Body({mass: 0});
phyPlane.addShape(new CANNON.Plane());
// X軸に90度に回転
phyPlane.quaternion.setFromAxisAngle(new CANNON.Vec3(1, 0, 0), -Math.PI / 2);
// 物理世界に追加
world.add(phyPlane);
// Boxのシェイプの剛体を質量1で生成
phyBox = new CANNON.Body({mass: 1});
phyBox.addShape(new CANNON.Box(new CANNON.Vec3(1, 1, 1)));
phyBox.position.y = 10;
// Z軸に10の角速度を設定
phyBox.angularVelocity.set(10, 10, 10);
// 減衰率
phyBox.angularDamping = 0.1;
// 物理世界に追加
world.add(phyBox);

new CANNON.RigidBody()new CANNON.Body()addShape()


var plane = new CANNON.Plane();  
phyPlane = new CANNON.RigidBody(0, plane);


phyPlane = new CANNON.Body({mass: 0});
phyPlane.addShape(new CANNON.Plane());

three.js


three.js





(一)

(二)copy()three.js

(三)render()


/sample01.html 109
function animate() {
    requestAnimationFrame(animate);
    // 物理エンジンの時間を進める
    world.step(1 / 60);
    viewCube.position.copy(phyBox.position);
    viewCube.quaternion.copy(phyBox.quaternion);
    // レンダリング
    renderer.render(scene, camera);
}

heightfield

/sample02.html


20141027_img3

Heightfield


/sample01.htmlnew CANNON.Heightfield()matrixraycastVehicle使

/sample02.html 59
// 地面用にPlaneの剛体を質量0で生成
phyGround= new CANNON.Body({mass: 0});
var matrix = [];
var sizeX = 100;
var sizeY = 100;
for (var i = 0; i < sizeX; i++) {
    matrix.push([]);
    for (var j = 0; j < sizeY; j++) {
        var height = Math.cos(i / sizeX * Math.PI * 5) * Math.cos(j/sizeY * Math.PI * 5) * 2 + 2;
        if(i===0 || i === sizeX-1 || j===0 || j === sizeY-1)
            height = 3;
        matrix[i].push(height);
    }
}
phyGround.addShape(new CANNON.Heightfield(matrix, {
    elementSize: 100 / sizeX
}));

three.jsHeightfield


/build/cannon.demo.jsHeightfieldthree.jsthree.js

/sample02.html 128
function createViewGround() {
    var material = new THREE.MeshPhongMaterial({
        color: 0x000000,
        specular: 0xffffff,
        shininess: 0
    });
    viewGround = new THREE.Object3D();
    for (var l = 0; l < phyGround.shapes.length; l++) {
        var shape = phyGround.shapes[l];
        var geometry = new THREE.Geometry();
        var v0 = new CANNON.Vec3();
        var v1 = new CANNON.Vec3();
        var v2 = new CANNON.Vec3();
        for (var xi = 0; xi < shape.data.length - 1; xi++) {
            for (var yi = 0; yi < shape.data[xi].length - 1; yi++) {
                for (var k = 0; k < 2; k++) {
                    shape.getConvexTrianglePillar(xi, yi, k===0);
                    v0.copy(shape.pillarConvex.vertices[0]);
                    v1.copy(shape.pillarConvex.vertices[1]);
                    v2.copy(shape.pillarConvex.vertices[2]);
                    v0.vadd(shape.pillarOffset, v0);
                    v1.vadd(shape.pillarOffset, v1);
                    v2.vadd(shape.pillarOffset, v2);
                    geometry.vertices.push(
                            new THREE.Vector3(v0.x, v0.y, v0.z),
                            new THREE.Vector3(v1.x, v1.y, v1.z),
                            new THREE.Vector3(v2.x, v2.y, v2.z)
                    );
                    var i = geometry.vertices.length - 3;
                    geometry.faces.push(new THREE.Face3(i, i+1, i+2));
                }
            }
        }
        geometry.computeBoundingSphere();
        geometry.computeFaceNormals();
        var mesh = new THREE.Mesh(geometry, material);
        mesh.receiveShadow = true;
        mesh.castShadow = true;
        if(mesh.children){
            for(var i=0; i<mesh.children.length; i++){
                mesh.children[i].castShadow = true;
                mesh.children[i].receiveShadow = true;
                if(mesh.children[i]){
                    for(var j=0; j<mesh.children[i].length; j++){
                        mesh.children[i].children[j].castShadow = true;
                        mesh.children[i].children[j].receiveShadow = true;
                    }
                }
            }
        }
        viewGround.add(mesh);
    }
    viewGround.position.copy(phyGround.position);
    viewGround.quaternion.copy(phyGround.quaternion);
    scene.add(viewGround);
}


Plane


sample03.html


使three.jsPhysijsthree.jsthree.jscopy()three.js使

三次元物理演算エンジン、Cannon.jsを使ってみた。」への1件のフィードバック

  1. ピンバック: Cannon.jsのRaycastVehicleのデモ | KnockKnock

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です