Archived

This topic is now archived and is closed to further replies.

julienX

Simple 2D collision help

Recommended Posts

In my game my menu selection boxes are all trapeziums (quadrilaterals, polygons....whatever) and I am having trouble with the collision of a point (the cursor) inside of them. Here is what my polygon/trapezium/quadrilateral looks like: ******* ********* *********** ************* *************** I know how to check for the rectangle part (well of course, that''s the easy bit!) but I am having troubles for the right side slope part. I was thinking of breaking the polygon up into a rectangle and a triangle, but then that leads to something else: triangle collision. Could anyone please help me with this? Or maybe point to some tutorials/articles on this?? (I''ve been checking around already, but failed to find something suitable) Thanks for your time (Remember all I want to check is a single point inside of the polygon)

Share this post


Link to post
Share on other sites
make a line from the cursor point, to somewhere outside the polygon. If that line intersects an even number of edges, the point is outside the polygon, if the line intersects an odd number of edges, the point is inside the polygon

My Site

Share this post


Link to post
Share on other sites
The rough and tough way to do it is with some nice geometry. We all remember having to draw things like:

y > x + 3;

With the dotted line, and the shaded area above. Well, lucky for us, we can make if statements with multiple parts

This means that whenever we''re working with concave shapes, as you are, you can simply do a multipart if statement to test if the point is in the shape.

So you''d have something like x > leftBorder && y > bottomBorder && y < topBorder && y < -x + constant (just a guess of the slope). That''s how I did collision in my shooter.


--Vic--

The future of 2D game development:
Flat Red Ball

Share this post


Link to post
Share on other sites