简体   繁体   English

如何检测三角位图碰撞Android?

[英]How to detect triangle bitmap collisions android?

I'm making an Android game and I'm having some problems with bitmap collision detection. 我正在制作一个Android游戏,但在位图碰撞检测方面遇到了一些问题。 The problem is that one of the bitmaps is a triangle and the second one is a rectangle. 问题在于位图之一是三角形,第二个是矩形。 I don't know how to detect collision between them because now I use this code: 我不知道如何检测它们之间的冲突,因为现在我使用以下代码:

if(policeY<((canvas.getHeight()/20)+eye.getHeight()) && (policeY+police.getHeight())>(canvas.getHeight()/20)){
            if((policeX+police.getWidth())>triangleLeft && policeX<(triangleLeft+eye.getWidth())){
                //collision
                play = false;
            }else if((policeX+police.getWidth())<triangleLeft && policeX>(triangleLeft+eye.getWidth())){
                //collision
                play = false;
            }

        }

However, this code handles both of them as rectangles and this causes collision even when triangle isn't even touching rectangle. 但是,此代码将它们都当作矩形处理,即使三角形甚至没有接触矩形,这也会导致碰撞。

Like in this picture the circled area is considered as a collision 像这张照片一样,圆圈区域被认为是碰撞

I'd set up the problem as an intersection problem. 我将这个问题设置为交叉问题。 Define the edges of the triangles as lines then use the line-line intersection formula with the edges of the rectangle. 将三角形的边缘定义为线,然后将线与线的交集公式与矩形的边缘一起使用。 Then check whether this point is on the edge of the rectangle or if it is outside the rectangle. 然后检查该点是在矩形的边缘还是在矩形的外部。

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

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