# How to Reduce surrounding area of a mesh for Collision..??

## Recommended Posts

Hello Everyone... Hope you all are fine..
i have a little query regarding collision of a mesh with a sphere..

i am making a table tennis game and when i am Colliding racket with ball then that collision is not very accurate.

Means i want to say that i am Mesh Imposter for racket and sphere imposter for ball. But in demo you can see racket when racket is little far from ball it is showing collision at that time as well.....

i am not sure that you have understood my question or not..

but you better see demo for understanding. Press C for racket rotation and press L for applying impulse on the ball. and you can see when racket is little far it is also showing collision at that time by changing the colour of the ball.

Here is the Demo.. !!!!

##### Share on other sites

Hi A!    Wow, you are mixing 3rd party physics engine (Oimo/Cannon) with... BJS built-in collision/intersects system.  Interesting and challenging!

I'm no expert in collisions/intersections, but POSSIBLY sphere.ellipsoid COULD need setting.   If you use console.log(sphere.ellipsoid);  returns -> Object { x: 0.5, y: 1, z: 0.5 }.  That is too large for a .2 sphere.  I think .ellipsoid uses radius values... so...  perhaps try sphere.ellipsoid = new BABYLON.Vector3(.1, .1, .1);

The ellipsoid "extends" .1 units from mesh origin in all directions.  SO, that should give you a .2 by .2 by .2 ellipsoid on the sphere.

BUT, I'm not sure .ellipsoid is used with mesh... for intersects.  Perhaps mesh use boundingBoxes instead.

Hopefully, smarter people than I... will comment.

Meantime, you can try the sphere.ellipsoid adjustment.

I struggled with your playground, so I couldn't test sphere.ellipsoid.  The PG reported many "Physics not enabled" errors.  Difficult to do testing.  Sorry.  Simpler playground possible?  One horizontal paddle only, with sphere bouncing vertically atop it, repeatedly, with real low gravity?   Do a simple playground like that, and then we can watch the onCollide happen slower and easier.  *shrug*  (thx)

##### Share on other sites

but .ellipsoid function did'nt help me... i have tried it here on line 53

is there any solution..??? i am just stucked.. :'(

##### Share on other sites

Hi again, A.  Sorry for bad solution.

Couple things, first.  You MUST use CannonJS physics engine... because Oimo does NOT have meshImpostors.  I changed to Cannon in the below playground.

In this playground (with new 'camera2' for racket close-up views), I removed all rotation from rackets. (scene #24 is boring/broken, not worth viewing)

I DID do some tests with r2... but it is difficult to rotate r2 to be horizontally "flat".

(Might need to click RUN twice or more... to cache-up racket models)

In #26, I force r2 (racket 2) to be somewhat flat, and drop low-restitution sphere with low gravity... onto it.  But, no onCollide triggers happening.  I also added .checkCollisions = true... to rackets and table surface, but I forgot to add it to ground.

Sphere->racket2 impact-point looks correct.  (good surface-to-surface impact spacing/distance).

In earlier tests, I saw no onCollide triggering during sphere-bouncing, but when ball came to stop UPON the racket (not sure why it stopped, world.allowSleep was set false)), I DID see an onCollide trigger (ball color change).  It only happened when ball stopped on the racket... near the end of its physics bouncing.

SO, I tried to rotate r2 perfectly flat, which was difficult to do.  It took precision rotation adjusting.  The rackets are not Blender-aligned with the Earth.

In that PG, I removed all racket .rotation and .position settings (made them raw).  Then in lines 336-337 I spin them around Y for fun.  R2 is CLOSE TO being parallel to table surface, but not quite.

There might be ways to bakeCurrentTransformIntoVertices(), to correct alignment problems with the rackets.  But you could also model a new racket, and you can publish your new racket file(s) at your new free github account.

In fact, you only need one .babylon file... with one racket.  We can "clone" or "instance" a second racket, with BJS.

Once we get a nice racket that imports to position 0, 0, 0, with perfectly horizontal or vertical points, perfect pivot-point/origin (pivot at mesh localSpace 0,0,0 or at ball-impact-pad center)... then we can REALLY do some nice racket/sphere collision tests.

Can do?  Can you make us a really nice racket... aligned with the world, with pivot point per your wishes?  Save another playground using your new racket file, then share it with us, here?  If not, its okay.  We might be able to force one racket into nice alignment, then use racket.bakeCurrentTransformIntoVertices().  I would need to think about HOW to do that.

Meantime, I will do some research about onCollide, and we'll beg for help/advice, too.  Things MIGHT have changed when we changed to CannonJS (because of meshImpostors).

I'll keep studying.  Right now, real windy here.  AC power is flickering.  Uh oh!  Wingy dons his hard hat and rubber lineman gloves.    Be well, talk soon.

##### Share on other sites

hey @Wingnut... i changed physics engine to cannonJS earlier. but it was causing problem in applying impulse.

if (evt.keyCode === 76) {
sphere.physicsImpostor = new BABYLON.PhysicsImpostor(sphere, BABYLON.PhysicsImpostor.SphereImpostor, { mass: 1, restitution: 0.9 }, scene);
sphere.physicsImpostor.applyImpulse(new BABYLON.Vector3(0, 7, 0), sphere.getAbsolutePosition());
r2.restitution += 0.1;
}

when i press L. ball goes up and don't come down. it continue to go up. but when i use Oimo.JS the same function works fine...
i don't know whats the problem.. is there any other function for applying impulse in cannonJS or some other thing is causing problem..??

##### Share on other sites

Oh yeah.  I see what you mean.

(May need twice RUN, as before).

"L" keypress (lines 314-318) is out-of-control and unpredictable, even with low-power Y impulse.  hmm.  Sometimes it flies forward, sometimes it flies backwards, and always very powerful.  What the heck?  Something is wrong, somewhere.

I will study this more.  Very strange.  Help/advice from everyone... always welcome.

##### Share on other sites

Okay, here we go...

"L" key applyImpulse is working much better.  Currently set at power 1 in line 315.

In earlier versions, the SECOND import was placed within the onSuccess callback "nesting"... of the FIRST import.  The remainder of the scene code was NOT within this "nest".

SO... I moved almost ALL scene code... into the first import onSuccess callback nest.  You'll see the end of that nest... at line 342.  It helped.

I'm not sure WHY the applyImpulse was "rotten" before making this change.  Working better now, though.

Now, what?   Further onCollide testing.  We need constantly-bouncing ball upon racket or table - for testing.  Then we can watch-for sphereImpostor onCollide events, yes?  Yeah!    sphere.impostor.onCollide trouble-shooting party!

I added line 38 - enabling scene collisions.  No change.  Predicted.  We have no issues with BJS intersect (yet).

Problem is... line 278... we aren't triggering sphere.physicsImpostor.onCollide function.  No reports from line 247.  More study to do.

##### Share on other sites

thanx @Wingnut... i did'nt see your replies in detail yet.. but by reading them.. it seems like you have done something good for me .. like always..
you were saying to built some new racket. i have built one .. hope you like that...
i am attaching babylon file ... check it .. and tell is this what you want??

Now i will look at your answers in detail... i think i am going to enjoy them..

##### Share on other sites

Hi again.  New racket looks ok, but minor "issues".

Top view, without racket scaling...

Note to readers/friends:  This playground sometimes requires extra RUN click.  We could use scene.executeWhenReady() to drop sphere, but .executeWhenReady() and playground don't like each other.  (Sometimes "Loading resources..." stuck displayed).  Could be Wingnut mistake, though... like putting scene.executeWhenReady in wrong hierarchical position (inside/outside loader onSuccess nests).

Anyway, Abdullah... the racket(s) are not X-aligned.  (not aligned atop purple line)  Can you fix that?  It will make your game better/easier to program.

Now we go to sideways view.

Racket2 (r2) is "higher" (more Y position) after I did rotation in line 94.

So, pivot point (origin) on new racket should be y-lowered...  1/2 racket thickness - amount.  We want to rotate racket (like line 94), and racket stays SAME y-position.    (if possible)  (squaring-up the racket)

I think... best idea... put pivot point/origin exactly in center of mesh.

Setting origins/pivot points in Blender is not super-easy.  I think it can be confusing.  Do your best.  I think it is called "origin" in Blender.

Ideally, we want to rotate on ANY axis, and racket does not change position.

But, still... we COULD think about setting origin... to be center of "ball impact pad".  (We would rotate racket AROUND impact area center, instead of mesh center.)  *shrug*  Maybe that is better for this game.  Not sure.

BUT STILL should be in center of racket "thickness" so when we do line 94 rotation... both rackets at same y-position.  Sorry for so much talk.

The new racket is much flatter and squarer, now.  Good job with that!   Better.  We can advance the onCollide testing, easier, in near-future.

But... notice... in #39 demo... red-side of racket... ball rolls toward racket handle.  Odd, and interesting.  Why would red side be slightly tilted (ball rolls toward handle) and black side not tilted (very little ball roll).  hmm.  Funny and interesting, to me.    Is racket "laminated"?  Are there 3 layers to it?  Red layer looks tilted, according to ball roll.

Fun project!  Slow, but fun.

In #39 PG, de-activate line 81.  (it sets racket1 scaling 1 1 1).  Scene changes... MUCH.  Not sure why.  It is bothering me.

##### Share on other sites

Single ball/sphere again.  We have some onCollide action... by using lines 236-239.  (Some "register" lines that I learned from your older playgrounds.)

Lots of onCollides happen when ball rolls across paddle, and when impacting with table, and when rolling across table edges (edges not physics active, so curious WHY), and when impacting floor (ball reset).

Also, I changed SOME ball-recovery handling.  When ball hits floor, I position it back to starting place, and remove all energy from the ball.  Gravity then drops it again.

Progress, right?

I DID see your issue (colliding when there was still space between racket and ball).  Still testing WHEN that happens, but I THINK it is related to scaling the rackets.  POSSIBLY, when a shape/mesh is scaled, and then impostor applied, impostor is mis-shaped or something.  Still studying.  Let's not scale rackets anymore.  Let's make a smaller racket model, or make a bigger table.  I vote for a bigger table, made with meshBuilder, so no scaling there, either.

Aww heck, I'll build a new table... using meshBuilder, with no scaling anywhere.

(Might take repeated RUN clicks, as before.)   Still painted odd... but... we removed a TON of .scaling, including for table parts.  I hope you don't mind the table re-build and variable renaming.  Easier to work-on, now.     Lines 88-97 lets you adjust lots of things. Table should stay in good alignment no matter what values you use.

I'm seeing good onCollide color changes EXCEPT when ball rolls across green surface.  (possibly due to low gravity).  Hitting "L" key to lift ball into air... and letting it drop on green surface... DOES trigger ball color change... so... I think surface onCollide IS working.  Perhaps surface should be createBox and not createGround (line 176).  Racket meshImpostors seem more sensitive to onCollide... than table surface boxImpostor, yes?  *nod*

You don't really WANT onCollide triggering during ball roll, do you?  Only during impact, right?  This is why it might be VERY important to model rackets PERFECTLY... all points on racket impact surface... should be "planar".  All points on ball-impact-area of racket... should be same "height" in modeler.  Using "snaps" in your modeler... helps with that.  All those points... should be parallel with each other.  Understand?  I hope so.  Perhaps they are already that way.

MeshImpostors on the rackets... are acting differently than boxImpostors on the table surface.  Change line 80 (in demo 44) so racket1 is BOX impostor.  Then ball acts differently.  Ball doesn't roll across racket as much, even after repeated or held "L" key impulsings.

BoxImpostor vs. meshImpostor... act differently, don't they?  MeshImpostors follow the shape of the mesh, and if the mesh is bumpy, or tilted, the meshImpostor will ALSO be so.  Perfect racket modeling... using "snaps" on your vertex positioning... making sure racket is very very flat... is important for meshImpostors, I think.  *shrug*

Also, first ball bounce onCollide seems to fail EVERY TIME.  hmm. *scratch scratch* Sorry, I talk SO much.

Are you working-with racket rotation and device orientation remaining in-sync and calibrated?  I saw you ask about that.  You need to constantly maintain the amount of rotation distance SINCE deviceOrientation first turned-on.  The amount of rotational change since scene start.  Device could be tilted in ANY way, but, at scene start, racket uses THAT device-tilt... as ITS 0, 0, 0 rotation.  So there is a constant xyz rotational offset to take into consideration.  Good fun, huh?  It's actually easy to code, though.

##### Share on other sites

thanx @Wingnut... yes i am working on it. racket rotation with device orientation. that is an other issue..
but till now i am just stucked with collision. i was trying changes you offered. but i have a little issue again. when i run the same code which we are running on PG then it gives me this run time error which i have attached below.

this error occur only on sphere.registeronphysicscollide function. and occur only when i check collision of rackets with sphere. all other collisions work fine. what is this..??

##### Share on other sites

Cool.  Umm... perhaps r1 and r2 don't exist at that time.  Maybe we need a little delay.  Look at this:

I disabled lines 226 & 227, and added lines 292-298.  (I wrapped the two "register" commands for rackets... into a scene.executeWhenReady function)

This works good in playground (yay, no stuck "Loading resources..." report), and it MIGHT help with your home version.  You probably understand what it does.  Hope this helps.  Write back, of course.    Advice welcome from all forum users, of course. Thx.  Talk soon.

##### Share on other sites

thanx @Wingnut... yes it helps me ... thanx alot..
Now i have another query for you. well..!!! i disturb you very much.. i am sorryy for that.. but i need help..
i want to set the mass of the sphere when i press L. And before that i want sphere to be still in a position. but when i set the physics imposter of the ball at L key event. sphere behaves like before. Means impulse is applied on the sphere. and ball goes up and don't come down.

i have changed line 43 and added line 265. check it..

##### Share on other sites

hey @Wingnut... i have two more new queries....

1) you know that i am making a multiplayer game. So can you help me that how i can sync the position of the Ball in both scene which are running on different browsers. while server is NodeJS with socket.io.

2) i want to add some audience in my scene like we see in multiplayer games. so how can i add it... ?? any help regarding this as well...

##### Share on other sites

Both are different topics.  We should probably stay on physics road, here.  Besides, I am really dumb in both 1 and 2 subjects.

Yeah, I see the over-powered "L" key, too.  I tried adding a scene.render() just before the impulse (before 266).  Changed things, but, oddly.  heh.  (Spiked the ball against the racket and deflected at extreme angle.)  hmm.  I'll keep thinking and perhaps try some tests.  Capture = true.  (Wingy winks at DK)

##### Share on other sites

okk @Wingnut... try to answer as soon as possible...

until then i try to find some help regarding audience of my scene..

##### Share on other sites

did you find anything..???

##### Share on other sites

What was the question?  I forget.

Did you try some things?  What did you discover?  I am newbie, too.

(I would use video of an audience, or multiple little pieces of video.)

##### Share on other sites

@Wingnut i am waiting for the answer of this question not of audience..

On 3/11/2017 at 4:23 AM, Abdullah said:

thanx @Wingnut... yes it helps me ... thanx alot..
Now i have another query for you. well..!!! i disturb you very much.. i am sorryy for that.. but i need help..
i want to set the mass of the sphere when i press L. And before that i want sphere to be still in a position. but when i set the physics imposter of the ball at L key event. sphere behaves like before. Means impulse is applied on the sphere. and ball goes up and don't come down.

i have changed line 43 and added line 265. check it..

kindly reply me as soon as possible... i did'nt get anything regarding this..

##### Share on other sites

Pretty sure that @Wingnut is starting to feel the first effect of the Puppy Syndrome

##### Share on other sites

Yeah, I think I have a new best friend.  It's all cool... except when the fan club gets as big as DK's fan club.  Then ya need Purina Puppy Plow.  heh

I don't have an answer for that, Abdullah... but... I found a possible work-around.

Way up in line 43... set impostor on ball... 0 mass so it hovers.  All normal there.

Down in lines 265-272 we process the "L" key.  Before, we would set a NEW impostor on sphere (line 268).  This time, I just set a mass on the sphere.

First "L" keypress drops the ball, as one would expect.  But no "applyImpulse" is seen on first "L".  All further "L" keypresses work ok.   hmm.  Creating the impostor... inside the "L" key handler... seems to cause troubles.

Possibilities:
#1:  CannonJS hates applyImpulse immediately after impostor setting.
#2:  CannonJS hates adding impostors... WITHIN a keypress handler. (still a possible 'dispose original' issue).
#3:  About 200 other potential causes.

Hope this helps.  Physics engine is sure being grumpy, huh?  *nod*  Let's keep experimenting.  Teach us things you discover, please.  thx!   It helps all of us understand CannonJS third-party physics engine.  (Wingnut checks the bottoms of his shoes... for some goofy reason)

I accidentally started another PG series... oops.  http://playground.babylonjs.com/index2_5.html#DWKPV#8  Feel free to hack-up that series with demented experiments and many more saves.  Sorry for the repeat PG series.  The playground app assigned the new URL when I wasn't paying attention.  heh

##### Share on other sites

i disturb you alot and i reallyy feel sorryy for that... but whenever i need help ... you come to my mind before google...  :D

well..!!!! jokes a part...

Actuallyy i am quite busy now a days ... so thats why i think i didn't express my knowledge right here in a right manner...
but i i know i am learning alot...

thanx friend.... =D =D

##### Share on other sites

It's all cool, A, thx for petting me!

Yeah, it looked like you were in a hurry.  No problems... thanks for being cognizant of your forum personality.  I appreciate the kind words and thoughts.  I'm a little slow sometimes, and quite inexperienced, too.

I hope I don't accidentally tell you wrong things.  I worry about that.  Watch out for Wingnut mistakes... ALWAYS.

Party on!

## 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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.