atallah salah

How to use collision in OimoJS with BABYLONJS

Recommended Posts

I'm trying to use collision Mesh with other mesh the problem in CanonJS it's support sphere only and when i use OimoJS Mesh collision not work correctly this example by BABYLONJS:

https://playground.babylonjs.com/#3B3135#1

I'm trying to change

scene.enablePhysics();

to

scene.enablePhysics(new BABYLON.Vector3(0,-9.81, 0), new BABYLON.OimoJSPlugin());

but when i change it something wrong happen to the Mesh collision

i think it give sphere or box collision to the Mesh imported

Share this post


Link to post
Share on other sites

Hiya AS... welcome to the forum!

I think this is expected.  Only CannonJS, the default physics engine... allows meshImpostor, and yes, it only collides with spheres.

Instead-of using meshImpostor, and sometimes WITH the use of meshImpostor, some folks "surround" the edges of a mesh... with invisible boxes or spheres... and put impostors on those.

These are like invisible "physics bumpers".  They might be put along the sides and on the bumpers of car models.  These can be LONG boxes, so only one is needed for each side of the car, and only one is needed for each bumper on the car's front and rear.

Give some thought to using invisible mesh... with impostors... to make edge-of-model bumpers or collided-object bumpers.

Stay tuned... others might have more good comments.

Share this post


Link to post
Share on other sites

Some of these settings are essential for Oimo to function as you want:

 
this.name = "OimoJSPlugin";
this._tmpImpostorsArray = [];
this._tmpPositionVector = BABYLON.Vector3.Zero();
this.world = new OIMO.World(1 / 60, 2, iterations, true);
this.world.clear();
            OIMO.WORLD_SCALE = 1;
            OIMO.INV_SCALE = 1;
this.world.isNoStat = true;
 
Especially 
            OIMO.WORLD_SCALE = 1;
            OIMO.INV_SCALE = 1;
Try these first and play with the values if these don't work exactly as you need. But this should get the behaviors you're seeking.
 
DB

Share this post


Link to post
Share on other sites
1 hour ago, dbawel said:

Some of these settings are essential for Oimo to function as you want:

this.name = "OimoJSPlugin";
this._tmpImpostorsArray = [];
this._tmpPositionVector = BABYLON.Vector3.Zero();
this.world = new OIMO.World(1 / 60, 2, iterations, true);
this.world.clear();
            OIMO.WORLD_SCALE = 1;
            OIMO.INV_SCALE = 1;
this.world.isNoStat = true;

First thank you Wingnut and Dbawel
I try to use this settings and change values but still not work
to get more clear what i mean :
CannonJS :


OimoJS :

 

Share this post


Link to post
Share on other sites

@atallah salah

I'm not certain what behavior you're looking for, but here's two PG's using both engines modified. I believe I can modify them to you needs, if I know specifically what you're looking for.

cannon.js

https://playground.babylonjs.com/#3B3135#8

This will work for you if you separate the jaw from the rest of the head and assign each an impostor.

 

oimo.js

https://playground.babylonjs.com/#3B3135#10

The framerate is low due to how you are creating geometry. All you need to do is clone your spheres, and the FPS increases dramatically.

Let me know if either is the behavior you're looking for.

DB

 

 

Share this post


Link to post
Share on other sites

@dbawel

What i mean if you see the cannon.js it's realy take Mesh impostor 
but when use Mesh impostor  in oimo.js it's look like using Box impostor  or Sphere impostor

what i'm planning to do is to have collision with meshes and tubes

i have here 3D object

image.png.b673fb58f1ad69f2cd94338e1f4efc97.png

 

if you see it have space in center

in cannon.js when i use Mesh impostor it see the space in this object and work fine but only for spheres

in omio.js when i use Mesh impostor it not see the space or any of corners or edges of the 3D object it's look like oimo.js take it as Box impostor

in previous example you can see spheres go to inside Skull when using cannon.js but it's not wok fine in oimo.js

Thank you


 

Share this post


Link to post
Share on other sites

@atallah salah

Oimo.js only has box impostors. I went ahead and cloned your spheres, and it only improves framerate when there are few collisions being calculated. First, replace the HUGE mesh "skull" with geometry that you will use or something resembling your mesh above. The biggest problem is that creating a MeshImposter for the skull is using most of your CPU/GPU. Also, considering you aren't creating a material in babylon, you aren't able to turn off back face culling. It sounds like you need to use cannon.js (the default physics engine), clone as much as you can, optimize your scene, and you'll run at a reasonable fps - depending on how you calculate your collisions. But replacing the skull will do most of the work for you.

Here's a PG scene cloning the spheres. You also don't need the default resolution of 32 when creating your spheres, although it's not really affecting your framerate. It's the Skull's MeshImposter collision calculations. You'll get the result you want, it hasn't failed me in production yet.

https://playground.babylonjs.com/#3B3135#11

DB

Share this post


Link to post
Share on other sites

Again, Oimo has no meshImpostor... and likely installs a "fallback" boxImpostor... whenever somebody tries to use a meshImpostor with Oimo.  Expected behavior.

Perhaps we should issue a console warning or error... if someone tries to use a meshImpostor with Oimo.

@dbawel I think Atallah was ONLY using the skull as an example mesh.  I think the mesh in the picture... is the actual project mesh.

This is going to be a physics challenge, for sure.

Atallah, can you publish the mesh in the picture... (.babylon file?)... to somewhere where we could load it into the playground?

I use my free github account... to store small models.  Then I can load them into a playground, and we can more easily do tests.

https://github.com/Wingnutt/misc

In that folder, you can see my bjs_text.babylon file.

Now look at this playground:  https://www.babylonjs-playground.com/#10MM3T#1

See the odd url base?  https://cdn.rawgit.com/Wingnutt/misc/master/  + bjs_text.babylon

Can you, somehow, get YOUR model from the picture... to load into a playground like this?  That would be great... and would let us try some more tests.  Thx.

Models used in playgrounds must load from a CORS-clear location.  CORS is a security thing which you can read about... on the web.

Share this post


Link to post
Share on other sites

@Wingnut, @atallah salah

Yes, he was only using the skull as a test mesh. However, it's about the worst mesh I can think of to calculate MeshImposter collisions. I just added scene optimization to the PG scene, and no effect when there are many collisions:

https://playground.babylonjs.com/#3B3135#12

I agree, an alert or console warning should come up when using oimo - at least on the playground. I went through the very same issue 2 years ago. However, oimo is great for some physics scenes, and incredibly efficient.

Cheers,

DB

Share this post


Link to post
Share on other sites

haha.  Yeah, the skull is a GRUESOME meshImpostor.  Bumpier than a Sarah Huckabee Sanders press conference.  ;)   (USA joke)

Let's pretend we DO NOT use a meshImpostor on Atallah's shape.  I guess... we could position (and parent) about 24 invisible boxes... onto Atallah's model... and put a box impostor on each one (but NO impostor on the shape itself).  Then... it can collide with things, and its hole stays open.  The hole will have 4 flat boxImpostors inside the hole, so other box impostors can slide thru the hole and get friction and guiding.  It would then act similar to a meshImpostor... but not use a meshImpostor.  So, no more limitations.... either physics engine.

*shrug*  It will help if we can get Atallah's model into playground.  Then we can test the performance of 24 invisible "bumper impostors" surrounding the shape and lining the hole.

Share this post


Link to post
Share on other sites

Thanks  @dbawel @Wingnut

i'm really still beginner in Babylon.js 
yes i'm using the mesh in picture for the project
 

28 minutes ago, Wingnut said:

Now look at this playground:  https://www.babylonjs-playground.com/#10MM3T#1

See the odd url base?  https://cdn.rawgit.com/Wingnutt/misc/master/  + bjs_text.babylon

 

i try this way but it's give error " shape is undefined"

here is example code of what i'm planing to do :


scene.enablePhysics();

let Sphere = BABYLON.MeshBuilder.CreateSphere("sphere",{diameter:6},scene)
Sphere.position.x=-35;
Sphere.position.y=2;
Sphere.rotation.y=-.7;

Sphere.physicsImpostor = new BABYLON.PhysicsImpostor(Sphere, BABYLON.PhysicsImpostor.SphereImpostor, {mass: 0, friction: 0, restitution: .3});


BABYLON.SceneLoader.ImportMeshAsync("SL_bracket_005_8","modal/", "Brackets.babylon", scene).then(function (bracket) {
    bracket=bracket.meshes
    camera.target = bracket[0];
    bracket[0].scaling={x:20,y:20,z:20}
    bracket[0].physicsImpostor = new BABYLON.PhysicsImpostor(bracket[0], BABYLON.PhysicsImpostor.MeshImpostor, {mass:1, friction: 0, restitution: .3});

})

 

i upload the mesh file

i'm using sphere in above code but i need to use something else like other meshes ,tubes and lines ...

Brackets.babylon

Share this post


Link to post
Share on other sites

https://www.babylonjs-playground.com/#10MM3T#7

I temporarily published your .babylon file in my misc folder at github.  I'll delete it on your command, Atallah. 

I didn't use the ImportMeshAsync method, but we can adjust anything, now. 

Grab a zip of that playground if you wish, A.

These are parts of dental braces, aren't they?  Cooooool.  :)

Sooo... 14 brackets... 24 invisble boxImpostors each... OUCH!  hmm.  Perhaps my "bumper impostors" idea is not so good.  Thinkin'.

The wire (not shown)... needs an impostor, too, eh?  OUCH MORE!  

New version:  https://www.babylonjs-playground.com/#10MM3T#8  I reduced the camera.minZ so we can mouseWheel real close. 

Remember control-drag feature... for "slewing" the camera target... handy for close-up bracket examination.

Share this post


Link to post
Share on other sites

Atallah... what will you use physics for?  Will you be trying to measure the amount of (wire) pull... between each bracket?  We might be able to do that by NOT putting impostors on the wire... but instead.... attach a physics "joint" between each bracket.  Probably, a "spring joint" or lock joint.  These joints have separation length settings.

Let's find a playground with some physics joints (joints are invisible, but still apply physics forces).

http://playground.babylonjs.com/#1KPF49#41

Hinge joints are applied between each chain bead.

The bead-gap (joint length)... can be adjusted at line 130.

Line 74 setPhysicsLinkWith is the thing that installs the hinge joints.  It's a helper function.  You can also get much more complex with joint-adding... experimenting with many types of joints, and using the "addJoint" command instead of setPhysicsLinkWith.  There's lots of power... in joints... but our physics plugins try to make it all easier... with helper functions.

http://schteppe.github.io/cannon.js/docs/

Cannon often calls its joints "constraints".  See the "distance constraint" in the list?  THAT could be a good one... for pulling/pushing brackets towards/apart-from each other.  hmm.

Still thinkin.  Maybe joints between each bracket... can somehow be useful.  Not sure, yet.  Thoughts? 

MeshImpostors or boxImpostors... might both work fine.  The wire would be for looks-only.  Fake.  :)   It doesn't really push/pull, it just LOOKS LIKE it does.  The invisible joints actually do all the pushing/pulling.

Share this post


Link to post
Share on other sites

Thank you Wingnut

I really  like your ideas

but i think try all possibilities even if we need to build specific physics for each mesh or object
and maybe we may have to use wire as mesh 3d object


i understand it's possible to do it may it's hard but still possible  do you think ?

is there other library may help us  ?

Share this post


Link to post
Share on other sites

My pleasure.

You haven't told me what you wish to "show" or "do" with the physics, yet.

Are you going to fly the brackets thru an asteroid field in space, and you want the asteroids to bounce-off?   :)

Think about this.  All boxImpostor brackets sit on a 0-mass (stays in place)... INVISIBLE physics-active ground.  Each bracket has VERY HIGH mass (and perhaps friction, but ground friction might cause impostors to tip-over when pulled by distance/lock joint).  Now put SOME joint in-between each bracket... probably a lock or distance joint.  NOT a hinge of any type, for that will make your brackets act like a worm/snake...(like my chain-o-beads).  We want a straight pull from the joint... just like the wire would do.

On distance joints... you can simply decrease the distance value... as the technician (using some gui sliders, maybe) tightens the wire between two brackets.  Increase distance... loosens the wire.

The distance value of a distance joint... CAN make two very-heavy brackets... move a tiny bit closer/further-apart.  In a way, it is a "torque" value, yes?

SO, yes, with a simple box impostor on each bracket, and some distance joints, and an invisible ground, and high mass on the brackets (reluctance to move, just like teeth)... SOME things could be visualized here.  What would be seen or learned... I dunno.  I don't know what your "big hope" is... and you might not want to say in public... because it is secret.  :) You can send me a personal message... I promise not to tell/steal secret.  I am not that type of person. 

To be frank, I don't know WHAT adjustments can be done with braces.  Can they y-rotation "twist" a tooth?  Can they change a tooth's z-rotation "tilt".  x-rotation tilt?  We can MAYBE do the same... with THREE joints between each bracket.  :x

lockJoints are different from distanceJoints... and may not have a "distance".  As you work-with joints more, you'll see that a joint CAN have a mainPivotPoint and a connectedPivotPoint.  You can move those two values... to change the length of a lock joint.

A springJoint... can do this, too... and some springJoints (down deep at native levels) have a restLength.  And, they have damping.  Set damping REAL HIGH... to make the springJoint VERY stiff... and then the amount of "torque" between two brackets... IS the springJoint.restLength.

Phew, lots of info to learn, huh?  Fancy physics work.  I hope you don't get a brain tumor.   :)

Share this post


Link to post
Share on other sites

@atallah salah

@Wingnut and I still don't understand specifically what you want to do. If you describe in detail as best as you can, this would allow us and everyone on the forum to provide you with the best solutions or at least several potential solutions. And no, there's not another physics extension currently compatible with babylon.js. However, between cannon.js and oimo.js as well as other available methods, there's nothing I can currently think of that you won't be able to accomplish in your scene. However, once we know more, we have to get a bit creative, but there's allot of experienced devs on this forum that are happy to help.

Being a beginner is not so easy, but using this forum and all of the devs resources available, you'll be a pro before you know it. At least you picked the correct framework, as I took the long way to babylon.

Cheers,

DB

Share this post


Link to post
Share on other sites

Thank you @Wingnut @dbawel

i was thinking i should not enter you into complexity because that i use skull and other thing as example
 

if you see these bracket i want to have Elastic(Rubber) between 2 bracket at least

you know maybe between these 2 bracket i will have object (teeth,wire..) the problem is if i want to draw a tube as Elastic between 2 bracket we don't want it to overlap with other object on his way

this also applies to wire maybe one of bracket will have rotate more than other bracket and we don't want the wire to overlap with teeth or bracket

think about if i want to draw a line between 2 mesh and i have other mesh in the way we don't want to overlap with other mesh


 

Share this post


Link to post
Share on other sites

Hi A.  Yep, you're going to be bending some lines.  The elastic is not a straight line, so the linesMesh segments must bend, too.

Invisible joint between those two anchors... no problems.  Drawn line?  More problem.  Sorry. 

BJS multi-picking (ray-casting) could probably tell you WHICH mesh would be intersected... IF a straight line was drawn between the two anchors... but it could NOT tell the BJS LinesMesh system... how to automatically "go-around" those intersected mesh. 

Elastic-adding could become a complex task.  I have no easy solutions.  Let's hope others have some ideas.

Share this post


Link to post
Share on other sites

@atallah salah

Believe it or not, I had many similar animations to produce in the past. I can tell you from my experience that the simplest method is to bind your geometry which needs to stretch to a detailed chain of bones (bones hierarchy.) then when you need to stretch, just scale the chain on the Z-axis most likely from the root of the bones hierarchy. Solving for intersections can be handled by parenting bones to simple box meshes, and avoiding collisions with transparent simple geometry on the teeth - or whatever. There are other ways to solve this, but you could build this system fully working in just a couple of hours - and it would work flawlessly - with a little tweaking of course.

DB

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.