Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android中两个Rect之间的冲突检测_Java_Android_Collision Detection_Accelerometer_Rect - Fatal编程技术网

Java android中两个Rect之间的冲突检测

Java android中两个Rect之间的冲突检测,java,android,collision-detection,accelerometer,rect,Java,Android,Collision Detection,Accelerometer,Rect,我使用canvas在画布上绘制了两个矩形。drawRect(上、左、右、下)注意,其中一个矩形是静态的,另一个是使用android加速计移动的 现在我想检测这两个矩形之间的碰撞 我尝试了很多方法,但它们并不完美——两个矩形有时会重叠,那么什么是检测它们之间碰撞的完美方法呢 我就是这么想的 英雄是移动的三角形 迷宫组件是静态矩形 if ((hero.top >= mazeComponent.top) && (hero.t

我使用
canvas在画布上绘制了两个矩形。drawRect(上、左、右、下)
注意,其中一个矩形是静态的,另一个是使用android加速计移动的

  • 现在我想检测这两个矩形之间的碰撞
  • 我尝试了很多方法,但它们并不完美——两个矩形有时会重叠,那么什么是检测它们之间碰撞的完美方法呢

    我就是这么想的 英雄是移动的三角形 迷宫组件是静态矩形

                if ((hero.top >= mazeComponent.top)
                    && (hero.top <= mazeComponent.bottom)
                    && (hero.left <= mazeComponent.right)) {
                collision = true;
            }
            if ((hero.bottom >= mazeComponent.top)
                    && (hero.bottom <= mazeComponent.bottom)
                    && (hero.right >= mazeComponent.left)) {
                collision = true;
            }
            if ((hero.left >= mazeComponent.left)
                    && (hero.left <= mazeComponent.right)
                    && (hero.top <= mazeComponent.bottom)) {
                collision = true;
            }
            if ((hero.top >= mazeComponent.top)
                    && (hero.top <= mazeComponent.bottom)
                    && (hero.left >= mazeComponent.left)) {
                collision = true;
            }
            if ((hero.left >= mazeComponent.left)
                    && (hero.left <= mazeComponent.right)
                    && (hero.top >= mazeComponent.top)) {
                collision = true;
            }
    
    if((hero.top>=mazeComponent.top)
    &&(hero.top=mazeComponent.left)
    &&(hero.left=mazeComponent.left)
    &&(hero.left=mazeComponent.top)){
    碰撞=真;
    }
    
    Java/Android Rect和Rectangle对象有一种方法可以用来测试碰撞

    此函数检测碰撞是的,但两个矩形仍然重叠。。我想阻止移动的一个合并到静态的一个!移动的矩形实际上是如何移动的?在一个常量循环中重复重画?如果是这样的话,你就不能在重新绘制之前测试一个十字路口吗?如果新位置将相交,是否不重新绘制?