2015-05-10 79 views
2

在我的程序中,我有四個Physijs.BoxMesh(...),它們都被添加到一個獨立的對象中。我想旋轉四個物理盒,但是一旦它們全部添加到第五個物體,它們將不會旋轉。有什麼方法可以旋轉包含的對象嗎?Javascript 3D將獨立對象旋轉到其他對象內

編輯:這是我的所有代碼:

<body></body> 
<script src="http://gamingJS.com/Three.js"></script> 
<script src="http://gamingJS.com/physi.js"></script> 
<script src="http://gamingJS.com/ChromeFixes.js"></script> 

<script> 
    // Physics settings 
    Physijs.scripts.ammo = 'http://gamingJS.com/ammo.js'; 
    Physijs.scripts.worker = 'http://gamingJS.com/physijs_worker.js'; 

    // This is where stuff in our game will happen: 
    var scene = new Physijs.Scene({ fixedTimeStep: 2/60 }); 
    scene.setGravity(new THREE.Vector3(0, -100, 0)); 

    // This is what sees the stuff: 
    var width = window.innerWidth, 
     height = window.innerHeight, 
     aspect_ratio = width/height; 
    var camera = new THREE.PerspectiveCamera(75, aspect_ratio, 1, 10000); 
    // var camera = new THREE.OrthographicCamera(
    // -width/2, width/2, height/2, -height/2, 1, 10000 
    //); 

    camera.position.z = 500; 
    scene.add(camera); 

    // This will draw what the camera sees onto the screen: 
    var renderer = new THREE.CanvasRenderer(); 
    renderer.setSize(window.innerWidth, window.innerHeight); 
    document.body.appendChild(renderer.domElement); 
    document.body.style.backgroundColor = '#ffffff'; 

    // ******** START CODING ON THE NEXT LINE ******** 

    var m1 = new Physijs.BoxMesh(new THREE.CubeGeometry(20, 150, 20), new THREE.MeshBasicMaterial({color:0x000000})); 
    var m2 = new Physijs.BoxMesh(new THREE.CubeGeometry(20, 150, 20), new THREE.MeshBasicMaterial({color:0x000000})); 
    var m3 = new Physijs.BoxMesh(new THREE.CubeGeometry(20, 150, 20), new THREE.MeshBasicMaterial({color:0x000000})); 
    var m4 = new Physijs.BoxMesh(new THREE.CubeGeometry(20, 150, 20), new THREE.MeshBasicMaterial({color:0x000000})); 
    var ground = new Physijs.BoxMesh(new THREE.CubeGeometry(5e2, 10, 5e2), new THREE.MeshBasicMaterial({color:0x0000ff}), 0); 

    var tars = new Physijs.BoxMesh(new THREE.CubeGeometry(1, 1, 1), new THREE.MeshBasicMaterial()); 

    m1.__dirtyPosition = true; 
    m1.position.x = -30; 
    m2.__dirtyPosition = true; 
    m2.position.x = -10; 
    m3.__dirtyPosition = true; 
    m3.position.x = 10; 
    m4.__dirtyPosition = true; 
    m4.position.x = 30; 
    ground.__dirtyPosition = true; 
    ground.position.y = -300; 

    tars.add(m1); 
    tars.add(m2); 
    tars.add(m3); 
    tars.add(m4); 

    scene.add(tars); // If I add Tars the Avatar, below at the document add listener, m1.setAngularVelocity(...) will not actually rotate the m1 block 

    scene.add(ground); 

    document.addEventListener('keydown', function(event) { 
    switch(event.keyCode) { 
     case 13: 
     m1.setAngularVelocity(new THREE.Vector3(1, 0, 0)); 
     break; 

     case 100: 
     m2.setAngularVelocity(new THREE.Vector3(1, 0, 0)); 
     break; 

     case 101: 
     m3.setAngularVelocity(new THREE.Vector3(1, 0, 0)); 
     break; 

     case 102: 
     m4.setAngularVelocity(new THREE.Vector3(1, 0, 0)); 
     break; 
    } 
    }); 

    // Animate motion in the game 
    function animate() { 
    requestAnimationFrame(animate); 
    renderer.render(scene, camera); 
    } 
    animate(); 

    // Run physics 
    function gameStep() { 
    scene.simulate(); 
    // Update physics 60 times a second so that motion is smooth 
    setTimeout(gameStep, 1000/60); 
    } 
    gameStep(); 
</script> 
+0

告訴我們一些代碼 – 2pha

+0

@ 2pha我現在已經添加了代碼。 – HyperNeutrino

回答

0

這不會起作用,因爲Physijs網具有不能從父Object3D繼承屬性。例如,Object3D不能有角動量。

獲得所需行爲的正確方法是使用Physijs constraints