碰撞检测在Android上旋转的位图 [英] Collision detection for rotated bitmaps on Android

查看:152
本文介绍了碰撞检测在Android上旋转的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为我的Andr​​oid游戏像素完美碰撞检测。我已经写了一些code检测具有正常位图(不旋转)碰撞;工作正常。不过,我不明白这一点的旋转位图。不幸的是,Java不具有旋转矩形一类,所以我实现了一个自己。它保持在相对于屏幕的四个角的位置和描述其位图的准确位置/层;所谓的itemSurface。我的解决检测的计划是:

I need pixel-perfect collision detection for my Android game. I've written some code to detect collision with "normal" bitmaps (not rotated); works fine. However, I don’t get it for rotated bitmaps. Unfortunately, Java doesn’t have a class for rotated rectangles, so I implemented one myself. It holds the position of the four corners in relation to the screen and describes the exact location/layer of its bitmap; called "itemSurface". My plan for solving the detection was to:


  1. 检测不同itemSurfaces的交集

  2. 计算重叠区域

  3. 设置有关这些地区以其优越的itemSurface /位图

  4. 每个单个像素与其他位图的相应像素进行比较

好吧,我在与第一个和第二个麻烦。是否有人有一个想法,或有一些code?也许已经有美元的Java / Android的库C $ c和我只是没有找到它。

Well, I’m having trouble with the first one and the second one. Does anybody has an idea or got some code? Maybe there is already code in Java/Android libs and I just didn’t find it.

推荐答案

我知道你是想矩形之间的碰撞检测(以不同的方式旋转)。不需要计算的重叠区域。此外,每一个象素比较将是无效的。

I understand that you want a collision detection between rectangles (rotated in different way). You don't need to calculate the overlapping area. Moreover, comparing every pixel will be ineffective.

实现一个静态布尔isCollision 函数,它会告诉你有一个方型,另一个发生碰撞。之前,您应该拿一张纸做一些几何图形,找出确切的公式。出于性能的原因并不在某些矩形包装类长方形,只需使用原始的类型,如双打等。

Implement a static boolean isCollision function which will tell you is there a collision between one rectangle and another. Before you should take a piece of paper do some geometry to find out the exact formulas. For performance reasons do not wrap a rectangle in some Rectangle class, just use primitive types like doubles etc.

然后(伪code):

for (every rectangle a)
  for (every rectangle b)
    if (a != b && isCollision(a, b))
      bounce(a, b)  

这是O(n ^ 2),其中n是矩形的数量。有更好的算法,如果你需要更多的性能。 反弹函数改变移动矩形,以便模仿碰撞的载体。如果物体的重量是一样的(你可以aproximate重量矩形的大小),你只需要交换两个速度向量。

This is O(n^2), where n is number of rectangles. There are better algorithms if you need more performance. bounce function changes vectors of moving rectangles so that imitates a collision. If the weight of objects was the same (you can aproximate weight with size of the rectangles), you just need to swap two speed vectors.

要正确反弹元素,你可能需要存储辅助表布尔alreadyBounced [] [] 来确定哪些矩形不需要他们向量后反弹(碰撞)的变化,因为他们已经反弹。

To bounce elements correctly you could need to store auxiliary table boolean alreadyBounced[][] to determine which rectangles do not need a change of their vectors after bounce (collision), because they were already bounced.

另外一个技巧:

如果你的Andr​​oid下做一个游戏,你必须小心,以在游戏中不分配的内存,因为它会更快调用GC,这需要很长的时间,缓慢起伏你的游戏。我建议你​​看的视频和相关的。祝你好运。

If you are making a game under Android you have to watch out to not allocate memory during gameplay, because it will faster invoke GC, which takes a long time and slow downs your game. I recommend you watching this video and related. Good luck.

这篇关于碰撞检测在Android上旋转的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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