# Problems with Raycasting and Sphere overlap

**URL:** <https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606>\
**Category:** 🌵 Needle Engine\
**Tags:** question\
**Created:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606 "2023-03-16T00:00:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![discord-import](https://forum.needle.tools/user_avatar/forum.needle.tools/discord-import/32/33_2.png) [@discord-import](https://forum.needle.tools/u/discord-import)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/1 "2023-03-16T00:00:00Z")

</div>

I describe the problem on the attached video, in summary it seems the sphereOverlap function is always hitting things and when trying to do multiple Raycasts on the same frame / code block they get overritten.

I Have uploaded the project feature in that video here: [GitHub - orlax/Needle-Engine-Scene-Switching at physics-problems](https://github.com/orlax/Needle-Engine-Scene-Switching/tree/physics-problems)

is there something I am doing wrong? how could I help to solve this physics issue? my next steps are to go directly to Three.js level and do my physics checks there instead.

[https://cdn.discordapp.com/attachments/1085614900312821820/1085614901181030500/needlePhysicsBugs.mp4?ex=65d6322a&is=65c3bd2a&hm=723379ac450fc018c30b95f7b03c8abbc63a25bdab7c203018e1619ad581c817&(image larger than 2 MB)](https://cdn.discordapp.com/attachments/1085614900312821820/1085614901181030500/needlePhysicsBugs.mp4?ex=65d6322a&is=65c3bd2a&hm=723379ac450fc018c30b95f7b03c8abbc63a25bdab7c203018e1619ad581c817&)

[Original Post on Discord](https://discord.com/channels/717429793926283276/1085614900312821820)

_by user 326444095130042368_

---

<div class="post-metadata">

**Author:** ![discord-import](https://forum.needle.tools/user_avatar/forum.needle.tools/discord-import/32/33_2.png) [@discord-import](https://forum.needle.tools/u/discord-import)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/2 "2023-03-16T00:00:00Z")

</div>

@Marcel_Wiessler1 🌵 if you can take a look at this, I would super super appreciate it.

_by user 326444095130042368_

---

<div class="post-metadata">

**Author:** ![Marcel\_Wiessler1](https://forum.needle.tools/user_avatar/forum.needle.tools/marcel_wiessler1/32/21_2.png) [@Marcel\_Wiessler1](https://forum.needle.tools/u/Marcel_Wiessler1)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/3 "2023-03-16T00:00:00Z")

</div>

Hey Orlando, I’ll take a look.

For debugging what you could do is drawing some gizmos to see what is actually hit and raycast. There are some methods available in the Gizmos class.

Out of curiousity: do you have a specific reason for testing collisions manually via raycasting instead of using the physics engine callbacks that we expose? (e.g. onCollisionEnter or onTriggerEnter)

 ![image.png](https://europe1.discourse-cdn.com/flex006/uploads/needle1/original/2X/d/dff75b2ae6387f27148257841ac67b21420ac419.png)

---

<div class="post-metadata">

**Author:** ![Marcel\_Wiessler1](https://forum.needle.tools/user_avatar/forum.needle.tools/marcel_wiessler1/32/21_2.png) [@Marcel\_Wiessler1](https://forum.needle.tools/u/Marcel_Wiessler1)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/4 "2023-03-16T00:00:00Z")

</div>

![image.png](https://europe1.discourse-cdn.com/flex006/uploads/needle1/original/2X/b/be3033e9e933d13ca832ec70111d78c6018be828.png)

---

<div class="post-metadata">

**Author:** ![Marcel\_Wiessler1](https://forum.needle.tools/user_avatar/forum.needle.tools/marcel_wiessler1/32/21_2.png) [@Marcel\_Wiessler1](https://forum.needle.tools/u/Marcel_Wiessler1)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/5 "2023-03-16T00:00:00Z")

</div>

The collision argument contains info about the collision which you could use to reset your objects position

---

<div class="post-metadata">

**Author:** ![Marcel\_Wiessler1](https://forum.needle.tools/user_avatar/forum.needle.tools/marcel_wiessler1/32/21_2.png) [@Marcel\_Wiessler1](https://forum.needle.tools/u/Marcel_Wiessler1)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/6 "2023-03-16T00:00:00Z")

</div>

Oh and for debugging you could also enable collider rendering using the `?showcolliders` url query parameter

---

<div class="post-metadata">

**Author:** ![Marcel\_Wiessler1](https://forum.needle.tools/user_avatar/forum.needle.tools/marcel_wiessler1/32/21_2.png) [@Marcel\_Wiessler1](https://forum.needle.tools/u/Marcel_Wiessler1)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/7 "2023-03-16T00:00:00Z")

</div>

Let me know if that is already helpful

---

<div class="post-metadata">

**Author:** ![discord-import](https://forum.needle.tools/user_avatar/forum.needle.tools/discord-import/32/33_2.png) [@discord-import](https://forum.needle.tools/u/discord-import)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/8 "2023-03-16T00:00:00Z")

</div>

so the main reason is to not do things afterwards, so that we would move the player, detect a colission and then move him back. This is just the way I we are accostume to do things coming from game development.

I will check those gizmos classes and see what I find

_by user 326444095130042368_

---

<div class="post-metadata">

**Author:** ![discord-import](https://forum.needle.tools/user_avatar/forum.needle.tools/discord-import/32/33_2.png) [@discord-import](https://forum.needle.tools/u/discord-import)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/9 "2023-03-16T00:00:00Z")

</div>

well, I added some Gizmos to draw the raycasts and it seems to confirm what we are looking at here:

1 raycast works as expected  
sphereOverlap is always returning hits  
multipleRaycast behaves odly

_by user 326444095130042368_

---

<div class="post-metadata">

**Author:** ![discord-import](https://forum.needle.tools/user_avatar/forum.needle.tools/discord-import/32/33_2.png) [@discord-import](https://forum.needle.tools/u/discord-import)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/10 "2023-03-16T00:00:00Z")

</div>

this is a tiny sphereCast, in this case it is floating over the floor and the blue cube is set to ignore raycasts, even then it is always hitting other things and drawing in red. I have uploaded another commit adding this gizmos

 ![image.png](https://europe1.discourse-cdn.com/flex006/uploads/needle1/original/2X/0/06c99e17789e472b371af6fafd0b31e4f06ebf3e.jpeg)

_by user 326444095130042368_

---

<div class="post-metadata">

**Author:** ![Marcel\_Wiessler1](https://forum.needle.tools/user_avatar/forum.needle.tools/marcel_wiessler1/32/21_2.png) [@Marcel\_Wiessler1](https://forum.needle.tools/u/Marcel_Wiessler1)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/11 "2023-03-16T00:00:00Z")

</div>

Ah it’s quite possible that the IgnoreLaycast layer is not respected when doing the physics raycasts using rapier (SphereCasts are being handled by rapier). Thanks for the repo. I’ll check it tomorrow

---

<div class="post-metadata">

**Author:** ![discord-import](https://forum.needle.tools/user_avatar/forum.needle.tools/discord-import/32/33_2.png) [@discord-import](https://forum.needle.tools/u/discord-import)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/12 "2023-03-16T00:00:00Z")

</div>

oh, let me try just removing the cube altogether

_by user 326444095130042368_

---

<div class="post-metadata">

**Author:** ![discord-import](https://forum.needle.tools/user_avatar/forum.needle.tools/discord-import/32/33_2.png) [@discord-import](https://forum.needle.tools/u/discord-import)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/13 "2023-03-16T00:00:00Z")

</div>

FYI empty object, floating in the air, still returning collisions:

 ![image.png](https://europe1.discourse-cdn.com/flex006/uploads/needle1/original/2X/f/fb769129a7766cd62ae4c5a382dec85cb6c3477b.jpeg)

_by user 326444095130042368_

---

<div class="post-metadata">

**Author:** ![discord-import](https://forum.needle.tools/user_avatar/forum.needle.tools/discord-import/32/33_2.png) [@discord-import](https://forum.needle.tools/u/discord-import)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/14 "2023-03-16T00:00:00Z")

</div>

even on an empty scene, with nothing else it will return a collision?

 ![image.png](https://europe1.discourse-cdn.com/flex006/uploads/needle1/original/2X/c/c7c1b059347afc89958ce8f81149739c1ab69596.jpeg)

_by user 326444095130042368_

---

<div class="post-metadata">

**Author:** ![discord-import](https://forum.needle.tools/user_avatar/forum.needle.tools/discord-import/32/33_2.png) [@discord-import](https://forum.needle.tools/u/discord-import)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/15 "2023-03-16T00:00:00Z")

</div>

thank you for the attention! hopefully this images/info help I will keep an eye on this

_by user 326444095130042368_

---

<div class="post-metadata">

**Author:** ![Marcel\_Wiessler1](https://forum.needle.tools/user_avatar/forum.needle.tools/marcel_wiessler1/32/21_2.png) [@Marcel\_Wiessler1](https://forum.needle.tools/u/Marcel_Wiessler1)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/16 "2023-03-16T00:00:00Z")

</div>

what does it render with ?showcolliders ?

---

<div class="post-metadata">

**Author:** ![discord-import](https://forum.needle.tools/user_avatar/forum.needle.tools/discord-import/32/33_2.png) [@discord-import](https://forum.needle.tools/u/discord-import)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/17 "2023-03-16T00:00:00Z")

</div>

nothing different

 ![image.png](https://europe1.discourse-cdn.com/flex006/uploads/needle1/original/2X/c/c86f939404da51cc971ed58db3d48b04223512aa.png)

_by user 326444095130042368_

---

<div class="post-metadata">

**Author:** ![Marcel\_Wiessler1](https://forum.needle.tools/user_avatar/forum.needle.tools/marcel_wiessler1/32/21_2.png) [@Marcel\_Wiessler1](https://forum.needle.tools/u/Marcel_Wiessler1)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/18 "2023-03-16T00:00:00Z")

</div>

Hi @Orlando Almario i think we ran into two issues here

1: the sphere gizmo did draw at a wrong size so your spherecast was actually bigger and did intersect with the floor  
2: the gizmos were not set to the correct raycasting layer so we hit those as well 😶

---

<div class="post-metadata">

**Author:** ![Marcel\_Wiessler1](https://forum.needle.tools/user_avatar/forum.needle.tools/marcel_wiessler1/32/21_2.png) [@Marcel\_Wiessler1](https://forum.needle.tools/u/Marcel_Wiessler1)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/19 "2023-03-16T00:00:00Z")

</div>

That being said the sphereCast is not great. It uses the threejs api and it does not give an accurate intersection (maybe fine for your case)  
Im looking into exposing more of the rapier api, they have shape overlapping and are very likely faster too

---

<div class="post-metadata">

**Author:** ![discord-import](https://forum.needle.tools/user_avatar/forum.needle.tools/discord-import/32/33_2.png) [@discord-import](https://forum.needle.tools/u/discord-import)\
**Post date:** [March 16, 2023, 12:00am UTC](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606/20 "2023-03-16T00:00:00Z")

</div>

hey, I ran some very extensive testing yesterday and I agree sphere casting is not great haha I ended up trying to create my own sphere to mesh collision my results where:

- by chance I decided to only check agains the “default” layer so I think this save me from the gizmos in the wrong layer problem.
- then I started by copying your implementation a little to check first for collisions agains world aligned bounding boxes.
- if I found a colission I then go into trying to detect sphere vs polygons collisions (building the triangles right from the three.js geo buffer lol)
- I got it kinnnddd of working but it was to convoluted at the end.

Appreciate you took a look. our current solution is to just keep with one single raycast and just add a bunch of transparent world aligned colliders

_by user 326444095130042368_

[Next page](https://forum.needle.tools/t/problems-with-raycasting-and-sphere-overlap/606.md?page=2)
