简单的碰撞检测 - Android
我想在类似乒乓球的游戏中进行非常简单的碰撞检测。 球是正方形,球拍(球棒)是长方形。
我有两个实体可以获取当前的 X 和 Y 位置以及位图高度和宽度。哪种方法最简单?
我有这个代码:
public void getCollision(Entity enitityOne, Entity enitityTwo){
double eventCoordX = (enitityOne.getCenterX() - (enitityTwo.getBitmapWidth() / 2));
double eventCoordY = (enitityOne.getCenterY() - (enitityTwo.getBitmapHeight() / 2));
double X = Math.abs(enitityTwo.getxPos() - eventCoordX);
double Y = Math.abs(enitityTwo.getyPos() - eventCoordY);
if(X <= (enitityTwo.getBitmapWidth()) && Y <= (enitityTwo.getBitmapHeight())){
enitityOne.collision();
enitityTwo.collision();
}
}
但我很盲目,这只适用于桨的中间而不是侧面。 问题是我看不出代码哪里错了。 有人吗? 有人有更好的主意吗?
I want to do really simple collision detection in a pong like game.
The ball is a square and the paddle (bats) is rectangles.
I have two entities coming in where I can get the current X and Y position, and the bitmap height and width. Which is the simplest way to do this?
I have this code:
public void getCollision(Entity enitityOne, Entity enitityTwo){
double eventCoordX = (enitityOne.getCenterX() - (enitityTwo.getBitmapWidth() / 2));
double eventCoordY = (enitityOne.getCenterY() - (enitityTwo.getBitmapHeight() / 2));
double X = Math.abs(enitityTwo.getxPos() - eventCoordX);
double Y = Math.abs(enitityTwo.getyPos() - eventCoordY);
if(X <= (enitityTwo.getBitmapWidth()) && Y <= (enitityTwo.getBitmapHeight())){
enitityOne.collision();
enitityTwo.collision();
}
}
But I'm pretty blind, this only works in the middle of the paddle not on the sides.
The problem is I can't see where the code is wrong.
Anybody?
Anybody have a better idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想要的只是查找 2 个给定矩形是否以某种方式相交(因此发生碰撞),这是最简单的检查(C 代码;随意使用浮点值):
矩形 A 和 B 由最小值和其角点的最大 X 和 Y 坐标。
嗯...之前已经问过这个问题。
If all you want is to find whether or not 2 given rectangles somehow intersect (and therefore collide), here's the simplest check (C code; feel free to use floating-point values):
The rectangles A and B are defined by the minimum and maximum X and Y coordinates of their corners.
Um... This has been asked before.
如果您正在处理矩形,那么
这是一个很好的例子......
if you are dealing with rectangles then
this is a good example...