Java 检测到达图像的物体
我是这个领域的新手,我有一个问题-Java 检测到达图像的物体,java,android,canvas,Java,Android,Canvas,我是这个领域的新手,我有一个问题- 我有一个图像和一个物体。让我们将对象称为“球” 球在移动,并且在某个点到达图像。这是我的问题- 如何检测球是否接触到图像? 提前谢谢。您应该以某种数学方式对对象建模-例如,作为闭合多边形。然后,您可以搜索两个多边形相交的算法。如果您使用的是2D,您可以使用简单的矩形碰撞检测。 基本上,要检查一个矩形是否与另一个矩形发生碰撞,您必须检查其X和Y位置及其宽度和高度 下面是一个使用html5标记画布的JSFIDLE示例 jsfiddle: 检查碰撞的代码是 func
我有一个图像和一个物体。让我们将对象称为“球”
球在移动,并且在某个点到达图像。这是我的问题-
如何检测球是否接触到图像?
提前谢谢。您应该以某种数学方式对对象建模-例如,作为闭合多边形。然后,您可以搜索两个多边形相交的算法。如果您使用的是2D,您可以使用简单的矩形碰撞检测。 基本上,要检查一个矩形是否与另一个矩形发生碰撞,您必须检查其X和Y位置及其宽度和高度 下面是一个使用html5标记画布的JSFIDLE示例 jsfiddle: 检查碰撞的代码是
function Mathematics()
{
}
Mathematics.prototype.PointInRect = function(pnt_x, pnt_y, rect_x, rect_y, rect_w, rect_h)
{
if ( (pnt_x >= rect_x) && (pnt_x <= rect_x + rect_w - 1) )
{
if ( (pnt_y >= rect_y) && (pnt_y <= rect_y + rect_h - 1) )
{return true;}
}
return false;
}
Mathematics.prototype.RectToRectCollision = function(rect1_x, rect1_y, rect1_w, rect1_h,
rect2_x, rect2_y, rect2_w, rect2_h)
{
// top-left corner
if ( this.PointInRect(rect1_x, rect1_y, rect2_x, rect2_y, rect2_w, rect2_h) ){return true;}
// top-right corner
if ( this.PointInRect(rect1_x + rect1_w - 1, rect1_y, rect2_x, rect2_y, rect2_w, rect2_h) ){return true;}
// bottom-right corner
if ( this.PointInRect(rect1_x + rect1_w - 1, rect1_y + rect1_h - 1, rect2_x, rect2_y, rect2_w, rect2_h) ){return true;}
// bottom-left corner
if ( this.PointInRect(rect1_x, rect1_y + rect1_h - 1, rect2_x, rect2_y, rect2_w, rect2_h) ){return true;}
// Check to see if rectangle 2 is hit any part of rectanlge 1
// top-left corner
if ( this.PointInRect(rect2_x, rect2_y, rect1_x, rect1_y, rect1_w, rect1_h) ){return true;}
// top-right corner
if ( this.PointInRect(rect2_x + rect2_w - 1, rect2_y, rect1_x, rect1_y, rect1_w, rect1_h) ){return true;}
// bottom-right corner
if ( this.PointInRect(rect2_x + rect2_w - 1, rect2_y + rect2_h - 1, rect1_x, rect1_y, rect1_w, rect1_h) ){return true;}
// bottom-left corner
if ( this.PointInRect(rect2_x, rect2_y + rect2_h - 1, rect1_x, rect1_y, rect1_w, rect1_h) ){return true;}
// If there is no collision
return false;
}
函数数学()
{
}
Mathematics.prototype.PointInRect=函数(pnt_x,pnt_y,rect_x,rect_y,rect_w,rect_h)
{
如果((pnt_x>=rect_x)和&(pnt_x=rect_y)和&(pnt_y)