For some simple 3D shapes (cubes, prisms) how can I determine which side a ray intersects, knowing the direction of the ray, and the normals of the shapes' sides.

I thought of using plane intersection for each side, but I'm sure there's a more efficient way.

Also, could this be done for a cylinder?

  • \$\begingroup\$ Most intersection routines will provide this information, directly or indirectly. Which technique/library are you using? \$\endgroup\$ – user41442 Feb 5 '15 at 0:17

Most of your efficiency benefit can come from checking against a loose bound for your whole shape (box or sphere known to contain it). When you find that the ray "might" intersect it (because it intersects a sphere or box that's not much larger), then you pretty much need to check each face.

You can skip faces whose normals point away from the ray, since a different face of your closed solid would have to catch it first, if at all.

In some very specific cases, you can rule out whole faces. For example, consider unit cube centered on (0,0,0). If you're casting any ray from (0,0,100), the only face it could hit, if any, is the Z-positive one.

You mentioned "simple 3d shapes". For a complex shape with many faces, you might do broad rejection (sphere or box) for smaller pieces or individual faces of the shape.

| improve this answer | |
  • 1
    \$\begingroup\$ more specifically: if the ray and normal are perpendicular then the ray either lies on the extended plane of the face (easily tested from any point on the ray) or do not intersect at all. \$\endgroup\$ – Pieter Geerkens Feb 5 '15 at 5:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.