5

我一直在寻找这个结论,但我似乎无法找到任何解决方案。

我正在尝试创建一个简单的场景,以便使用 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();
}

这是创建具有角色和世界的“级别”的功能。关卡对象还包含一个更新函数,它在调用 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;
}

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

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

4

2 回答 2

1

行。首先,您不应该使用速度__dirtyPosition来工作。我建议使用setLinearVelocityand getLinearVelocity

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
}

其次,您遇到此问题的原因可能是因为您没有将__dirtyRotation标志设置为true. 您有一个小区域要重置旋转。除此之外,我认为您发布的内容没有任何问题。

于 2015-11-04T01:31:16.323 回答
0

对于单个立方体角色移动,请尝试object.setLinearVelocity(vector)而不是使用object.__dirtyPosition,

我认为__dirtyPosition通常会禁用大部分物理效果

于 2014-11-24T03:16:36.967 回答