Javascript 3D旋转其他对象中的独立对象

Javascript 3D旋转其他对象中的独立对象,javascript,three.js,Javascript,Three.js,在我的程序中,我有四个Physijs.BoxMesh(…),它们都被添加到一个独立的对象中。我想旋转四个物理框,但一旦它们都添加到第五个对象,它们就不会旋转了。有没有办法旋转包含的对象 编辑:以下是我的全部代码: <body></body> <script src="http://gamingJS.com/Three.js"></script> <script src="http://gamingJS.com/physi.js"><

在我的程序中,我有四个
Physijs.BoxMesh(…)
,它们都被添加到一个独立的对象中。我想旋转四个物理框,但一旦它们都添加到第五个对象,它们就不会旋转了。有没有办法旋转包含的对象

编辑:以下是我的全部代码:

<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>

//物理设置
Physijs.scripts.ammo=http://gamingJS.com/ammo.js';
Physijs.scripts.worker=http://gamingJS.com/physijs_worker.js';
//这就是我们游戏中发生的事情:
var scene=new Physijs.scene({fixedtimestop:2/60});
场景.setGravity(新的3.Vector3(0,-100,0));
//这就是我们看到的东西:
变量宽度=window.innerWidth,
高度=窗内高度,
纵横比=宽度/高度;
var摄像机=新的三透视摄像机(75,纵横比,1,10000);
//var摄像机=新的三轴正交摄像机(
//-宽度/2,宽度/2,高度/2,-高度/2,1,10000
// );
摄像机位置z=500;
场景。添加(摄影机);
//这将把摄像机看到的东西绘制到屏幕上:
var renderer=new THREE.CanvasRenderer();
renderer.setSize(window.innerWidth、window.innerHeight);
document.body.appendChild(renderer.doElement);
document.body.style.backgroundColor='#ffffff';
//*******在下一行开始编码********
var m1=新的Physijs.BoxMesh(新的三元立方计量法(20,150,20),新的三元网格基本材质({color:0x000000}));
var m2=新的Physijs.BoxMesh(新的3.CubeGeometry(20,150,20),新的3.MeshBasicMaterial({color:0x000000}));
var m3=新的Physijs.BoxMesh(新的3.CubeGeometry(20,150,20),新的3.MeshBasicMaterial({color:0x000000}));
var m4=新的Physijs.BoxMesh(新的三个立方体测量法(20,150,20),新的三个.MeshBasicMaterial({color:0x000000}));
var ground=新的Physijs.BoxMesh(新的三立方测量法(5e2,10,5e2),新的三网格基本材料({color:0x0000ff}),0);
var tars=new Physijs.BoxMesh(new THREE.CubeGeometry(1,1,1),new THREE.MeshBasicMaterial());
m1.uu dirtyPosition=true;
m1.位置x=-30;
m2.uu dirtyPosition=true;
m2.位置x=-10;
m3._方向定位=真;
m3.位置x=10;
m4.uuu dirtyPosition=true;
m4.位置x=30;
接地。uu dirtyPosition=true;
地面位置y=-300;
焦油添加量(m1);
焦油添加量(m2);
焦油添加量(m3);
焦油添加量(m4);
场景。添加(焦油);//如果我在下面的文档添加侦听器中添加Tars化身,m1.setAngularVelocity(…)实际上不会旋转m1块
场景。添加(地面);
document.addEventListener('keydown',函数(事件){
开关(event.keyCode){
案例13:
m1.设定角速度(新的三矢量3(1,0,0));
打破
案例100:
m2.设定角速度(新的三矢量3(1,0,0));
打破
案例101:
m3.设定角速度(新的三矢量3(1,0,0));
打破
案例102:
m4.设定角速度(新三矢量3(1,0,0));
打破
}
});
//在游戏中设置运动动画
函数animate(){
请求动画帧(动画);
渲染器。渲染(场景、摄影机);
}
制作动画();
//运行物理
函数gameStep(){
scene.simulate();
//每秒更新物理60次,以使运动平滑
设置超时(游戏步骤,1000/60);
}
gameStep();

这将不起作用,因为Physijs网格具有无法从父对象Object3D继承的属性。例如,Object3D不能具有角动量


获得所需行为的正确方法是使用。

@2pha我现在添加了代码。