Discuss and collaborate on GML scripts

You are not logged in.

#1 2013-01-30 19:19:41

Registered: 2009-05-21
Posts: 74

Instance inside random shape

Anyone have a clue?

I think I've seen a solution before but I can't find it anymore.

Thanks for the help!


#2 2013-01-30 20:34:57

Registered: 2007-08-18
Posts: 1,202

Re: Instance inside random shape

I'd do a point-in-polygon test for the instances, once the shape is drawn.

Determining when the shape is complete is a bit trickier. I'm presuming the shape is defined by a series of line segments. The simplest way to detect the closure of a shape would be to test each new segment (as the shape is drawn) for intersection with all of the previous segments (not including the adjacent segment). This operation could be somewhat slow if there are a lot of segments.

If there is an intersection, create the polygon from the intersection point, through the following segments, and up to the last segment. The end point of the last segment and start point of the intersecting segment should be skipped. The point_in_polygon script will treat the polygon as closed.

You will also have to ensure that the polygon is given to the point_in_polygon script with the points in the right order (either clockwise or counter-clockwise, not sure which). You can test the handedness or winding of a polygon by measuring its area and testing if the result is positive or negative. If the winding is incorrect, reverse the order of the points before passing the polygon to the point_in_polygon script.


An alternative to doing point-in-polygon tests would be to use precise sprite-based collisions. With this any instance touching the polygon would count as being inside. Once the shape has been defined, you could draw the polygon to a surface and create a sprite from it. Assign the new sprite to a utility object you have created for the purpose of testing polygon collisions. Check for collisions with the relevant instances and the utility object. Destroy the sprite when you are done with it.

This method could be further extended to test if an instance is entirely enclosed within the bounds of the polygon. You would need to create a second sprite that is the inverse of the polygon, basically a solid rectangle with the polygon hollowed out of the center. For every instance that collides with the first sprite, test if it collides with the second. If it does not, the instance is completely enclosed.

Sprite creation functions are rather slow which might make these alternatives impractical.

Last edited by xot (2013-01-30 21:13:10)

Abusing forum power since 1986.


Board footer

Powered by FluxBB