2014-04-15 24 views
5

我一直在尋找一段時間,爲這個結論,但我似乎無法找到任何解決方案。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(); 
} 

這就是「等級」,與字符和世界時生成的函數。 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; 
} 

我知道類似的問題已經出現,但沒有人真的得到了滿意的答案。我真的無所適從,或者我應該使用哪些功能。

編輯 我忘了補充這一部分:我一直在使用一個小的框架,確定自己是否應該得到一個three.js所對象或PHYSI.js對象,它作爲我的比賽,之間的橋樑我庫。這些遵循與THREE.js和PHYSI.js相同的參數,我用註釋標記了哪些函數返回哪種類型的對象。

+0

我喜歡這麼多的物理引擎根本沒有實現一個合適的運動學角色控制器。等等,然後等。討厭。 – jozxyqk

回答

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 
} 

其次重要的是,你有這樣的問題,可能是因爲你沒有在__dirtyRotation標誌設置爲true的原因。你有一個想要重置旋轉的小區域。除此之外,我沒有看到你發佈的內容有任何問題。

0

對於只是一個單一的立方體人物移動, 嘗試object.setLinearVelocity(vector)而不是使用object.__dirtyPosition

我覺得__dirtyPosition一般禁用大多數的物理效果