Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.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# 矩形列表使用intersect检查冲突的语法是什么_C#_Collision Detection_Xna 4.0 - Fatal编程技术网

C# 矩形列表使用intersect检查冲突的语法是什么

C# 矩形列表使用intersect检查冲突的语法是什么,c#,collision-detection,xna-4.0,C#,Collision Detection,Xna 4.0,我现在想知道如何检测与矩形列表的冲突 Rectanlge Player; List<Rectangle> BlockHitBox = new List<Rectangle>(); Rectanlge播放器; List BlockHitBox=新列表(); 我不知道正确的语法 BlockHitBox.Intersect<>(); BlockHitBox.Intersect(); 代码应该检测矩形命中框是否与播放器命中框发生碰撞 我的目标

我现在想知道如何检测与矩形列表的冲突

    Rectanlge Player;
    List<Rectangle> BlockHitBox =  new List<Rectangle>();
Rectanlge播放器;
List BlockHitBox=新列表();
我不知道正确的语法

BlockHitBox.Intersect<>();
BlockHitBox.Intersect();
代码应该检测矩形命中框是否与播放器命中框发生碰撞


我的目标是从玩家无法通过的小矩形中创建一个房间,因此我需要一次检测多个碰撞(对于拐角)

您可以使用
矩形
对象的
相交方法,如下所示:

var commonSize = new Size(100, 100);
var player = new Rectangle(new Point(0,0), commonSize);
var blockHitBox = new List<Rectangle>
{
    new Rectangle(new Point(0, 100), commonSize), // This one will not collide
    new Rectangle(new Point(100, 0), commonSize), // This one will not collide
    new Rectangle(new Point(0, 99), commonSize) // This one will collide
};

bool collision = blockHitBox.Any(item => item.IntersectsWith(player));
var commonSize=新大小(100100);
var player=新矩形(新点(0,0),共用);
var blockHitBox=新列表
{
新建矩形(新点(0,100),共用),//此矩形不会碰撞
新建矩形(新点(100,0),共用),//此矩形不会碰撞
新建矩形(新点(0,99),共用)//此矩形将发生碰撞
};
bool collision=blockHitBox.Any(item=>item.IntersectsWith(player));

这取决于您是否愿意:1)所有矩形都应该命中2)只命中一次就足够了。知道这一点后,只需重复。如果您想要奇特的LINQ语句,请在此处搜索
All
ForEach
Single
Intersect
。它用于返回两个
IEnumerable
对象之间的公共项。