Raycast not working properly. Possible bug?

Topics: Developer Forum
Jun 19, 2011 at 3:49 AM

Just thought I would raise this issue again as development seems to have started up again. I'm seeing rays penetrate through objects when set to end at the nearest body.

Here is a video illustrating the issue: http://www.youtube.com/watch?v=1ykVpOVVQhM

It seems to be related to the angle the ray is cast at. Notice how the ray penetrates the loop shape.

Jan 20, 2012 at 10:26 AM

I've run into the exact same problem with the latest build of farseer on Windows Phone. It seems pretty easy to reproduce when casting against axis aligned boxes. It also seems oddly random, that's to say casting a ray through two boxes on every frame from the exact same position will often result in the ray very randomly either returning the first hit box or skipping though that box and hitting the second. That's been my experience at least.

A work around seems to be allowing the raycast to return all objects it hits by always returning -1 in the ratcast callback. Then arrange the results so that only the nearest hit is saved. It seems the raycasting will always return the first hit object but it might not always be the first one to be sent back through the callback.

Hope this helps people dealing with the same problem.

Jan 21, 2012 at 11:18 PM

I've had this issue and couldn't get around resolving it unless I collected all the results but even then sometimes it would act strangely so I moved on without trying to figure it out. Now I went back to my tests returning -1 and this seemed to have solved the issue where previously nothing would help so thank you. There is definitely something fishy in the raycast logic but i had hard time reproducing it in code. It's a hunch but since I am casting against edge fixturesperhaps it's a problem with the inside/outside. As far as I've read if the origin of the ray is inside a shape sometimes it would not return a valid edge and you have to cast it in the opposite direction.

Jan 23, 2012 at 9:16 AM
Edited Jan 23, 2012 at 9:16 AM

I had the same problem, check this out this post:

http://farseerphysics.codeplex.com/discussions/265543

I hope it works for you as well.

Jan 25, 2012 at 11:40 AM
Edited Jan 25, 2012 at 12:55 PM
jerrysb wrote:

I've had this issue and couldn't get around resolving it unless I collected all the results but even then sometimes it would act strangely so I moved on without trying to figure it out. Now I went back to my tests returning -1 and this seemed to have solved the issue where previously nothing would help so thank you. There is definitely something fishy in the raycast logic but i had hard time reproducing it in code. It's a hunch but since I am casting against edge fixturesperhaps it's a problem with the inside/outside. As far as I've read if the origin of the ray is inside a shape sometimes it would not return a valid edge and you have to cast it in the opposite direction.


I had the same problem in my game where the world is made up of polygon shapes. From what i can tell the problem happens in QuadTree.RayCast() which doesn't always check objects being collided with in the correct order.

Jan 25, 2012 at 11:40 AM
Pnikosis wrote:

I had the same problem, check this out this post:

http://farseerphysics.codeplex.com/discussions/265543

I hope it works for you as well.


I did try this early on but it seems to have absolutely no effect =/.