php中实现矩阵的碰撞检测
<?php class Rect{ private $x; private $y; private $width; private $height; function __construct($x,$y,$width,$height){ $this->x = $x; $this->y = $y; $this->width = $width; $this->height = $height; } function panDuan($px,$py){ $x1 = $this->x; $x2 = $this->x+$this->width; $y1 = $this->y; $y2 = $this->y+$this->height; if (($px>=$x1) && ($px<=$x2) && ($py>=$y1) && ($py<= $y2)) { echo "in"; }else{ echo "over"; } } function rectArc($dx1,$dx2){ //第一个矩形的左上角的x,y,width,height; $x1 = $dx1->x; $y1 = $dx1->y; $w1 = $dx1->width; $h1 = $dx1->height; //第二个矩形的左上角的x,y,width,height; $x2 = $dx2->x; $y2 = $dx2->y; $w2 = $dx2->width; $h2 = $dx2->height; //两个矩形的宽度的一半 $heng = $w1/2+$w2/2; //两个矩形的高度的一半 $shu = $h1/2+$h2/2; //第一个矩形的中心的x,y值 $jxx1 = $x1+$w1/2; $jxy1 = $y1+$h1/2; //第二个矩形的中心的x,y值 $jxx2 = $x2+$w2/2; $jxy2 = $y2+$h2/2; //判断是否横碰撞 $bool1 = abs($jxx1-$jxx2)<=$heng; //判断是否竖碰撞 $bool2 = abs($jxy1-$jxy2)<=$shu; if ($bool1&&$bool2) { echo "碰撞"; }else{ echo "不碰撞"; } } } $r1 = new Rect(1,1,2,2); $r2 = new Rect(4,4,1,1); $r1->rectArc($r1,$r2); ?>
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。