# Shadow Casting and Physics V2

**URL:** <https://forum.babylonjs.com/t/shadow-casting-and-physics-v2/46125>\
**Category:** Questions\
**Tags:** lighting, tutorial, physics, shadows\
**Created:** [December 2, 2023, 6:32pm UTC](https://forum.babylonjs.com/t/shadow-casting-and-physics-v2/46125 "2023-12-02T18:32:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidP](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/davidp/32/45669_2.png) [@DavidP](https://forum.babylonjs.com/u/DavidP)\
**Post date:** [December 2, 2023, 6:32pm UTC](https://forum.babylonjs.com/t/shadow-casting-and-physics-v2/46125/1 "2023-12-02T18:32:19Z")

</div>

Hello,  
I started with Babylon.js’s Physics Helpers example

> **[Babylon.js Playground](https://playground.babylonjs.com/#E5URLZ%232)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

and added a few lines to casts shadow:  
changed lights - line 29  
added shadow generator - line 32  
made the ground to receive the shadow - line 36  
and made boxes to cast shadow - line 152

> **[Babylon.js Playground](https://playground.babylonjs.com/#E5URLZ%236)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

When I click “radial explosion” and run for 10 seconds, shadows become corrupted.  
Any suggestions why that happens and how that can be fixed?  
thank you

---

<div class="post-metadata">

**Author:** ![Asdrubal](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/asdrubal/32/36271_2.png) [@Asdrubal](https://forum.babylonjs.com/u/Asdrubal)\
**Post date:** [December 2, 2023, 11:40pm UTC](https://forum.babylonjs.com/t/shadow-casting-and-physics-v2/46125/2 "2023-12-02T23:40:49Z")

</div>

Pretty interesting bug 😃

 ![Screenshot from 2023-12-02 23-39-45](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/e/2/e2c5ca82918c6db91036fa0413ea34ec897425a6.png)

---

<div class="post-metadata">

**Author:** ![DavidP](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/davidp/32/45669_2.png) [@DavidP](https://forum.babylonjs.com/u/DavidP)\
**Post date:** [December 3, 2023, 1:08am UTC](https://forum.babylonjs.com/t/shadow-casting-and-physics-v2/46125/3 "2023-12-03T01:08:18Z")

</div>

After I reduced the box count, I am getting a more stable behavior but if you wait long enough, you will see the problem ☹

> **[Babylon.js Playground](https://playground.babylonjs.com/#E5URLZ%237)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![Asdrubal](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/asdrubal/32/36271_2.png) [@Asdrubal](https://forum.babylonjs.com/u/Asdrubal)\
**Post date:** [December 3, 2023, 1:37am UTC](https://forum.babylonjs.com/t/shadow-casting-and-physics-v2/46125/4 "2023-12-03T01:37:48Z")

</div>

I think there’s some limit of objects to cast shadow, but we can change, I don’t remember where is…

---

<div class="post-metadata">

**Author:** ![DavidP](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/davidp/32/45669_2.png) [@DavidP](https://forum.babylonjs.com/u/DavidP)\
**Post date:** [December 3, 2023, 1:59am UTC](https://forum.babylonjs.com/t/shadow-casting-and-physics-v2/46125/5 "2023-12-03T01:59:52Z")

</div>

I am guessing that I am not hitting the limit of object to cast because even with the original object count initially I am getting the correct shadows. The problem starts when I use one of the helpers. Therefore, we might be dealing with memory corruption…

---

<div class="post-metadata">

**Author:** ![DavidP](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/davidp/32/45669_2.png) [@DavidP](https://forum.babylonjs.com/u/DavidP)\
**Post date:** [December 3, 2023, 2:18am UTC](https://forum.babylonjs.com/t/shadow-casting-and-physics-v2/46125/6 "2023-12-03T02:18:37Z")

</div>

I see that we have the same bug in Ammo solver as well

> **[Babylon.js Playground](https://playground.babylonjs.com/#PX6E6C%2324)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Also here is a playground that recreates the problem with only 10 spheres

> **[Babylon.js Playground](https://playground.babylonjs.com/#Z8HTUN%23797)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

On the other hand, if there is no simulator, I can easily have 50 spheres and I am getting correct shadows

> **[Babylon.js Playground](https://playground.babylonjs.com/#LKA8VM%2366)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [December 3, 2023, 11:35am UTC](https://forum.babylonjs.com/t/shadow-casting-and-physics-v2/46125/7 "2023-12-03T11:35:52Z")

</div>

That’s because, by default, the light frustum is calculated automatically according to the shadow caster positions. So, it also takes into account the boxes when they have fallen far apart from the ground, which extents the light frustum and reduce the shadow quality.

You should use a static light frustum, by setting `light.autoUpdateExtends = false;` and set values to orthoLeft/orthoRight/orthoTop/orthoBottom/shadowMinZ/shadowMaxZ:

> **[Babylon.js Playground](https://playground.babylonjs.com/#E5URLZ%2314)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![DavidP](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/davidp/32/45669_2.png) [@DavidP](https://forum.babylonjs.com/u/DavidP)\
**Post date:** [December 3, 2023, 6:21pm UTC](https://forum.babylonjs.com/t/shadow-casting-and-physics-v2/46125/8 "2023-12-03T18:21:37Z")

</div>

@Evgeni_Popov HUGE Thank you for the solution and the explanation! This light frustrum setup does resolve my problem
