# How to detect the collision between camera and mesh without making mesh impenetrable?

**URL:** <https://forum.babylonjs.com/t/how-to-detect-the-collision-between-camera-and-mesh-without-making-mesh-impenetrable/33852>\
**Category:** Questions\
**Created:** [September 12, 2022, 3:24am UTC](https://forum.babylonjs.com/t/how-to-detect-the-collision-between-camera-and-mesh-without-making-mesh-impenetrable/33852 "2022-09-12T03:24:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![splash27](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@splash27](https://forum.babylonjs.com/u/splash27)\
**Post date:** [September 12, 2022, 3:24am UTC](https://forum.babylonjs.com/t/how-to-detect-the-collision-between-camera-and-mesh-without-making-mesh-impenetrable/33852/1 "2022-09-12T03:24:09Z")

</div>

If we want a mesh to be available inside of `camera.onCollide` handler, we can mark the mesh like that:

```auto
box.checkCollisions = true;

```

But this property also makes this mesh impenetrable (camera can’t fly through that mesh). Sometimes it is exactly what we want, but sometimes we want to detect collisions with camera while still allowing a camera to go through.

Is there an easy way to do this in BabylonJS?

A playground for reference: [https://playground.babylonjs.com/#DAC1T5](https://playground.babylonjs.com/#DAC1T5)

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [September 12, 2022, 12:57pm UTC](https://forum.babylonjs.com/t/how-to-detect-the-collision-between-camera-and-mesh-without-making-mesh-impenetrable/33852/2 "2022-09-12T12:57:23Z")

</div>

You could try to check if the camera’s position is inside the mesh [Mesh Intersections | Babylon.js Documentation (babylonjs.com)](https://doc.babylonjs.com/divingDeeper/mesh/interactions/mesh_intersect) This could be done either on the render loop or just when the camera position changes using observables [Observables | Babylon.js Documentation (babylonjs.com)](https://doc.babylonjs.com/divingDeeper/events/observables) 🙂

---

<div class="post-metadata">

**Author:** ![splash27](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@splash27](https://forum.babylonjs.com/u/splash27)\
**Post date:** [September 12, 2022, 10:23pm UTC](https://forum.babylonjs.com/t/how-to-detect-the-collision-between-camera-and-mesh-without-making-mesh-impenetrable/33852/3 "2022-09-12T22:23:07Z")

</div>

I kind of made it: [https://playground.babylonjs.com/#DAC1T5#3](https://playground.babylonjs.com/#DAC1T5#3)

But unfortunately, camera doesn’t have `onPositionChangeObservable`. And it seems like it’s not easy to make a custom one. I was thinking about using something like that

```auto
set position(value) {
    super.position = value;
    //notify observers here.
}

```

But unfortunately, camera doesn’t re-assign position itself while moving, it’s just altering x, y and z fields of Vector3 inside of position. So, I didn’t find easy way to detect camera position change without re-writing the whole getter and setter (and maybe more stuff) from FreeCamera class.  
Right now, I am listening `onViewMatrixChangedObservable`, which is triggered a bit too often, because it fires on every view change (even when just looking around, without actual position change).  
Listening `onBeforeRenderObservable/onBeforeStepObservable` from `scene` will give even more unnecessary calls.

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [September 13, 2022, 12:44pm UTC](https://forum.babylonjs.com/t/how-to-detect-the-collision-between-camera-and-mesh-without-making-mesh-impenetrable/33852/4 "2022-09-13T12:44:17Z")

</div>

Hmmm maybe @PolygonalSun knows of a nice way to listen for changes in the camera’s position?

---

<div class="post-metadata">

**Author:** ![splash27](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@splash27](https://forum.babylonjs.com/u/splash27)\
**Post date:** [September 13, 2022, 6:20pm UTC](https://forum.babylonjs.com/t/how-to-detect-the-collision-between-camera-and-mesh-without-making-mesh-impenetrable/33852/5 "2022-09-13T18:20:55Z")

</div>

Ideally, I would like to have some sort of `camera.onIntersect` function in Babylon.js that would work like `camera.onCollide`, but with penetrable meshes. I wonder if it’s possible to add this in Babylon.js officially.

---

<div class="post-metadata">

**Author:** ![PolygonalSun](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/polygonalsun/32/2704_2.png) [@PolygonalSun](https://forum.babylonjs.com/u/PolygonalSun)\
**Post date:** [September 15, 2022, 12:44am UTC](https://forum.babylonjs.com/t/how-to-detect-the-collision-between-camera-and-mesh-without-making-mesh-impenetrable/33852/6 "2022-09-15T00:44:25Z")

</div>

TBH, I can’t think of any elegant ways to listen for camera position changes, but you could always check if there are any non-zero values in the `camera.cameraDirection` vector. The way that our camera works is to add changes there and decrement the values as inertial movements happen.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 15, 2022, 2:53pm UTC](https://forum.babylonjs.com/t/how-to-detect-the-collision-between-camera-and-mesh-without-making-mesh-impenetrable/33852/7 "2022-09-15T14:53:37Z")

</div>

onViewMatrixChangedObservable can help detect any changes on the camera position or orientation
