# Question about intersectMesh

**URL:** <https://forum.babylonjs.com/t/question-about-intersectmesh/21303>\
**Category:** Questions\
**Created:** [May 31, 2021, 9:36pm UTC](https://forum.babylonjs.com/t/question-about-intersectmesh/21303 "2021-05-31T21:36:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![schoening](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/schoening/32/4950_2.png) [@schoening](https://forum.babylonjs.com/u/schoening)\
**Post date:** [May 31, 2021, 9:36pm UTC](https://forum.babylonjs.com/t/question-about-intersectmesh/21303/1 "2021-05-31T21:36:57Z")

</div>

Hi, I am testing out BabylonJS and when there is a collision I want to move back to the previous non-intersecting position. I made a little demo where I keep track of the previous position. But it isn’t working which has me kind of stumped. Coming from a functional background I would have expected this to work. I made a little demo where my sphere should reverse direction once it collides. Instead, it gets stuck inside the box:

[https://playground.babylonjs.com/#QZY3Q6#6](https://playground.babylonjs.com/#QZY3Q6#6)

Any idea why this is happening?

---

<div class="post-metadata">

**Author:** ![Raggar](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raggar/32/20672_2.png) [@Raggar](https://forum.babylonjs.com/u/Raggar)\
**Post date:** [May 31, 2021, 10:02pm UTC](https://forum.babylonjs.com/t/question-about-intersectmesh/21303/2 "2021-05-31T22:02:50Z")

</div>

Make sure to recompute the matrix whenever you use a function depending on updated scale/rotation/position values.  
[https://playground.babylonjs.com/#QZY3Q6#7](https://playground.babylonjs.com/#QZY3Q6#7)

---

<div class="post-metadata">

**Author:** ![schoening](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/schoening/32/4950_2.png) [@schoening](https://forum.babylonjs.com/u/schoening)\
**Post date:** [May 31, 2021, 10:32pm UTC](https://forum.babylonjs.com/t/question-about-intersectmesh/21303/3 "2021-05-31T22:32:30Z")

</div>

Thank you! 😃  
Is this a very expensive operation to do at a larger scale? I googled it and going past a thousand objects seems to slow that method down considerably.

Would it be better to do game logic / collision / movement outside BabylonJS and then update the meshes from my array of game objects?

(I was planning to do it that way initially but I don’t want to deal with writing my own move/rotate/collision detection when that is build into the objects already)

---

<div class="post-metadata">

**Author:** ![Raggar](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raggar/32/20672_2.png) [@Raggar](https://forum.babylonjs.com/u/Raggar)\
**Post date:** [May 31, 2021, 10:46pm UTC](https://forum.babylonjs.com/t/question-about-intersectmesh/21303/4 "2021-05-31T22:46:27Z")

</div>

If the mesh is rotating, scaling or moving, it happens every frame, as this updated matrix is what is being sent to the GPU in order to render the mesh. If I’m not mistaken, a flag is set, preventing the engine to recalculate the matrix when you manually do it, so there should be no performance penalty.

Are you talking about worker threads? Almost anything long-running or compute intensive you can put in a thread is a plus. You’ll have to try it out yourself, though, and see if you are happy with the result, as the messaging to- and from threads is not completely instantaneous, and might result in a bit of jigger.

---

<div class="post-metadata">

**Author:** ![schoening](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/schoening/32/4950_2.png) [@schoening](https://forum.babylonjs.com/u/schoening)\
**Post date:** [June 1, 2021, 7:08am UTC](https://forum.babylonjs.com/t/question-about-intersectmesh/21303/5 "2021-06-01T07:08:06Z")

</div>

Thanks again for replying 🙂

No what I meant was if I should treat babylonjs as the “view” and run my game logic on a separate set of game structures and then map their state onto my babylonjs view

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [June 1, 2021, 7:59am UTC](https://forum.babylonjs.com/t/question-about-intersectmesh/21303/6 "2021-06-01T07:59:51Z")

</div>

You may be interested in these SPS particles colliding [https://playground.babylonjs.com/#HIM0WS#1](https://playground.babylonjs.com/#HIM0WS#1)

See

> **[Solid Particle System | Babylon.js Documentation](https://doc.babylonjs.com/divingDeeper/particles/solid_particle_system)**
>
> Learn all about the solid particle system in Babylon.js.

> **[Introduction to Developing a System For Collisions | Babylon.js Documentation](https://doc.babylonjs.com/guidedLearning/workshop/Collide_Intro)**
>
> A collision system built using justBabylon.js code with no physics engines

> **[Simple Particle Collisions Using a Grid | Babylon.js Documentation](https://doc.babylonjs.com/guidedLearning/workshop/Grid_Moves)**
>
> Particles restricted to moving between grid coordinates

> **[Slow Moving Particle Collisions | Babylon.js Documentation](https://doc.babylonjs.com/guidedLearning/workshop/Slow_Collide)**
>
> Keeping the speed of particles below twice their radius per frame to simplify collisions

> **[Free Particle Collisions | Babylon.js Documentation](https://doc.babylonjs.com/guidedLearning/workshop/Free_Collide)**
>
> Particles that move with no restriction to their start position or velocity
