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
对象之间的公共项。