Physijs,结合运动和物理

Ama*_*rik 5 javascript physics three.js physijs

我一直在寻找一段时间以获得结论,但我似乎无法找到任何解决方案.

我正在尝试使用THREE.JS和PHYSI.JS创建一个简单的场景来获得一个小平台动作.我有一个质量为0的扁平盒子作为我的工作,还有一个盒子网格作为我的角色.Physijs的网站似乎建议我将__dirtyPosition变量设置为true.这里的问题是:__dirtyPosition与物理混淆.当我将角色从水平边缘移开时,他并没有摔倒.事实上,当它发生碰撞时,它似乎会停在世界立方体的中间位置.

当我尝试更新位置而不应用时__dirtyPosition,立方体会被放回原位.它有点颤抖,好像它有一个小小的癫痫发作,就是这样.如果我可以同时使用物理和运动,那就太棒了,使用Physi进行碰撞就像是过度使用它.这是我现在的表现,显然是错误的方式:

level1.generateScene = function()
{
   level1.camera = new THREE.PerspectiveCamera(75, this.cameraWidth / this.cameraHeight, 0.1, 1000);

   level1.material = new AdapterEngine.createBasicMaterial({color: 0x00ff00}); //Threejs basic material
   level1.ground = new AdapterEngine.createBoxMesh(5, 0.1, 5, level1.material, 0); //Physijs box mesh with a mass of 0

   level1.playerMaterial = new AdapterEngine.createBasicMaterial({color:0x0000ff}, 0.6, 0.3); //Physijs basic material with a fruction of 0.6, and a restitution of 0.3
   level1.player = new AdapterEngine.createBoxMesh(0.3, 0.3, 0.3, level1.playerMaterial, 0.1); //Physijs box mesh with a mass of 0.1
   level1.player.y = 50; //yes, it should fall down for a bit.

   level1.scene.add(level1.ground);
   level1.scene.add(level1.player);

   level1.camera.position.z = 5;
   level1.camera.position.y = 1.4;
   level1.activeCamera = level1.camera;
   level1.controls.init();
}
Run Code Online (Sandbox Code Playgroud)

这是"水平"的功能,同时创建了角色和世界.level对象还包含的是一个更新函数,它在调用requestAnimationframe函数之后以及渲染器渲染之前播放.

level1.update = function()
{
   this.scene.simulate();

   level1.player.__dirtyPosition = true;
   if(level1.controls.isKeyDown('RIGHT')) level1.xvelocity = 0.1;
   else if(level1.controls.isKeyDown('LEFT')) level1.xvelocity = -0.1;
   else if(level1.controls.isKeyUp('RIGHT') || level1.controls.isKeyUp('LEFT')) level1.xvelocity = 0;

   level1.player.rotation.x = 0;
   level1.player.rotation.y = 0;
   level1.player.rotation.z = 0;

   level1.player.position.x = level1.player.position.x + 0.5*level1.xvelocity;
}
Run Code Online (Sandbox Code Playgroud)

我知道出现了类似的问题,但没有一个真正产生令人满意的答案.我真的不知道该做什么,或者我应该使用哪些功能.

编辑 我忘了添加这一部分:我一直在使用一个小框架来确定我是否应该获得一个THREE.js对象或一个PHYSI.js对象,它可以作为我的游戏和我的库之间的桥梁.它们遵循与THREE.js和PHYSI.js相同的参数,我用注释标记哪个函数返回哪种类型的对象.

小智 1

好的。首先,你应该习惯于__dirtyPosition快速工作。我建议使用setLinearVelocitygetLinearVelocity

level1.update = function(){
    this.scene.simulate();

    // Conditions...

    var oldVector = this.player.getLinearVelocity(); // Vector of velocity the player already has
    var playerVec3 = new THREE.Vector3(oldVector.x + .5 * this.xvelocity, oldVector.y, oldVector.z);
    this.player.setLinearVelocity(playerVec3); // We use an updated vector to redefine its velocity
}
Run Code Online (Sandbox Code Playgroud)

其次,您遇到此问题的原因可能是因为您没有将标志设置__dirtyRotationtrue。您有一个小区域想要重置旋转。除此之外,我从你发布的内容中看不出有什么问题。