www.jpct.net

jPCT-AE - a 3d engine for Android => Projects => Topic started by: Thomas. on March 10, 2012, 10:29:28 pm

Title: Physics example
Post by: Thomas. 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 (http://dl.dropbox.com/u/26148874/PhysicsTest.zip)
application - http://dl.dropbox.com/u/26148874/PhysicsTest.apk (http://dl.dropbox.com/u/26148874/PhysicsTest.apk)

(http://i44.tinypic.com/f4pu08.png)
Title: Re: Physics example
Post by: Thomas. 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?
Title: Re: Physics example
Post by: Thomas. on May 12, 2012, 12:32:13 pm
I uploaded new version with pinch to zoom support ;)
Title: Re: Physics example
Post by: nilotic on May 13, 2012, 02:35:26 am
How Can i download the files(new vers.) ??  :'(
I can't find linked site.
Title: Re: Physics example
Post by: Thomas. on May 13, 2012, 10:30:52 am
Links are in the first post ;)
Title: Re: Physics example
Post by: 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.
Title: Re: Physics example
Post by: kbjansen 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
Title: Re: Physics example
Post by: MarianAldenhoevel 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
Title: Re: Physics example
Post by: EgonOlsen 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.
Title: Re: Physics example
Post by: MarianAldenhoevel 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
Title: Got it
Post by: MarianAldenhoevel 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
Title: Re: Physics example
Post by: Thomas. 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! :)
Title: Re: Physics example
Post by: EgonOlsen 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... :)
Title: Re: Physics example
Post by: EgonOlsen 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.
Title: Re: Physics example
Post by: aleks1283 on April 25, 2021, 08:04:05 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.
Title: Re: Physics example
Post by: AeroShark333 on April 25, 2021, 08:56:16 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.

I believe the Dropbox links have died since the post is from years ago.
I once experimented with jBullet too... but the performance was too poor for my likings I believe.
Though, I might still have the jBullet version that was originally posted here somewhere on my computer.
I can try to look for it if you're really interested...

Otherwise, maybe this will help:
https://www.jpct.net/forum2/index.php?topic=1509.0

Title: Re: Physics example
Post by: EgonOlsen on April 26, 2021, 01:57:47 pm
This should be the code Thomas. released back then: https://jpct.de/download/example/PhysicsTest.zip (https://jpct.de/download/example/PhysicsTest.zip)

Please note: I've just zipped my project directory. It might contain a lot of unneeded stuff and was created for an Android-SDK from ages ago. That said, it still compiled, uploaded and worked fine for me.
Title: Re: Physics example
Post by: aleks1283 on April 28, 2021, 12:26:45 am
Thanks. Is there an alternative to jbullet but not slow?
Title: Re: Physics example
Post by: AeroShark333 on April 28, 2021, 12:36:57 am
Thanks. Is there an alternative to jbullet but not slow?
I believe there's jMonkeyEngine... (which can work without jPCT as well..)
But I'm not sure how well it works together with jPCT.

An alternative is probably to decompile jBullet and optimize it somehow... (making it multithreaded or something..?)
Title: Re: Physics example
Post by: aleks1283 on April 28, 2021, 08:25:23 am
Well, if I understand correctly, threads do not add speed on devices, but only transfer work to the background.
Title: Re: Physics example
Post by: AeroShark333 on April 30, 2021, 06:27:54 am
Well, if I understand correctly, threads do not add speed on devices, but only transfer work to the background.
I'm not entirely sure about this...
I believe you could off-load some heavy physics calculations on a seperate thread than the GL-thread.
When heavy calculations are done in the GL-thread, you might suffer from lower FPS due to reduced draw(...) call rates.
I think this might especially give speed boosts on multi-core processor devices (nearly all devices nowadays) which would handle multithreading better.
Though, making sure the threads are synced and working well together might be more complex...
jPCT(-AE)/OpenGL(ES) isn't entirely multi-thread friendly as far as I know.

Maybe @EgonOlsen knows more about this...
Title: Re: Physics example
Post by: EgonOlsen on May 01, 2021, 01:08:13 pm
The "old" purpose of multiple threads is indeed to do slow IO operations in the background. But nowadays, given that all processors have multiple cores/threads (the machine I'm writing this on can handle 24 threads in parallel, for example), it also very common to use multiple threads for calculations.
That said, it's not easy to use multiple threads for physics, because the result of one thread's calculation can have an impact on the other. Imagine 8 boxes colliding with each other. If you calculate each box in it's own thread, the outcome of one would depend on the collisions of all the others, which is almost impossible to synchronize properly.
jPCT(-AE) itself isn't thread safe (but you could do calculations on independant objects or tasks in multiple threads) and neither is OpenGL(ES). If you want to use multi-threading in one way or the other, you have to make sure that the tasks that are processed by the different threads are independant (for example: One thread for physics, one for sound, one for path finding...).
Title: Re: Physics example
Post by: aleks1283 on May 13, 2021, 06:39:08 am
Thanks Egon :)
Title: Re: Physics example
Post by: aleks1283 on May 21, 2021, 02:14:13 pm
Hello Egon and Everybody.)
Is it possible to use standard jpct-ae functions to somehow cover two different objects with common skin or clothing? I want to apply this to physical modeling with sutavas .
Title: Re: Physics example
Post by: EgonOlsen on May 21, 2021, 02:55:12 pm
I'm not sure what you mean by "cover with clothing"!?
Title: Re: Physics example
Post by: aleks1283 on May 21, 2021, 06:15:50 pm
Well, there is a model of a person, let's say, made up of cylinders. And at the junctions of these cylinders, you can see that these are separate cylinders. Is it possible to cover the human figure with some kind of mesh so that the person becomes like a single figure but with moving arms and legsneck ...?
Smooth transition between.

Title: Re: Physics example
Post by: aleks1283 on May 21, 2021, 10:10:26 pm
So that the mesh stretches when moving parts of the human body
Title: Re: Physics example
Post by: aleks1283 on May 21, 2021, 10:33:00 pm
and apply a texture to the mesh
Title: Re: Physics example
Post by: AeroShark333 on May 22, 2021, 12:12:17 pm
I believe you would want the 'joints' to appear smoothly visually?

I guess you could try this: https://github.com/raftAtGit/Bones
(see also: https://www.jpct.net/forum2/index.php/board,10.0.html)

I am not entirely sure how it works but I believe you'd need one complete mesh of the person (including arms, legs, etc.)
And somehow define the body parts using the library
Title: Re: Physics example
Post by: EgonOlsen on May 22, 2021, 12:52:29 pm
I see. There's no functionality that does this automatically. You have to model it that or come up with some algorithm to do it (which won't be trivial...).
Title: Re: Physics example
Post by: aleks1283 on May 22, 2021, 02:38:41 pm
Thanks Egon.
In general, my next project is very non-trivial. :) :) :)
Title: Re: Physics example
Post by: aleks1283 on May 27, 2021, 12:15:14 am
GenerateVertexController does not work in real time, makes a change in the object from the value (0) of the variable to "private" once and does not work anymore. I looked through the whole forum and I probably did it right. I also use jbullet in the project. Could it beis it that GenerateVertexController is not compatible with Jbullet in the same project?
Title: Re: Physics example
Post by: EgonOlsen on May 27, 2021, 08:48:11 am
...makes a change in the object from the value (0) of the variable to "private" once and does not work anymore.
I don't understand what you mean by that!? Are you calling applyVertexController() on the mesh that it has been assigned to? Also, you have to make sure that the object gets recognized as "dynamic", which usually happens automatically, but it might fail if you assign the controller after the first use of the object. In that case, try to explicitly call Object3D.compile(true) on the Object3D in question and see if that helps.
Title: Re: Physics example
Post by: aleks1283 on May 27, 2021, 12:52:05 pm
I can dynamically create the object I want using getPolygonManager. But it seems to me that it will be too slow, since it will be necessary to add an object to the scene and draw a texture on it every time.
Title: Re: Physics example
Post by: aleks1283 on May 27, 2021, 02:33:56 pm
      Everything started to work.
Added to onDrawFrame -     

            cyl7.compile(true);
            cyl7.touch();
            objSkinCyl7.compile(true);
            objSkinCyl7.touch();
            cyl7.getMesh().applyVertexController();
            objSkinCyl7.getMesh().applyVertexController();
Title: Re: Physics example
Post by: aleks1283 on May 27, 2021, 05:20:48 pm
How get global(world) coordinate of vertex from mesh?In GenerateVertexController
Title: Re: Physics example
Post by: AeroShark333 on May 27, 2021, 09:41:18 pm
How get global(world) coordinate of vertex from mesh?In GenerateVertexController
I guess you'd need to pass your Object3D instance to your custom implementation of GenericVertexController.
Then using Object3D.getTransfornedCenter() to find the mesh's center in world coordinates. And using the rotations applied to the Object3D and GenericVertexController.getSourceMesh() to find the individual world coordinates.
...

However, maybe multiplying the sourceMesh with object3d.getWorldTransformation() could work too... Seems to go from object space to world space..?
Title: Re: Physics example
Post by: aleks1283 on May 28, 2021, 06:24:11 pm
 //Dont copy object. -

VVV = new GenericVertexController() {
            @Override
            public void apply() {         
                SimpleVector[] aaa = getSourceMesh();
                SimpleVector[] bbb = getDestinationMesh();           
                                int lll=aaa.length;
                                for (int f=0;f<lll;f++)
                                {
                                    //bbb[f].matMul(objSkinCyl7.getWorldTransformation());
                                    bbb[f].x = X77[f];
                                    bbb[f].y = Y77[f];
                                    bbb[f].z = Z77[f];
                                }
            }
        };
        VVV2 = new GenericVertexController() {
            @Override
            public void apply() {
                SimpleVector[] aaa = getSourceMesh();       
                int lll=aaa.length;
                SimpleVector[] hhh = new SimpleVector[lll];             
                X77 = new float[lll];
                Y77 = new float[lll];
                Z77 = new float[lll];
                for (int f=0;f<lll;f++)
                {                 
                    aaa[f].matMul(cyl7.getWorldTransformation());                           
                    X77[f] = aaa[f].x;
                    Y77[f] = aaa[f].y;
                    Z77[f] = aaa[f].z;
                }
            }
        };
cyl7.setVisibility(false);

///////////
///////////
///////////
//Copy object,but wrong rotate. Infinite rotation -


VVV = new GenericVertexController() {
            @Override
            public void apply() {             
                SimpleVector[] aaa = getSourceMesh();
                SimpleVector[] bbb = getDestinationMesh();

                                int lll=aaa.length;
                                for (int f=0;f<lll;f++)
                                {
                                    //bbb[f].matMul(objSkinCyl7.getWorldTransformation());
                                    bbb[f].x = X77[f];
                                    bbb[f].y = Y77[f];
                                    bbb[f].z = Z77[f];
                                }
            }
        };
        VVV2 = new GenericVertexController() {
            @Override
            public void apply() {               
                SimpleVector[] aaa = getSourceMesh();             
                int lll=aaa.length;
                SimpleVector[] hhh = new SimpleVector[lll];               
                X77 = new float[lll];
                Y77 = new float[lll];
                Z77 = new float[lll];
                for (int f=0;f<lll;f++)
                {                   
                    aaa[f].matMul(cyl7.getRotationMatrix());                 
                    X77[f] = aaa[f].x + cyl7.getTranslation().x;
                    Y77[f] = aaa[f].y + cyl7.getTranslation().y;
                    Z77[f] = aaa[f].z + cyl7.getTranslation().z;
                }
            }
        };

cyl7.setVisibility(false);
Title: Re: Physics example
Post by: aleks1283 on May 29, 2021, 11:25:08 am
Why when I call the multiplication of each vector of the point of the object by the matrix(getRotationMatrix), the incorrect rotation of the object (infinite rotation) starts? ..
Title: Re: Physics example
Post by: AeroShark333 on May 29, 2021, 12:20:59 pm
I thought you only wanted to look up the coordinates of each vertex in world space rather than object space... I don't think the destination mesh should be given in world space coordinates, however...
Title: Re: Physics example
Post by: aleks1283 on May 29, 2021, 12:55:13 pm
Yes,every vertex in world space.
My target mesh will consist of interconnected meshes of several objects, so to speak, it's easy to visualize the fragmented parts - skin tight muscles. Therefore, I need to get the global coordinates of the vertices of the objects as they look in the world inplace with rotation angles, and the target mesh will have to give a copy of the selected objects, and in places where I need this target mesh, I will link with new polygons .
Title: Re: Physics example
Post by: aleks1283 on May 29, 2021, 01:40:59 pm
SimpleVector[] aaa = getSourceMesh();
aaa[f].matMul(cyl7.getWorldTransformation()); - return wrong world coordinate
Title: Re: Physics example
Post by: AeroShark333 on May 29, 2021, 02:07:10 pm
Maybe this would work:

aaa[f].matMul(cyl7.getRotationMatrix());
Then:
aaa[f].matMul(cyl7.getTranslationMatrix());

I'm not sure if this will work... I can't test this out right now, but it would somewhat make sense I hoped... 😅 Otherwise, maybe Egon has better ideas
Title: Re: Physics example
Post by: aleks1283 on May 29, 2021, 02:22:14 pm
Not work

 aaa[f].matMul(cyl1.getTranslationMatrix()); or aaa[f].matMul(cyl1.getWorldTransformation()); - returns coordinates of order 6234 or 2323 in world space when the original(Source object cyl7) coordinates are - 6 ... 7 in world space
Title: Re: Physics example
Post by: aleks1283 on May 29, 2021, 02:27:57 pm
When i do - aaa[f].matMul(cyl7.getRotationMatrix());   // or  aaa[f].rotate(cyl7.getRotationMatrix());         
                    X77[f] = aaa[f].x + cyl7.getTranslation().x;
                    Y77[f] = aaa[f].y + cyl7.getTranslation().y;
                    Z77[f] = aaa[f].z + cyl7.getTranslation().z; all good(coordinates,and the shape of the object) but object wrong(infinite) rotation,instead of the original turn
Title: Re: Physics example
Post by: AeroShark333 on May 29, 2021, 08:28:16 pm
When i do - aaa[f].matMul(cyl7.getRotationMatrix());   // or  aaa[f].rotate(cyl7.getRotationMatrix());         
                    X77[f] = aaa[f].x + cyl7.getTranslation().x;
                    Y77[f] = aaa[f].y + cyl7.getTranslation().y;
                    Z77[f] = aaa[f].z + cyl7.getTranslation().z; all good(coordinates,and the shape of the object) but object wrong(infinite) rotation,instead of the original turn
Then do you need the inverse matrix of the rotationmatrix? I believe there's an option... You can also manually align the sourcemesh to the object mesh I guess...
I'm not sure if it is easily possible
Title: Re: Physics example
Post by: aleks1283 on May 29, 2021, 08:42:34 pm
I understand what the problem is. If initially the object had some kind of rotation angle or movement, then when the frames are updated and when multiplied by the matrix, it does not become in its proper place, but adds value every time. Even if the original object is no longermoves and does not turn.how to solve this problem ???
So I don't like these vector movements)))
Title: Re: Physics example
Post by: AeroShark333 on May 29, 2021, 09:12:28 pm
I believe if values are repetitively added, I suppose you should do an inverse operation at the end of what you are doing..? So maybe the inverse matrix or something would help (Matrix.invert()).

What you could also do is not apply the initial rotation at first but only at the end maybe. So after your vertex controller things..?

I'm not sure if I entirely understand your problem. It's hard to understand if you don't see what's happening
Title: Re: Physics example
Post by: aleks1283 on May 29, 2021, 09:41:09 pm
Thanks, I hope you see a cool game this year.
I need something like object.clearRotation and preferably object.clearTranslation to apply to sourceMesh.clearRotation and sourceMesh.clearTranslation.
Title: Re: Physics example
Post by: AeroShark333 on May 29, 2021, 10:18:49 pm
Thanks, I hope you see a cool game this year.
I need something like object.clearRotation and preferably object.clearTranslation to apply to sourceMesh.clearRotation and sourceMesh.clearTranslation.
Good luck ;)
Title: Re: Physics example
Post by: aleks1283 on May 30, 2021, 02:23:42 pm
 I defeated the first dragon !!! Fixed.Working.


VVV2 = new GenericVertexController() {
            @Override
            public void apply() {
                SimpleVector[] aaa = getSourceMesh();
                int lll=aaa.length;
                X77 = new float[lll];
                Y77 = new float[lll];
                Z77 = new float[lll];
                for (int f=0;f<lll;f++)
                {
                    aaa[f].matMul(cyl7.getWorldTransformation());
                    X77[f] = aaa[f].x;
                    Y77[f] = aaa[f].y;
                    Z77[f] = aaa[f].z;
                    aaa[f].matMul(cyl7.getWorldTransformation().invert());
                }
            }
        };