Jump to content
  • Advertisement


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


Simple 2D collision help

This topic is 5644 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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.


The future of 2D game development:
Flat Red Ball

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!