# Distance measurement using collision system?

I need to implement a minimum-distance calculation and I’m wondering if somewhere in the collision handling system this is already done for me somehow. If not, I think I can fake it by creating a series of ever-expanding collision spheres around my avatar, until I detect a collision. But I’m hoping there’s a more efficient way already built-in (since that seems to often be the case with Panda3D!).

The collision system will give you two NodePaths, whose distance you can calculate by:

``````lenVector = nodePath1.getPos() - nodePath2.getPos()
lenVector.length()``````

You don’t really need to use the collision system if you already know the node path’s that you wish to check distances with but if you do you just need to get the nodepaths from the collision entry

Uh, if you’re looking for the exact distance you can use a CollisionRay, and use getSurfacePoint and the ray origin to calculate the distance.

You could also create a camera where the ray originates that renders a very small depth buffer, and read the value from that depth texture.

Sorry, I might not have been clear in my original post. I need the minimum distance between the surfaces of objects (i.e. triangle-to-triangle). The distance between the NodePath origins does me no good.

Because I want the minimum distance, a CollisionRay does not work because that only goes in one specific direction. I need the minimum distance to any of the triangles in the world. I guess I could use a whole set of CollisionRays oriented in all different directions and take the minimum over those, but I’m trying to avoid a solution that requires a lot of iteration. Also, I’d be risking missing parts of the world that slip between two CollisionRays, especially as distance from my avatar increases.

I should probably mention that this is in the context of a research project, not a game. My focus is not on the distance measurement itself, so I’m hoping to find a fast, black box type approach. If you take a look at the webpages for PQP (cs.unc.edu/~geom/SSV/), you’ll get an idea of what I’d like to be able to do. Of course I could just use PQP but I’d like to avoid integrating another library if I can avoid it.

Thanks for your suggestions though. These forums are really great and one of the reasons that Panda3D is my engine of choice for this project.

Ah, sorry for misunderstanding your question then.

If you fake it with expanding collision spheres then collisionEntry.getSurfacePoint(fromNodePath) sounds like your best bet. Pro-rsoft’s depthmap idea also sounds feasible except I don’t really know if there’s a way to find the minimum value from a texture using a shader.

Well, you can copy the depth image from the GPU back to RAM, and then analyze it to get the minimum depth, then, if you know the camera near and far, you can calculate the distance then. This is expensive, but I don’t know if a collision system alternative would be faster than this.