How to check if one graphic is overlapping another?
I've looked on Google and still couldn't find anything. I had an idea for a simple Snake type game or like a 'Coin Collection' game using 2D graphics, but if a coin is a graphic and the moving character is a graphic, how do I che开发者_运维知识库ck if the character goes over the coin? I'm stumped. Any ideas?
For a crude implementation have all your sprites backed by a Rectangle2D object, and use the intersects method to test for collision. Caveat, this is very crude!
Yes, the classic Picking and Selection problem. It's a bit long to explain here - please read http://download.oracle.com/javase/tutorial/2d/advanced/user.html . And also, the easiest is to use contains(MousePoint) .
See this Picking in java 2d .
I can't think of any way to do this using the graphics package; moreover, I think this is something you should do in your model rather than your graphics.
The problem you're looking at is generally called "collision detection". There are many different approaches to this; looking around online for some guides would be useful. However, I think one simple approach is to think of each object (coin, snake...etc) as a rectangle, making the math really simple. Circles (for the coin) should not be too bad either.
精彩评论