2014-06-27 26 views
1

我想通過一次性學習Java和子彈物理學的方式與我一路戰鬥。儘管可能有點太多,但我喜歡挑戰。子彈物理,質感的球體不會滾動

到目前爲止,我已經學會了如何導入g3db對象,應用物理學的子彈向他們並通過使用下面的代碼在屏幕上與它們進行交互:

assets = new AssetManager(); 
assets.load("globe.g3db", Model.class); 
assets.load("crate.g3db", Model.class); 
assets.finishLoading(); 

Model model = assets.get("globe.g3db", Model.class); 
ModelInstance inst = new ModelInstance(model); 
inst.transform.trn(0, 20, 0); 

btRigidBody body; 
btSphereShape sh = new btSphereShape(1); 
sh.calculateLocalInertia(1, new Vector3(0,0,0)); 

body = new btRigidBody(new btRigidBody.btRigidBodyConstructionInfo(3, new btDefaultMotionState(inst.transform), sh)); 
body.setUserValue(Minstances.size); 
body.proceedToTransform(inst.transform); 

motionState = new MyMotionState(); 
motionState.transform = inst.transform; 
body.setMotionState(motionState); 

dynamicsWorld.addRigidBody(body); 
Minstances.add(inst); 

這工作得很好,如果我將它設置它落在地面上並停在地面上,然而當它移動時,它會滑動而不是滾動。 有沒有簡單的解決方法?

+0

https://xoppa.github.io/blog/using-the-libgdx-3d-physics-bullet-wrapper-part2/#add-dynamic-properties – Xoppa

回答

0

您還需要讀出並設置旋轉,現在您只需閱讀\應用翻譯。

創建一個新的四元數並獲取xyzw值並將它們應用到您的對象。

是這樣的(C++代碼,但是你可以很容易地做同樣的在Java):

btTransform trans; 
Quat myquat; 
yourBulletDynamicObject->getMotionState()->getWorldTransform(trans); 

btQuaternion rot = trans.getRotation(); 
myquat.w = rot.w(); 
myquat.x = rot.x(); 
myquat.y = rot.z(); 
myquat.z = rot.y(); 

設置myquat回你的對象。

1

要允許身體的滾動,您需要計算其局部慣性並將其提供給施工信息。在你的代碼中,你幾乎做對了。

的方法

btCollisionShape.calculateLocalInertia(float mass, Vector3 inertia) 

確實計算在其第二個參數「的Vector3慣性」本地慣性,但存儲。沒有變化適用於btCollisionShape本身。

獲得的慣性矢量後,您需要將它傳遞給

btRigidBodyConstructionInfo(float mass, btMotionState motionState, btCollisionShape collisionShape, Vector3 localInertia) 

作爲最後一個參數。

正確的代碼應該是這樣的:不需要

btRigidBody body; 
btSphereShape sh = new btSphereShape(1); 
Vector3 inertia = new Vector3(0,0,0); 
sh.calculateLocalInertia(1, inertia); 

body = new btRigidBody(new btRigidBody.btRigidBodyConstructionInfo(3, new btDefaultMotionState(inst.transform), sh, inertia)); 

本地慣性來進行模擬,但是沒有它,所有的力量應用到機構被視爲中央力量,因此可以在不影響角速度。