Java中两个图像之间的碰撞检测
我正在编写的游戏中显示了两个角色:玩家和敌人。 定义如下:
public void player(Graphics g) {
g.drawImage(plimg, x, y, this);
}
public void enemy(Graphics g) {
g.drawImage(enemy, 200, 200, this);
}
然后调用:
player(g);
enemy(g);
我可以用键盘移动player(),但在尝试检测两者之间的碰撞时我不知所措。 很多人都说要使用矩形,但作为初学者,我不知道如何将其链接到我现有的代码中。 有人可以给我一些建议吗?
I have two characters displayed in a game I am writing, the player and the enemy. defined as such:
public void player(Graphics g) {
g.drawImage(plimg, x, y, this);
}
public void enemy(Graphics g) {
g.drawImage(enemy, 200, 200, this);
}
Then called with:
player(g);
enemy(g);
I am able to move player() around with the keyboard, but I am at a loss when trying to detect a collision between the two. A lot of people have said to use Rectangles, but being a beginner I cannot see how I would link this into my existing code. Can anyone offer some advice for me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我认为你的问题是你没有为你的玩家和敌人使用良好的面向对象设计。 创建两个类:
您的播放器应该有 X、Y、宽度和高度变量。
你的敌人也应该如此。
在你的游戏循环中,执行如下操作 (C#):
为了加快速度,不必费心检查敌人坐标是否在屏幕外。
I think your problem is that you are not using good OO design for your player and enemies. Create two classes:
Your Player should have X,Y,Width,and Height variables.
Your enemies should as well.
In your game loop, do something like this (C#):
To speed things up, don't bother checking if the enemies coords are offscreen.
首先,使用 Jonathan Holland 描述的边界框看看是否可能发生碰撞。
从(多色)精灵创建黑白版本。 如果你的精灵是透明的(即边界框内有一些地方,但你仍然可以看到背景),你可能已经有了这些。 这些是“面具”。
在蒙版上使用 Image.getRGB() 来获取像素。 对于每个不透明的像素,在整数数组(下面的
playerArray
和enemyArray
)中设置一个位。 如果width <= 32
像素,则数组的大小为height
,否则为(width+31)/32*height
。 下面的代码适用于width <= 32
。如果边界框发生碰撞,请执行以下操作:
链接:JGame, < a href="http://vredungmand.dk/programming/sjg/" rel="nofollow noreferrer">小型 Java 游戏框架
First, use the bounding boxes as described by Jonathan Holland to find if you may have a collision.
From the (multi-color) sprites, create black and white versions. You probably already have these if your sprites are transparent (i.e. there are places which are inside the bounding box but you can still see the background). These are "masks".
Use
Image.getRGB()
on the mask to get at the pixels. For each pixel which isn't transparent, set a bit in an integer array (playerArray
andenemyArray
below). The size of the array isheight
ifwidth <= 32
pixels,(width+31)/32*height
otherwise. The code below is forwidth <= 32
.If you have a collision of the bounding boxes, do this:
Links: JGame, Framework for Small Java Games
您不希望在绘画代码中包含碰撞检查代码。 绘画需要速度快。 碰撞可以进入游戏循环。 因此,您需要独立于其精灵的对象的内部表示。
You don't want to have the collision check code inside the painting code. The painting needs to be fast. Collision can go in the game loop. Therefore you need an internal representation of the objects independent of their sprites.
这是我的碰撞检测程序的主类。
您可以在以下位置查看它的运行情况:http://www.youtube.com/watch?v=JIXhCvXgjsQ< /a>
Here's the main class from my collision detection program.
You can see it run at: http://www.youtube.com/watch?v=JIXhCvXgjsQ
由于 Java 没有相交函数(真的!?),您可以通过简单地比较每个可能发生碰撞的对象的边界框(矩形)的 X 和 Y、宽度和高度值来进行碰撞检测。
所以...在每个碰撞对象的基础对象中...即,如果你的玩家和敌人有一个共同的基础,你可以放置一个简单的矩形对象,称为 BoundingBox 之类的东西。 如果公共基础是内置的 Java 类,那么您需要创建一个扩展内置类的类,并让玩家和敌人对象扩展您的新类或者是该类的实例。
在创建时(以及每次勾选或更新),您需要为玩家和敌人设置 BoundingBox 参数。 我面前没有 Rectangle 类,但它很可能是 X、Y、宽度和高度之类的东西。 X 和 Y 是游戏世界中的对象位置。 我认为宽度和高度是不言自明的。 它们很可能会从玩家位置的右侧出现,因此,如果 X 和 Y 均为 0,并且宽度和高度均为 256,那么您将看不到任何内容,因为角色位于左上角屏幕之外。
无论如何......要检测碰撞,您需要比较玩家和敌人边界框的属性。 像这样的事情......
逻辑可能会变得有点复杂,但您需要比较每个 BoundingBox 之间的距离并比较位置。
Since Java doesn't have an intersect function (really!?) you can do collision detection by simply comparying the X and Y, Width and Height values of the bounding boxes (rectangle) for each of the objects that could potentially collide.
So... in the base object of each colliding object... i.e. if your player and enemy have a common base you can put a simple Rectangle object called something like BoundingBox. If the common base is a built in Java class then you'll need to create a class that extends the build in class and have the player and enemy objects extend your new class or are instances of that class.
At creation (and each tick or update) you'll need to set the BoundingBox paremeters for both your player and enemy. I don't have the Rectangle class infront of me but its most likely something like X, Y, Width and finally Height. X and Y are that objects location in your game world. The width and height are self explanatory I think. They'll most likely come out from the right of the players location though so, if the X and Y were bothe at 0 and your Width and Height were both at 256 you wouldn't see anything because the character would be at the top left outside of the screen.
Anyways... to detect a collision, you'll want to compare the attributes of the player and enemy BoundingBoxes. So something like this...
The logic can get sort of complicated but you'll need to compare the distances between each BoundingBox and compare locations.
这是一个使用大量碰撞的开源游戏,非常有用:http://robocode.sourceforge.net/
您可以查看代码并补充此处写的答案。
Here's a useful of an open source game that uses a lot of collisions: http://robocode.sourceforge.net/
You may take a look at the code and complement with the answers written here.
有问题吗:
这也可以应用于圆圈:
要检查您是否已经到达屏幕边缘,您可以使用以下命令:
is there a problem with:
this can also be applied to circles:
to check whether youve hit the edge of a screen you could use the following:
使用矩形包围每个玩家和敌人,矩形的高度和宽度应与您包围的物体相对应,想象它位于一个仅足以容纳它的盒子中。
现在,您像移动对象一样移动这些矩形,因此它们有一个“边界框”,
我不确定Java是否有这个,但它可能在矩形对象上有一个名为 .intersects() 的方法,所以您' d 执行 if(rectangle1.intersectS(rectangle2) 来检查一个对象是否与另一个对象发生碰撞。
否则,您可以获得框的 x 和 y 坐标,并使用它们的高度/宽度检测它们是否相交不管怎样
,你可以用它来在交叉点上执行一个事件(使一个爆炸,或其他什么)或阻止绘制运动(恢复到以前的坐标)
编辑:这里我们使用
布尔值
所以我会这样做(并且不要粘贴这段代码,它很可能不起作用,很长一段时间没有完成java,而且当我使用它时我没有做图形。)
显然你需要适应它在某处。
Use a rectangle to surround each player and enemy, the height and width of the rectangles should correspond to the object you're surrounding, imagine it being in a box only big enough to fit it.
Now, you move these rectangles the same as you do the objects, so they have a 'bounding box'
I'm not sure if Java has this, but it might have a method on the rectangle object called .intersects() so you'd do if(rectangle1.intersectS(rectangle2) to check to see if an object has collided with another.
Otherwise you can get the x and y co-ordinates of the boxes and using the height/width of them detect whether they've intersected yourself.
Anyway, you can use that to either do an event on intersection (make one explode, or whatever) or prevent the movement from being drawn. (revert to previous co-ordinates)
edit: here we go
boolean
So I would do (and don't paste this code, it most likely won't work, not done java for a long time and I didn't do graphics when I did use it.)
obviously you'd need to fit that in somewhere.
无需使用矩形...不断比较 2 个玩家的坐标。
喜欢
if(x1===x&&y1==y)
记住在比较时增加x的范围。
如果你的矩形宽度为30,则视为
if (x1>x&&x2>x+30)
..同样yNo need to use rectangles ... compare the coordinates of 2 players constantly.
like
if(x1===x&&y1==y)
remember to increase the range of x when ur comparing.
if ur rectangle width is 30 take as
if (x1>x&&x2>x+30)
..likewise y首先创建类Player和Entity
接下来将这些类添加到Main类中,并添加方法:
Main类:
First create classes Player and Entity
Next add this classes to Main class, and add method:
Main class:
我重新编写了自己的框架,这次碰撞非常重要。 我想我把它做得很简单。 它适用于比例和 BufferedImage 方法 getRGB(x,y)。
I reasently coded my own framework and this collision was very important. I think i made it very simple. It works with scale and the BufferedImage Method getRGB(x,y).
这是用于两个或多个 ImageView 碰撞的 Java 代码,而不是矩形或其他,请使用 ImageViews 代替。
1.此碰撞代码适用于任何视图或布局中的任何地方。
2.添加定时器重复执行并重复检测碰撞。
3.它仅适用于视图和布局。
It's Java code for collision of two or more ImageViews not rectangles or other,use ImageViews Instead.
1.This code of Collision works every where in any views or layouts.
2.Add a timer to repeat it and to detect collision repeatedly.
3.It only works with views and layout.