Author Topic: Physics example  (Read 9645 times)

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Physics example
« on: March 10, 2012, 10:29:28 pm »
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

« Last Edit: February 23, 2013, 02:34:29 pm by Thomas. »

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Physics example
« Reply #1 on: April 13, 2012, 11:18:08 pm »
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?
« Last Edit: February 23, 2013, 02:34:05 pm by Thomas. »

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Physics example
« Reply #2 on: May 12, 2012, 12:32:13 pm »
I uploaded new version with pinch to zoom support ;)
« Last Edit: February 23, 2013, 02:33:31 pm by Thomas. »

Offline nilotic

  • byte
  • *
  • Posts: 4
    • View Profile
Re: Physics example
« Reply #3 on: May 13, 2012, 02:35:26 am »
How Can i download the files(new vers.) ??  :'(
I can't find linked site.

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Physics example
« Reply #4 on: May 13, 2012, 10:30:52 am »
Links are in the first post ;)

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Physics example
« Reply #5 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.

Offline kbjansen

  • byte
  • *
  • Posts: 37
    • View Profile
Re: Physics example
« Reply #6 on: March 14, 2013, 10:33:56 am »
Woooahh! Thanks for the jBullet optimization, I will try it.

Edit:
Sensastional! The physics are now looking very fluently.
Great Work :D
« Last Edit: March 15, 2013, 02:22:58 pm by kbjansen »

Offline MarianAldenhoevel

  • byte
  • *
  • Posts: 3
    • View Profile
Re: Physics example
« Reply #7 on: July 26, 2013, 01:02:48 pm »
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():

Code: [Select]
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

Code: [Select]
boxgfx.rotateX(30f);
or

Code: [Select]
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:

Code: [Select]
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

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11771
    • View Profile
    • http://www.jpct.net
Re: Physics example
« Reply #8 on: July 26, 2013, 01:07:57 pm »
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.

Offline MarianAldenhoevel

  • byte
  • *
  • Posts: 3
    • View Profile
Re: Physics example
« Reply #9 on: July 26, 2013, 02:17:45 pm »
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

Offline MarianAldenhoevel

  • byte
  • *
  • Posts: 3
    • View Profile
Got it
« Reply #10 on: July 26, 2013, 03:15:52 pm »
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:

Code: [Select]
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.

Code: [Select]
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

Offline Thomas.

  • double
  • *****
  • Posts: 833
    • View Profile
Re: Physics example
« Reply #11 on: December 19, 2013, 03:54:51 pm »
On Android 4.4 with ART runtime take calculations only 13ms. It is two times faster than dalvik! :)
« Last Edit: December 19, 2013, 09:52:28 pm by Thomas. »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11771
    • View Profile
    • http://www.jpct.net
Re: Physics example
« Reply #12 on: December 19, 2013, 04:51:26 pm »
I always wanted to try that, i was just to lazy to switch runtimes again. Nice performance increase... :)

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11771
    • View Profile
    • http://www.jpct.net
Re: Physics example
« Reply #13 on: December 21, 2014, 01:37:06 pm »
Tried it on a Nexus 9...physics calculations run in approx. 7ms, the device provides steady 60fps.