如何检查一个图形是否与另一个图形重叠?

发布于 2024-12-06 10:12:14 字数 111 浏览 1 评论 0原文

我已经在谷歌上查看过,但仍然找不到任何东西。我有一个简单的蛇类型游戏的想法,或者像使用 2D 图形的“硬币收集”游戏,但如果硬币是图形并且移动的角色是图形,我如何检查角色是否越过硬币?我很困惑。有什么想法吗?

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 check if the character goes over the coin? I'm stumped. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

策马西风 2024-12-13 10:12:14

对于粗略的实现,让所有精灵都由 Rectangle2D 对象支持,并使用 intersects 方法来测试碰撞。警告,这是非常粗糙的!

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!

梦旅人picnic 2024-12-13 10:12:14

是的,经典的挑选和选择问题。这里解释起来有点长 - 请阅读 http://download. oracle.com/javase/tutorial/2d/advanced/user.html 。而且,最简单的方法是使用 contains(MousePoint) 。

请参阅此 选取java 2d .

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 .

请恋爱 2024-12-13 10:12:14

我想不出任何方法可以使用图形包来做到这一点;此外,我认为这是您应该在模型而不是图形中执行的操作。

您正在查看的问题通常称为“碰撞检测”。对此有许多不同的方法;在网上查找一些指南会很有用。然而,我认为一种简单的方法是将每个对象(硬币、蛇等)视为一个矩形,使数学变得非常简单。圆圈(硬币)也不应该太糟糕。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文