Questions about RayCasting in 3.1 with XNA

Topics: User Forum
Feb 12, 2011 at 2:44 AM

Background on what I'm using Raycasting for:

I've implemented dynamic shadowing in the XNA Platformer tutorial (XNA 3.1 tutorial that I moved to 4.0, with Farseer 3.1), and am trying to use raycasting to optimize the number of triangles needed to be drawn by the graphics card, since my fairly beefy machine got bottle-necked quite quickly trying to do large radius lights on the GPU, with lots of room to spare on the CPU.


My questions are:

1. What exactly is the "float fraction" for? I seemed to have picked up that it relates to how far it tries to test the ray in relation to the original vectors passed in, is this correct?

2. Is there a particular order in which objects get hit by the ray?

Assuming A is the start Vector2, and B is the end Vector2. When A is below and to the left of B, objects C and D (with C being closest to A) in the path of the ray, D is hit first. But when I account for this and use only the LAST object hit by the ray, when A is above B, the rays will "flicker" (I'm drawing them every frame, from A to Vector2 "point" returned from the last object hit) between objects C and D.

3. If this is a problem with 3.1, does 3.2 resolve this, and is it considerably easier to migrate a simple project from 3.1 to 3.2 than it was 2.x to 3.0?

Feb 12, 2011 at 11:02 AM


The fraction works like this:

return -1: ignore this fixture and continue
return 0: terminate the ray cast
return fraction: clip the ray to this point
return 1: don't clip the ray and continue

It basically controls the behavior of the raycast.


I can only think of situations involving floating errors that would make the ray return inconsistent results. Try looking at the Testbed samples, they show you how to use Raycasts correctly.

3. I would recommend you upgrade to the latest version. Upgrading between 3.x versions should not be too much of a problem. I always try to keep the major updates incremental, so it should be relatively easy.

Feb 12, 2011 at 11:58 AM

For question 1. I had read that in the source for Farseer already, but which end does it "clip", from the start vector to the new point, or the new point to the end vector.

2. Working on looking at t he test bed right now, well post again once I have gone over it and the results I get.

3. I have upgraded to 3.2 now (same results currently for raycasting), wasn't too painful, just some collision stuff that had to be moved around, good job making it easy to move :)

Feb 12, 2011 at 12:39 PM
Edited Feb 12, 2011 at 12:40 PM

Solved my problem, I was originally doing half of the returns right (was returning 0 on first hit, and -1 on an object i didnt want to hit (like the light source itself, or a non-shadowing object)), but then i changed it to return fraction and 1 respectivly on hit/skip. (As it was when I posted this).

To skip an object I had to return -1 and then t hings magically worked, I don't know why this would make a difference since the documentation for RayCastCallback says "return -1 to ignore this fixture and continue" and "return 1: don't clip the ray and continue". Maybe specify a little bit more on what returning 1 actually does? (What does it do anyways?)

Feb 13, 2012 at 3:20 PM

I'd like to get some clarifications too on this:

return -1: ignore this fixture and continue
return 0: terminate the ray cast
return fraction: clip the ray to this point
return 1: don't clip the ray and continue


-1: I understand it will skip the fixture and keep going.
0: I understand that it will not trigger any more callbacks and the raycast will return immediately; is that right?
frac: how is that different from 0?
1: Is it like -1 but it ignores THAT edge, but will collide with other edges in the fixture?