Android 碰撞检测
描述:
我有一个项目现在即将完成,但我注意到碰撞并没有真正起作用。这是一款类似蛇的游戏,可以通过触摸屏进行控制,因此可以实现锐利(?,对不起,德国)角度。目前,我只留下一点容差(忽略前 2 个精灵)以实现一点转动。主要问题是精灵正在旋转,这会导致超维碰撞盒。我没有使用任何游戏引擎或 OpenGL。
碰撞代码: 偏移X & offsetY是位图的宽度或高度/2,被称为蛇的头部。蛇(鸟)中的每个链接都是一个可放置的
public boolean doesHit(Placeable p) {
int xLen = Math.abs(this.x - p.x);
int yLen = Math.abs(this.y - p.y);
if (bmp != null) {
if (xLen < offsetX + p.offsetX && yLen < offsetY + p.offsetY)
return true;
} else {
if (xLen < Bird.BIG_W[Bird.mUseBird] / 2
&& yLen < Bird.BIG_H[Bird.mUseBird] / 2)
return true;
}
return false;
}
TL;DR /问题:
有没有办法旋转矩形然后比较它们(首选,因为除此之外游戏已经完成)?或者最简单的方法是移植到 OpenGL/游戏引擎?
Description:
I've got a project which is nearly finished now, but I've noticed the collision is not really working. It's a snake-like game that can be controlled via the touch-screen, so sharp (?, sorry german) angles are possible. At the moment I just leave a bit of tolerance (ignore the first 2 sprites) to enable a bit of turning. The main problem is that the sprites are being rotated which results in overdimensional collision boxes. I'm not using any game engines or OpenGL.
Collision Code:
offsetX & offsetY are the bitmaps width or height / 2, is called on the head of the snake. Each link in the snake (Bird) is a placeable
public boolean doesHit(Placeable p) {
int xLen = Math.abs(this.x - p.x);
int yLen = Math.abs(this.y - p.y);
if (bmp != null) {
if (xLen < offsetX + p.offsetX && yLen < offsetY + p.offsetY)
return true;
} else {
if (xLen < Bird.BIG_W[Bird.mUseBird] / 2
&& yLen < Bird.BIG_H[Bird.mUseBird] / 2)
return true;
}
return false;
}
TL;DR / Question:
Is there a way to rotate Rects and then compare them (preferred, as the game is already finished apart from this)? Or would the simplest way be to port to OpenGL / a game engine?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最好的选择是使用 OpenGL 并使用相交的旋转多边形。
然而,为了快速破解,我会更改 doesHit() 例程,将精灵视为圆形而不是矩形。这样它们在旋转时就不会超出边界。代价是角落里的碰撞检测会很糟糕。
The best option would be to go OpenGL and use rotated polygons with intersects.
However for quick hack i would change the doesHit() routine to consider the sprites as circles instead of rectangles. This way they will not grow outside bounds when rotated. The price is that collision detecting will be lousy in the corners.