Loading...
Logo
Processing Forum
Hi all,

I understand how to constrain a balls movements within a box by testing its x,y according to some stored bounds. And I understand how to fill a circular area with graphics using trigonometry.

But is it possible, or how do you approach constraining within a "custom" shape? for example a pentagon or a letterform? Seems like it might involve some serious maths. How difficult is it to extend such a technique to a 3D shape?

Thanks in advance.

Replies(6)

indeed, this is not easy.
But you can use geomerative library for this. http://www.ricardmarxer.com/geomerative/
for example use contains(RShape) or intersects(RShape)
Thanks, that looks like a fantastic library.
there are two general approaches to this problem:  'graphical' and 'analytical'.  the first is easy, but less precise; the second is harder, but more precise (and might offer additional info like 'how far' inside/outside or direction perpendicular to nearest edge, that the graphical approach won't)

graphically, just draw your constraining shape on an offscreen buffer, then just test pixel color for collision.

analytically, it's often easier to triangulate the shape, then you'll just have a single triangle test that you can repeat for the entire collection that make up the overall shape.

in 3d, for the graphical approach, you'd have to use 'voxels' then point-sample as before in 2d.  analytically, you'd have to model the 'volume' with oriented polygonal faces (if your point is on 'inside' all face normals of a convex shape, then you're inside the volume)

hth

davbol, thanks for the explanation. I will try the the graphical method out, but the analytical one I can't quite get my head around! When you say triangulate the shape, do you mean I can use trigonometry to work out a triangle 'area' and build my shape from those triangles?

thanks
in 2d, using your pentagon as example, there are two ways to 'think' about that shape.  since it's convex you can treat it as 5 oriented line segments and (as in the 3d case described above) ask 'is your point 'inside' the normals of all five lines?' (if so, it's inside the pentagon).  it's an easy test, but only works if the shape is convex.

for concave shapes, there are other algorithms, such as counting the number of times a line constructed through your point crosses polygon edges.  slightly more work, but doesn't have the convex-only restriction. (search for graphics gems code, and i strongly suspect the geomerative lib uses a method along these lines)

or, you can split the pentagon into three triangles and think about it that way, that's what i meant by triangulation. then instead you ask 'is it inside any of the three triangles?' (if so, it's inside the pentagon)  depending on exactly what you're doing, this may or may not be better than an edge-crossing test.  again, the advantage of the triangle test is it'll work with any shape, concave or convex, and (if properly handled) even if the shape is 'degenerate' (has edges that self-intersect), and thinking in terms of polygonal 'faces' will be more advantageous if you intend to extend it to 3d.

thanks for the extra explanation. I'm having fun with the graphical method at the moment and once i've got comfortable with that then i'll try the more advanced techniques.

cheers