Java游戏中的碰撞检测? [英] Collision detection in Java game?

查看:146
本文介绍了Java游戏中的碰撞检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一种游戏,其中我有运动图像的碰撞检测问题。游戏中有一个宇宙飞船和数量的小行星(障碍物)。我想检测它们之间的碰撞。我怎么能这样做?

I am developing a game in which I have the problem of collision detection of moving images. The game has a spaceship and number of asteroids (obstacles). I want to detect the collision between them. How can I do this?

推荐答案

对于除矩形之外的其他任何事情,碰撞检测通常都很棘手。

Collision detection is generally tricky for anything other than rectangles.

我过去这样做的方法是为每个对象提供图像和掩码。例如,迷失太空中的木星2太空船等对象将具有以下图像和掩码:

The way I've done this in the past is to provide both an image and a mask for each object. So for example, an object like the Jupiter 2 spaceship from Lost in Space would have the following image and mask:

     X            00000100000
  XXXXXXX         00111111100
 X       X        01111111110
X         X       11111111111
 X       X        01111111110
  XXXXXXX         00111111100
    XXX           00001110000

image 被抨击屏幕,但掩码是用于碰撞检测的内容。您会注意到蒙版中的1基本上是图像的轮廓和内容。

The image is what gets blatted to the screen but the mask is what's used for collision detection. You'll notice that the 1's in the mask are basically the outline and contents of the image.

检测碰撞的方式:


  • 检查矩形是否重叠。如果没有,则不会发生碰撞。

  • 否则创建一个由其掩码组成的对象编号1的矩形。

  • 构造另一个矩形对象2由其掩码组成。

  • 按位-AND矩形2与矩形1的重叠部分。

  • 如果还有1位在矩形1中,你有碰撞。

  • check if the rectangles overlap. If not, there can be no chance of collision.
  • Otherwise create a rectangle of object number 1 consisting of its mask.
  • Construct another rectangle of object 2 consisting of its mask.
  • Bitwise-AND the overlapping part of rectangle 2 with rectangle 1.
  • If there are any 1-bits left in rectangle 1, you have collision.

这考虑了近乎未命中,其中每个物体的边界矩形重叠但不一定该对象概述了自己。按位运算符是一种有效的方法来检测它。

This takes into account "near misses" where the bounding rectangles of each object overlap but not necessarily the object outlines themselves. Bitwise operators are an efficient way to detect this.

这是一个箭头没有碰到气球的例子 - 在我的图形设计技能之前颤抖:

Here's an example of an arrow not quite hitting a balloon - tremble before my graphical design skills:

....xx....
..xx..xx..
.x......x.
.x......x.
x........x
x........x
.x......x.
.x......x.
..xx..xx..
....xx.**y.....
       .y......
       yyyyyyyy
       .y......
       ..y.....

您可以看到,即使矩形重叠(参见 ** y ),箭头实际上也没有与气球接触。通过对掩码应用按位AND运算,这些位将最终为零,从而导致非冲突。

You can see that, even though the rectangles overlap (see **y), the arrow has not actually made contact with the balloon. By applying the bitwise AND operation to the masks, those bits will end up as zero, resulting in a non-collision.

@kyoryu在他的评论中提出了一个有趣的观点。有些游戏很适合用较小的矩形组成对象,你可以根据矩形组件简化碰撞检测(不必担心像素完美)。例如,我们的老朋友太空入侵者(实际上后卫对抗该游戏中的太空入侵者)可能由两个矩形组成,X和Y,导弹由Z制成:

And @kyoryu raises an interesting point in his comment. Some games adapt well to having objects made up off smaller rectangles and you can simplify collision detection based on the rectangular components (without worrying about pixel perfection). For example, our old friend the space invader (actually the defender against the space invaders in that game) may be made up of two rectangles, X and Y with the missiles being made from Z:

    YYYY                .Z.
    YYYY                .Z.
XXXXXXXXXXXX            .Z.
XXXXXXXXXXXX            ZZZ
XXXXXXXXXXXX            .Z.
XXXXXXXXXXXX

这将归结为针对两个空间的导弹的简单矩形检查入侵者矩形 - 考虑到导弹的大小,即使你联系其中一个字符,也可能称它为碰撞(考虑它们是近距离导弹而不是撞击导弹品种)。

This would come down to a simple rectangular check of the missile against the two space invader rectangles - Given the size of the missile, you could probably call it a collision even if you contact one of the . characters (consider them proximity missiles rather than those of the impact variety).

这篇关于Java游戏中的碰撞检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆