期待される成果ではない:ボックスは地面にドロップしますと、それは何が起こっている
を「ボックスがちょうど地面を打つ」という警告ボックスが生成されます:アラートボックスが作成されていません。関連するjavascriptコンソールログも衝突時に生成されません。
私はmy github repoの小さなコードベースを共有しています。クローンしてクロームブラウザで自分で実行することができます。ソースコード内の**** scripts/app.js ****ファイルのphysijsBox.addEventListener()
部分を調べることができます。
var sceneObj = (function(){
"use strict";
Physijs.scripts.worker = "scripts/physijs_worker.js";
Physijs.scripts.ammo = "ammo.js";
var scene, camera, renderer
var physijsBox, physijsGround
function initScene(){
scene = new Physijs.Scene();
scene.setGravity = new THREE.Vector3(0, -50, 0);
camera = new THREE.PerspectiveCamera(35, window.innerWidth/window.innerHeight , 1, 1000);
camera.position.z = 100;
renderer = window.WebGLRenderingContext ? new THREE.WebGLRenderer() : new THREE.CanvasRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById("webgl-container").appendChild(renderer.domElement);
addPhysijsBox();
addPhysijsGround();
render();
}
function addPhysijsBox(){
var myBoxMaterial = Physijs.createMaterial(
new THREE.MeshBasicMaterial({
color: 0xff00ff
}),
0, // friction
0.8 // restitution/bounciness
);
physijsBox = new Physijs.BoxMesh(new THREE.CubeGeometry(15,15,15), myBoxMaterial);
physijsBox.position.set(0,30,10);
physijsBox.rotation.set(0,50,90);
scene.add(physijsBox);
physijsBox.addEventListener('collision', function(
theOtherObject, linearVelocity, angularVelocity, arg4
){
console.log("box collided with something");
if (theOtherObject.name == "ground"){
alert("Box just hit the ground");
}
})
}
function addPhysijsGround(){
var myGroundMaterial = Physijs.createMaterial(
new THREE.MeshBasicMaterial({
color: 0x008888
}),
0, // friction
0.4 // restitution/bounciness
);
physijsGround = new Physijs.BoxMesh(new THREE.CubeGeometry(150, 3, 150), myGroundMaterial, 0);
physijsGround.name = "ground";
physijsGround.position.y = -15;
scene.add(physijsGround);
}
function render(){
scene.simulate();
renderer.render(scene, camera);
requestAnimationFrame(render);
}
window.onLoad = initScene();
return scene;
})();
関連PhysiJSドキュメント:
- https://github.com/chandlerprall/Physijs/wiki/Collisions
- https://github.com/chandlerprall/Physijs/issues/177
なぜ両方のオブジェクトの摩擦を0に設定していますか? 「インパルス」の計算は、摩擦の係数に関連している。インパルスの大きさは、物理エンジンが衝突AFAIKを検出するために使用するものです。インパルス計算における摩擦の関係はここで与えられています - http://www.euclideanspace.com/physics/dynamics/collision/threed/ –
摩擦がある程度の量を与えてもらえますか? –
摩擦を設定しても改善はありません。コードをプルして実行すると、実際に衝突が発生していることがわかります。落下する箱はコースを変え、地面に当たると跳ね返ります。しかし何らかの理由で、衝突イベントが 'physijsBox.addEventListener( 'collision'、function(){})' –