简体   繁体   English

Android-位图碰撞检测(旋转)

[英]Android - Bitmap collision detection (rotated)

I already know about rectangle collision detection with rectangles, but this time I have 2 rotated bitmaps. 我已经知道矩形的矩形碰撞检测,但是这次我有2个旋转的位图。 I mean, I have normal bitmaps + a float variable called "direction" and telling in which direction the bitmap must be rotated at drawing. 我的意思是,我有普通的位图+一个称为“方向”的float变量,告诉位图在绘制时必须沿哪个方向旋转。

But how do I find out if 2 of those objects are bumping into each other? 但是,如何确定其中两个对象是否相互碰撞? It would be enought, too, if you could say me how collision detection between 2 rotated rectangles works... Or maybe you could post some code... 如果您可以说两个旋转的矩形之间的碰撞检测是如何工作的,也就足够了……或者您可以发布一些代码...

Thank you if you can help (and thank you if you spent time on reading this or thinking about an answer) 谢谢您的帮助(如果您花时间阅读本文或思考答案,则谢谢)

Generally, you will needto use the Separating Axis Theorem to determine whether two rotated rectangles are colliding. 通常,您将需要使用“ 分离轴定理”来确定两个旋转的矩形是否发生碰撞。 But there is a simple way that you can tell whether the rectangles are colliding prior to using the SAT so that you don't have to do unneccesary processing. 但是,有一种简单的方法可以在使用SAT之前确定矩形是否正在碰撞,这样您就不必进行不必要的处理。 You can do a bounding circle check, where you prove that the rectangles dont intersect by proving that their bounding circles dont intersect. 您可以进行边界圆检查,通过证明矩形的边界圆不相交来证明矩形不相交。

The bounding circle of a rectangle shares its center and has a diameter equal to the length of either diagonal of the rectangle. 矩形的边界圆共享其中心,并且其直径等于矩形的任一对角线的长度。 Essentially, if the circles do not intersect, than the rectangles cannot intersect either. 本质上,如果圆不相交,则矩形也不能相交。

I'm not sure how you are performing your rotations, but if you are using Shape/Area type objects, you can use AffineTransform to perform the rotation and then use intersects() on the Area of each rotated object to check if they collide, this saves you from implementing it yourself. 我不确定您如何执行旋转,但是如果您使用Shape / Area类型的对象,则可以使用AffineTransform进行旋转,然后在每个旋转对象的Area上使用intersects()来检查它们是否发生碰撞,这样可以避免您自己实施。 Consider the following example: 考虑以下示例:

import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;

public class Main {

    public static void main(String args[]) {
        //Create rectangle 
        Rectangle rect = new Rectangle(10, 10, 10, 10);
        //Create transformation object
        AffineTransform af = new AffineTransform();
        //Rotate the rectangle by 45 degrees
        af.rotate(Math.PI/4, rect.x, rect.y); 

        Rectangle rect2 = new Rectangle(20, 20, 20, 20);
        AffineTransform bf = new AffineTransform();
        bf.rotate(Math.PI/4, rect2.x, rect2.y);

        //Create Area objects based off of the Rectangle objects
        Area areaA = new Area(rect);
        //Set the Area object to be the same as the Rectangle object
        areaA = areaA.createTransformedArea(af);
        Area areaB = new Area(rect2);
        areaB = areaB.createTransformedArea(bf);

        //Check if the objects collide by using their Area equivalent
        if (areaA.intersects(areaB.getBounds())) {
            System.out.println("Collision!");
        }
    }
}

You can obviously, modify this to suite your implementation - I'm hoping this points you in the right direction. 您显然可以修改它以适合您的实现-我希望这可以为您指明正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM