Physics example

Started by Thomas., March 10, 2012, 10:29:28 PM

Previous topic - Next topic

Thomas.

Some people are asking for physics on Android devices. Maybe you found my test app and tried how fast is calculated physics on your device. In my opinion, jBullet is slow for games on mobile devices, but if you want to play with it, there are source codes and application. By slide you change height and camera angle, by tap is fired sphere and by long tap you can moving with objects, pinch to zoom.

sources - http://dl.dropbox.com/u/26148874/PhysicsTest.zip
application - http://dl.dropbox.com/u/26148874/PhysicsTest.apk


Thomas.

#1
Speed of native Bullet on android
video: http://www.youtube.com/watch?v=PTP3Joe9D3I
sources:  http://jmonkeyengine.org/groups/android/forum/topic/is-android-viable
and here is some app and sources
http://bompo-blog.appspot.com/2011/3/Physics-Engines-in-libgdx

Is there someone who will try to rewrite sources to jPCT-AE?

Thomas.

#2
I uploaded new version with pinch to zoom support ;)

nilotic

How Can i download the files(new vers.) ??  :'(
I can't find linked site.

Thomas.

Links are in the first post ;)

Thomas.

Sources in first post was updated. Zip contains optimized jBullet lib (thanks EgonOlsen for tips! ;)), adjusted vecmath lib and sources of PhysicsTest app. On my SGS3 physics calculations of 9 boxes take about 37ms with original libs and 29ms with optimized ones. It is almost 30% speed up, it is not much for my app, but maybe it can help to someone.

kbjansen

#6
Woooahh! Thanks for the jBullet optimization, I will try it.

Edit:
Sensastional! The physics are now looking very fluently.
Great Work :D

MarianAldenhoevel

Hello,

I am into the first few hours of learning to build Android apps and my (overambitious) toy-project uses jPCT-AE and jBullet for graphics and physics.

Your example has been fantastic: It gave me a working sample to dissect and play with and flattened the learning-curve a lot for me.

Still I am merely a stumbling idiot in these things, but maybe you can give me a pointer in one simple area: I can't make the initial positions of the boxes rotated.

In your example I tried to hack into addBox():

public void addBox(int x, int y, int z) {
BoxShape shape = new BoxShape(new Vector3f(2, 2, 2));
float mass = 20;
Vector3f localInertia = new Vector3f(0, 0, 0);
shape.calculateLocalInertia(mass, localInertia);

Object3D boxgfx = new Object3D(sBox);
boxgfx.translate(x, y, z);
boxgfx.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);
boxgfx.setCollisionOptimization(Object3D.COLLISION_DETECTION_OPTIMIZED);
boxgfx.build();
world.addObject(boxgfx);

JPCTBulletMotionState ms = new JPCTBulletMotionState(boxgfx);

RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, ms, shape, localInertia);
RigidBody body = new RigidBody(rbInfo);

body.setRestitution(0.3f);
body.setFriction(0.8f);
body.setDamping(0, 0);

body.setUserPointer(boxgfx);
boxgfx.setUserObject(body);
bodyList.add(body);

dynamicWorld.addRigidBody(body);
}


I can see the call to boxgfx.translate that puts the graphical representation of the box into the place specified by the caller. Later that transform is picked up by the MotionState and transferred to the jBullet RigidBody to take part in the physics simulation.

So I thought to make things more interesting I would only have to rotate boxgfx in addition to the translation. So I tried something like

boxgfx.rotateX(30f);

or

boxgfx.rotateAxis(new SimpleVector(0, 1, 0), 30);

but neither makes any difference. The boxes are still neatly aligned with the axes.

For reference here's the MotionState-method that copies from graphic representation to jBullet. It looks pretty to me and seems to handle rotation:

private void setTransformFromGraphic(Transform tran) {
SimpleVector p = SimpleVector.create();
obj3d.getTransformedCenter(p);
tran.origin.set(p.x, -p.y, -p.z);
Matrix matrixGfx = obj3d.getRotationMatrix();
Log.v(JoggleBoardActivity.TAG, matrixGfx.toString());
matrixGfx.fillDump(matDump);
MatrixUtil.getOpenGLSubMatrix(tran.basis, matDump);
}


Thanks to anyone that looks at this questions, I am happy with any hinton what to try or what to read next.

Ciao, MM

EgonOlsen

I think that's because the graphics follow the physics and not vice versa. You would have to rotate the physical box, not the graphical.

MarianAldenhoevel

Thank you very much for your reply.

Do you think I have to work on the physics-side of things even on the initial creation? Translation works fine, the boxes all show up where I want them and then drop under the influence of gravity. I just can't make them appear initially not aligned with the axes.

The graphic objects are created first, then the RigidBodies to go with them and the initial position gets picked up nicely.

I tried your suggestion but to simply rotate the RigidBody I need to understand jBullets Quaternions first, I think. I did try a quick applyTorqueImpulse after the creation, but they did not care and still only fell straight down without any sign of rotation.

I think the problem is in the MotionState setTransformFromGraphic. I have a feeling that does not pick up rotation correctly. But see above for how well I understand that :-).

I'll go back and study a bit...

Ciao, MM

MarianAldenhoevel

Hi,

Sometimes very little education helps a lot!

There was indeed a bug in the original example that never surfaced because the initial cubes are all axis-aligned.

Exhibit A:

private void setTransformFromGraphic(Transform tran) {
SimpleVector p = SimpleVector.create();
obj3d.getTransformedCenter(p);
tran.origin.set(p.x, -p.y, -p.z);
Matrix matrixGfx = obj3d.getRotationMatrix();
matrixGfx.fillDump(matDump);
MatrixUtil.getOpenGLSubMatrix(tran.basis, matDump);
}


This is supposed to update the physics transform tran from the position of the graphic object. But where it says getOpenGLSubMatrix() we need setFromOpenGLSubMatrix(). We want to update tran from matDump, not the other way round.

private void setTransformFromGraphic(Transform tran) {
SimpleVector p = obj3d.getTransformedCenter();
tran.origin.set(p.x, -p.y, -p.z);

Matrix matrixGfx = obj3d.getRotationMatrix();
matrixGfx.fillDump(matDump);
MatrixUtil.setFromOpenGLSubMatrix(tran.basis, matDump);
}


That is all. Works perfectly now.

Thank you very much again! I'm happy for now.

Ciao, MM

Thomas.

#11
On Android 4.4 with ART runtime take calculations only 13ms. It is two times faster than dalvik! :)

EgonOlsen

I always wanted to try that, i was just to lazy to switch runtimes again. Nice performance increase... :)

EgonOlsen

Tried it on a Nexus 9...physics calculations run in approx. 7ms, the device provides steady 60fps.

aleks1283

Quote from: Thomas. on February 23, 2013, 02:28:42 PM
Sources in first post was updated. Zip contains optimized jBullet lib (thanks EgonOlsen for tips! ;)), adjusted vecmath lib and sources of PhysicsTest app. On my SGS3 physics calculations of 9 boxes take about 37ms with original libs and 29ms with optimized ones. It is almost 30% speed up, it is not much for my app, but maybe it can help to someone.
Hello Thomas,give please link of source code jpct-ae+jbullet,link in top not work.