Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.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/2/ssis/2.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
C# c形式:检查两个矩形碰撞的位置_C#_.net_Collision Detection_Rectangles - Fatal编程技术网

C# c形式:检查两个矩形碰撞的位置

C# c形式:检查两个矩形碰撞的位置,c#,.net,collision-detection,rectangles,C#,.net,Collision Detection,Rectangles,如何进行简单的碰撞检测,以便知道它们在何处发生碰撞,以及如何在if-else语句中使用这些碰撞检测?首先,通过示例,您必须先将它们进行比较 class myRectangle { public Rectangle r = new Rectangle(); public int speedX = 5; public int speedY = 5; } 对于(int first=0;first首先,举例来说,您必须首先将其与其他进行比较 class myRectangle

如何进行简单的碰撞检测,以便知道它们在何处发生碰撞,以及如何在if-else语句中使用这些碰撞检测?

首先,通过示例,您必须先将它们进行比较

class myRectangle
{
    public Rectangle r = new Rectangle();
    public int speedX = 5;
    public int speedY = 5;
}

对于(int first=0;first首先,举例来说,您必须首先将其与其他进行比较

class myRectangle
{
    public Rectangle r = new Rectangle();
    public int speedX = 5;
    public int speedY = 5;
}

for(int first=0;first使用rectangle.intersect怎么样?我认为它具有您想要的功能,并为您节省了一些编码

您可以这样使用它:

if(listRect[first].r.X<listRect[second].r.X)
{
   if(listRect[first].r.Right > listRect[second].r.Left)
   {
       listRect[first].speedX = ran.Next(-6,-4);
       listRect[second].speedX = ran.Next(4, 6);
   }
}
else
{
    if(listRect[first].r.Left < listRect[second].r.Right)
    {
       listRect[first].speedX = ran.Next(4, 6);
       listRect[second].speedX = ran.Next(-6,-4);
    }
}
if(Rect.r.IntersectsWith(rect2.r))
{
if(右前左<右后左)
rect.speedX=ran.Next(-6,-4);
rect2.speedX=ran.Next(4,6);
if(rect.r.Left>rect2.r.Left)
rect.speedX=ran.Next(4,6);
rect2.speedX=ran.Next(-6,-4);
if(rect.r.Toprect2.r.Top)
rect.speedY=ran.Next(-6,-4);
rect2.speedY=ran.Next(4,6);
}
您可以在上找到文档


编辑:你还需要比较从左到左和从上到上(或从右到右和从下到下等等)来改变你的速度,我编辑了代码片段。

使用rectangle.intersect怎么样?我想这有你想要的功能,并为你节省了一些代码

您可以这样使用它:

if(listRect[first].r.X<listRect[second].r.X)
{
   if(listRect[first].r.Right > listRect[second].r.Left)
   {
       listRect[first].speedX = ran.Next(-6,-4);
       listRect[second].speedX = ran.Next(4, 6);
   }
}
else
{
    if(listRect[first].r.Left < listRect[second].r.Right)
    {
       listRect[first].speedX = ran.Next(4, 6);
       listRect[second].speedX = ran.Next(-6,-4);
    }
}
if(Rect.r.IntersectsWith(rect2.r))
{
if(右前左<右后左)
rect.speedX=ran.Next(-6,-4);
rect2.speedX=ran.Next(4,6);
if(rect.r.Left>rect2.r.Left)
rect.speedX=ran.Next(4,6);
rect2.speedX=ran.Next(-6,-4);
if(rect.r.Toprect2.r.Top)
rect.speedY=ran.Next(-6,-4);
rect2.speedY=ran.Next(4,6);
}
您可以在上找到文档


<>编辑:你也只需要比较左到左,从右到右,从右到右,从底部到底部……来改变你的速度,我编辑了代码片段。

我认为你不应该把一个元素的左边和另一个元素的右边进行比较。
if (Rect.r.IntersectsWith(rect2.r))
                {
                    if (rect.r.Left < rect2.r.Left) 
                        rect.speedX = ran.Next(-6,-4);
                        rect2.speedX = ran.Next(4, 6);

                    if (rect.r.Left > rect2.r.Left)
                        rect.speedX = ran.Next(4, 6);
                        rect2.speedX = ran.Next(-6, -4);

                    if (rect.r.Top < rect2.r.Top)
                        rect.speedY = ran.Next(4, 6);
                        rect2.speedY = ran.Next(-6, -4);

                    if (rect.r.Top > rect2.r.Top)
                        rect.speedY = ran.Next(-6, -4);
                        rect2.speedY = ran.Next(4, 6);
                }

检查哪个矩形相对于另一个矩形不显示碰撞面。

< P>我认为你不应该将元素的左边与另一个元素的右边进行比较。请考虑这个代码:

if (Rect.r.IntersectsWith(rect2.r))
                {
                    if (rect.r.Left < rect2.r.Left) 
                        rect.speedX = ran.Next(-6,-4);
                        rect2.speedX = ran.Next(4, 6);

                    if (rect.r.Left > rect2.r.Left)
                        rect.speedX = ran.Next(4, 6);
                        rect2.speedX = ran.Next(-6, -4);

                    if (rect.r.Top < rect2.r.Top)
                        rect.speedY = ran.Next(4, 6);
                        rect2.speedY = ran.Next(-6, -4);

                    if (rect.r.Top > rect2.r.Top)
                        rect.speedY = ran.Next(-6, -4);
                        rect2.speedY = ran.Next(4, 6);
                }

检查哪个矩形相对于另一个矩形的位置不会显示碰撞面。

矩形是否沿轴移动?它们是旋转还是相互平行?它们“自由”移动沿着X,Y轴,可以从任何方向相互碰撞。没有旋转或类似的东西。我在下面添加了一个可能的解,我认为矩形。相交可能是一个好方法。祝你好运!矩形是沿着一个轴移动吗?它们是旋转还是保持平行?它们“自由”移动沿着X,Y轴,可以从任何方向相互碰撞。没有旋转或类似的东西。我在下面添加了一个可能的解决方案,我认为rectangle.intersect可能是一个好方法。祝你好运!List listRect=new List();这就是我初始化列表的方式我不确定它是否与您的解决方案兼容。哦,谢谢!我是否在矩形类中定义centerX属性?在矩形的哪个属性处添加speedX?哦..不..如何定义矩形的当前位置?呃,我不知道,但它将使用矩形.r.X和矩形.r.Y作为定义eC#矩形代码。List listRect=新列表();这就是我初始化列表的方式我不确定它是否与您的解决方案兼容。哦,谢谢!我是否在矩形类中定义centerX属性?在矩形的哪个属性处添加speedX?哦..不..如何定义矩形的当前位置?呃,我不知道,但它将使用矩形.r.X和矩形.r.Y作为定义e c#矩形代码。
                    rect.speedX = -1 * rect.speedX;
                    rect2.speedY = -1 * rect.speedY;