# Physics Engine question

**URL:** <https://forum.babylonjs.com/t/physics-engine-question/2214>\
**Category:** Questions\
**Created:** [March 28, 2019, 7:49pm UTC](https://forum.babylonjs.com/t/physics-engine-question/2214 "2019-03-28T19:49:17Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![trevordev](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/trevordev/32/16_2.png) [@trevordev](https://forum.babylonjs.com/u/trevordev)\
**Post date:** [March 28, 2019, 9:29pm UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/23 "2019-03-28T21:29:13Z")

</div>

good find 🙂 looks promising, if someone has a decent repro I can try to add a fix using that method.

---

<div class="post-metadata">

**Author:** ![mauricedoepke](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mauricedoepke/32/1360_2.png) [@mauricedoepke](https://forum.babylonjs.com/u/mauricedoepke)\
**Post date:** [March 28, 2019, 9:30pm UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/24 "2019-03-28T21:30:22Z")

</div>

You can simulate falling manually, but you still need some kind collision checking. Four your style of game, you could have a metadata object for every platform, that stores at which angle is a hole. Based on that you can do a very simple custom collision checking without a big physics engine.

---

<div class="post-metadata">

**Author:** ![mauricedoepke](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mauricedoepke/32/1360_2.png) [@mauricedoepke](https://forum.babylonjs.com/u/mauricedoepke)\
**Post date:** [March 28, 2019, 9:32pm UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/25 "2019-03-28T21:32:33Z")

</div>

@trevordev I try to provide one tomorrow.

---

<div class="post-metadata">

**Author:** ![Asagi](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@Asagi](https://forum.babylonjs.com/u/Asagi)\
**Post date:** [March 29, 2019, 4:09am UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/26 "2019-03-29T04:09:26Z")

</div>

Can you show some examples?

---

<div class="post-metadata">

**Author:** ![mauricedoepke](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mauricedoepke/32/1360_2.png) [@mauricedoepke](https://forum.babylonjs.com/u/mauricedoepke)\
**Post date:** [March 29, 2019, 10:52am UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/27 "2019-03-29T10:52:33Z")

</div>

I’d try something like his for the metadata:

> ```
> platforms: [
> {id: "whatever", mesh: plaformMesh, holes: [{from: 0, from: Math.PI/4}]}
> {id: "whatever2", mesh: plaformMesh2, holes: [{from: 0, size: Math.PI/4},{from: Math.PI, size: Math.PI/4}]}
> ] 
> 
> ```

With that metadata you coud even genereate the platforms by code.  
Use somehing like his for gravity: [software engineering - How can I implement gravity? - Game Development Stack Exchange](https://gamedev.stackexchange.com/questions/15708/how-can-i-implement-gravity/41917)

And with the metadata + the roation of your platforms you can check whether the ball is on a platform or a hole. I would however only use his if he holes are of fixed size. Otherwise his approach gets complicated fast.

If you want it easy, you need to be patient, till this bug is fixed.

---

<div class="post-metadata">

**Author:** ![mauricedoepke](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mauricedoepke/32/1360_2.png) [@mauricedoepke](https://forum.babylonjs.com/u/mauricedoepke)\
**Post date:** [March 29, 2019, 11:34am UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/28 "2019-03-29T11:34:56Z")

</div>

Here a set of playgrounds @trevordev

With restitution set to one and colliding the mid of the ground, everything is fine, I got 1 even.  
[https://playground.babylonjs.com/#RG2AQ4#2](https://playground.babylonjs.com/#RG2AQ4#2)  
The edge case with restitution set to 1, this produces 4 events for me, but should only give 1:  
[https://playground.babylonjs.com/#RG2AQ4#5](https://playground.babylonjs.com/#RG2AQ4#5)

@Asagi 's case: Produces always 2 events for me, should be 1  
[https://playground.babylonjs.com/#RG2AQ4#3](https://playground.babylonjs.com/#RG2AQ4#3)  
Asagis Edge case: Produces 2-5 events for me, should be only 1, too  
[https://playground.babylonjs.com/#RG2AQ4#4](https://playground.babylonjs.com/#RG2AQ4#4)

---

<div class="post-metadata">

**Author:** ![Asagi](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@Asagi](https://forum.babylonjs.com/u/Asagi)\
**Post date:** [March 29, 2019, 11:39am UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/29 "2019-03-29T11:39:46Z")

</div>

May be problem with mesh? For ex. platforms?

---

<div class="post-metadata">

**Author:** ![mauricedoepke](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mauricedoepke/32/1360_2.png) [@mauricedoepke](https://forum.babylonjs.com/u/mauricedoepke)\
**Post date:** [March 29, 2019, 11:44am UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/30 "2019-03-29T11:44:17Z")

</div>

I don’t know, but you could upload a platform mesh then we can take a quick look at it.

---

<div class="post-metadata">

**Author:** ![Asagi](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@Asagi](https://forum.babylonjs.com/u/Asagi)\
**Post date:** [March 29, 2019, 12:09pm UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/31 "2019-03-29T12:09:05Z")

</div>

One platform for example.  
[http://www.mediafire.com/file/x4152q3cb0vzntv/p1.babylon/file](http://www.mediafire.com/file/x4152q3cb0vzntv/p1.babylon/file)

---

<div class="post-metadata">

**Author:** ![mauricedoepke](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mauricedoepke/32/1360_2.png) [@mauricedoepke](https://forum.babylonjs.com/u/mauricedoepke)\
**Post date:** [March 29, 2019, 12:38pm UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/32 "2019-03-29T12:38:15Z")

</div>

This smoothed out edge could be a problem. Making that a regular sharp edge might be a temporary solution until the bug is fixed.

![edge](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/9/9452c111130b990e5d5369a077382a7614c264b0.png)

---

<div class="post-metadata">

**Author:** ![Asagi](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@Asagi](https://forum.babylonjs.com/u/Asagi)\
**Post date:** [March 29, 2019, 12:55pm UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/33 "2019-03-29T12:55:53Z")

</div>

Hm. Thanks, i rebuild meshes. But what about double event when ball hit? This is no problem with mesh platform?

---

<div class="post-metadata">

**Author:** ![mauricedoepke](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mauricedoepke/32/1360_2.png) [@mauricedoepke](https://forum.babylonjs.com/u/mauricedoepke)\
**Post date:** [March 29, 2019, 12:59pm UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/34 "2019-03-29T12:59:18Z")

</div>

Yea this seems to be a bug. I also can’t guarantee that rebuilding the meshes will fix it, is just an idea.

Before you rebuild them, you might want to try commenting out your apply force and set restitution to 1 for the ball and the discs, like this:  
[https://playground.babylonjs.com/#RG2AQ4#2](https://playground.babylonjs.com/#RG2AQ4#2)

---

<div class="post-metadata">

**Author:** ![Asagi](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@Asagi](https://forum.babylonjs.com/u/Asagi)\
**Post date:** [March 29, 2019, 1:22pm UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/35 "2019-03-29T13:22:44Z")

</div>

Can i do next?  
Manualy move sphere and check collision with platform mesh? Platform and voids in it?

---

<div class="post-metadata">

**Author:** ![trevordev](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/trevordev/32/16_2.png) [@trevordev](https://forum.babylonjs.com/u/trevordev)\
**Post date:** [March 29, 2019, 5:19pm UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/36 "2019-03-29T17:19:50Z")

</div>

So far when i’m investigating I’ve found the the double event isn’t even on the same simulation frame, even when checking the manifolds like you shared, the same issue is occurring. For some reason the sphere enters the object and bounces up but doesnt get fully out of the object. Right now i’m investigating if the calls to stepSimulation might be causing this.

---

<div class="post-metadata">

**Author:** ![mauricedoepke](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mauricedoepke/32/1360_2.png) [@mauricedoepke](https://forum.babylonjs.com/u/mauricedoepke)\
**Post date:** [March 29, 2019, 6:35pm UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/37 "2019-03-29T18:35:27Z")

</div>

If this should be a problem of ammo/bullet couldn’t we ‘debounce’ that ourselves?  
Thus sending the first event to the listener and only raise another event once the object was not in a collision for at least one frame, which is the case if it is not in the manifolds anymore.

EDIT: This would also solve the problem of this case: [https://playground.babylonjs.com/#RG2AQ4#5](https://playground.babylonjs.com/#RG2AQ4#5)

For performance reasons we could only debounce the cases that have listeners attached, so it should not be a huge performance impact.

---

<div class="post-metadata">

**Author:** ![Asagi](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@Asagi](https://forum.babylonjs.com/u/Asagi)\
**Post date:** [March 29, 2019, 6:51pm UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/39 "2019-03-29T18:51:49Z")

</div>

Some video fragment.  
As you can see all very bad.  
[http://www.mediafire.com/file/muy0camuf210x8s/screen-capture\_%282%29.zip/file](http://www.mediafire.com/file/muy0camuf210x8s/screen-capture_%25282%2529.zip/file)

---

<div class="post-metadata">

**Author:** ![Asagi](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@Asagi](https://forum.babylonjs.com/u/Asagi)\
**Post date:** [March 29, 2019, 6:58pm UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/40 "2019-03-29T18:58:16Z")

</div>

![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/c/cc0b1d198e44b9a5608b10ead15d94153a88fcc8.png)

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/f/fcba7fe952ba84948e6152b2a8c5b71b5f20d7a3.png)  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/1/11789be3284b1ff93df37fc7dece6866dfffe48d.png)  
Some details…

---

<div class="post-metadata">

**Author:** ![mauricedoepke](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mauricedoepke/32/1360_2.png) [@mauricedoepke](https://forum.babylonjs.com/u/mauricedoepke)\
**Post date:** [March 29, 2019, 7:06pm UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/41 "2019-03-29T19:06:23Z")

</div>

That looks bad. Maybe you should indeed write that physics part yourself. I currently have no time to explain that in much detail. You would have to do your own research with the snippets I already provided you.

As soon as I am done with my own current project I can do an extensive tutorial on that topic.

---

<div class="post-metadata">

**Author:** ![Asagi](https://avatars.discourse-cdn.com/v4/letter/a/e36b37/32.png) [@Asagi](https://forum.babylonjs.com/u/Asagi)\
**Post date:** [March 29, 2019, 7:13pm UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/42 "2019-03-29T19:13:12Z")

</div>

Can i correct detect holes in meshes manualy? Without physic engine? Any keys?  
Or may be exist easy way to create normal collision box?

---

<div class="post-metadata">

**Author:** ![mauricedoepke](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mauricedoepke/32/1360_2.png) [@mauricedoepke](https://forum.babylonjs.com/u/mauricedoepke)\
**Post date:** [March 29, 2019, 7:15pm UTC](https://forum.babylonjs.com/t/physics-engine-question/2214/43 "2019-03-29T19:15:06Z")

</div>

Here I already wrote something regarding that:

> [@Physics Engine question](https://forum.babylonjs.com/t/physics-engine-question/2214/27):
>
> I’d try something like his for the metadata: platforms: [{id: "whatever", mesh: plaformMesh, holes: [{from: 0, from: Math.PI/4}]} {id: "whatever2", mesh: plaformMesh2, holes: [{from: 0, size: Math.PI/4},{from: Math.PI, size: Math.PI/4}]} ] With that metadata you coud even genereate the platforms by code. Use somehing like his for gravity: [https://gamedev.stackexchange.com/questions/15708/how-can-i-implement-gravity/41917](https://gamedev.stackexchange.com/questions/15708/how-can-i-implement-gravity/41917) And with the metadata + the roation of your platforms you can…

[Previous page](https://forum.babylonjs.com/t/physics-engine-question/2214.md?page=1)

[Next page](https://forum.babylonjs.com/t/physics-engine-question/2214.md?page=3)
