3
\$\begingroup\$

I have a mesh post-processor in Unity that automatically a convex mesh collider to objects that need one (mostly all).

Sometimes however there are objects that only need a box collider instead.

How can I detect if a mesh is a rectangular prism?

\$\endgroup\$
1
  • \$\begingroup\$ I just realised one simple way to do this is to check if all angles between edges are 90 degrees :O \$\endgroup\$ Jun 30, 2016 at 1:26

1 Answer 1

1
\$\begingroup\$

I realised that all that's required to check if a mesh is a rectangular prism or not is to check if every triangle of a given mesh contains exactly one 90° angle between the triangles' edges.

For calculating the angle, the top answer on the question was very helpful: https://stackoverflow.com/questions/3486172/angle-between-3-points

\$\endgroup\$
2
  • \$\begingroup\$ This may suffice for the collection of objects you're using, but it's not a sufficient criterion in general. I can create many meshes out of right-angle triangles which are not rectangular prisms. Any union of voxels will pass this criterion if triangulared appropriately (staircases, plus-signs, etc). This test will also fail to recognize many objects which are not simple rectangular prisms, but are sufficiently rectangular-prism-like for purposes of collision - like a rectangular prism with a subtle bevel on its edges. This doesn't affect the bulk shape but trips up angle-based tests. \$\endgroup\$
    – DMGregory
    Jun 30, 2016 at 17:10
  • \$\begingroup\$ I was surprised how it worked so well for what I tested and was so straight forward. I currently have a vertex limit of 24 (in Unity a rectangular prism is 4 vertices multiplied by 6 faces) so it should literally only accept boxes, but as you mentioned it's missing out on rectangular prisms with bevels (and others). Do you have any idea what I could do instead of the angle based approach? \$\endgroup\$ Jun 30, 2016 at 23:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .