Ray casting and collision


I need a way to be able to cast a ray through the physics world and have it return to me the type of object I hit, and the point of intersection.
This needs to work very efficiently (I should be able to do it multiple times per frame with almost no impact).
The ray can technically be a line (have two endpoints instead of one like a traditional ray), if it helps performance.
This should probably interact directly with the physics system. I'd prefer sending a message to the physics system about the ray, and the physics system fills my message with the information about the type and intersection point.
Closed Jan 24, 2012 at 3:32 PM by LordIkon
Implemented in v0.261.


LordIkon wrote Feb 8, 2008 at 12:04 PM

If we're not going to have immediate messages than at the very least I need a function that I can pass in two vectors: My to and from points, and the function will return the intersection point and the type of physics object hit (in some kind of enum or int). This will need to be abstracted through the physics system but ultimately lies with the fact that I doubt JigLib supports this, while Ageia almost certainly does.

Sturm wrote Feb 8, 2008 at 1:33 PM

Do you only want the first entity hit or all entities between A and B?

shawmishrak wrote Feb 8, 2008 at 4:51 PM

What kind of results do you want, precisely? How would you like to identify the target of the query?

LordIkon wrote Feb 10, 2008 at 4:30 PM

I'd like two different rays, one that sends me a list of Vector3s and IDs, for the points of intersections, and the type of physics object hit. We could consider a third parameter to tell us the specific entity ID of what we've hit.

The second ray would just return the first thing hit. Again, just a Vector3 point of intersection, and an int ID.

LordIkon wrote Feb 10, 2008 at 4:31 PM

We can create collision filters if we need, so when we cast the ray, we only check specific physics types for collision. For example, if I only wanted to check for terrain, I could use a filter that stated such.

Sturm wrote Feb 10, 2008 at 10:54 PM

I guess it would be benefitial to create different query methods:
  • One for getting the first entity intersecting
  • One for getting the list of intersecting entities, including terrain
  • One for testing intersection with the terrain

LordIkon wrote Feb 11, 2008 at 3:52 PM

Well I'm not sure I see the physics world as distinguishing between types of things, other than by ID. So as long as the ray returned the type of object it hit (e.g. Terrain, Player, Bot, any custom type), then we wouldn't have to set up types specifically for terrain. However, if we wanted to hit only terrain we could specify that in the filter when we cast the ray.

Sturm wrote Feb 11, 2008 at 7:20 PM

Well I didn't assume that the physics layer would be the one quired about this. I could easily assume that you could query a Collision manager, and then that would query the physics engine. Matching the returned ids it would return the list of entities.

LordIkon wrote Feb 12, 2008 at 3:54 PM

The terrain would likely not be queried. Ageia makes heightfield info of its own based on the terrain, and I'm sure it will use that for the query. I implemented a function once to do ray collision on my original terrain component. Even with quad-tree optimization is took about 300-400ms on a 1024x1024. I need to queries to take less than 1ms probably. I know this is easily possible from a physics engine.

noone88 wrote Feb 28, 2008 at 1:05 AM

JigLibX supports this. CollisionSystem.SegmentIntersect() ... You have to pass an segment (finite line) and it passes you back the objects you want.

shawmishrak wrote Feb 28, 2008 at 2:56 PM

300-400ms? Wow. Were you locking the vertex buffer, extracting the vertex data, then doing ray-triangle intersections?

The integer ID part still needs to be defined. Is that assocated with the scene graph entity and copied to the physics actor? Or does the physics actor store a reference to its owning scene entity and return that?

Casting the ray is the easy part. The hard part is returning meaningful, efficient results.

LordIkon wrote Mar 13, 2008 at 4:12 AM

I was casting a ray, checking each bounding box for the terrain. And based on the bounding box I could cull out the others just like view culling. After I found the leaf node the ray hit then I had to check each triangle in that section of terrain for collision.

Very similar to the picking sample from XNA.

wrote Feb 4, 2009 at 3:37 PM

wrote Sep 25, 2009 at 9:53 AM

wrote Dec 1, 2011 at 6:39 PM

wrote Jan 9, 2012 at 3:22 PM

wrote Jan 9, 2012 at 3:52 PM

wrote Jan 24, 2012 at 3:31 PM

wrote Jan 24, 2012 at 3:32 PM

wrote Feb 14, 2013 at 9:36 PM

wrote May 16, 2013 at 11:01 AM