游戏中的碰撞。
public static bool RectIntersect(Rectangle rect1, Rectangle rect2)
{
return (
((double)rect1.GetValue(Canvas.LeftProperty) <= (double)rect2.GetValue(Canvas.LeftProperty) + rect2.Width) &&
((double)rect1.GetValue(Canvas.TopProperty) <= (double)rect2.GetValue(Canvas.TopProperty) + rect2.Height) &&
((double)rect2.GetValue(Canvas.LeftProperty) <= (double)rect1.GetValue(Canvas.LeftProperty) + rect1.Width) &&
((double)rect2.GetValue(Canvas.TopProperty) <= (double)rect1.GetValue(Canvas.TopProperty) + rect1.Height)
);
}
构造了这样一个检测碰撞的函数。
如果一个为假,就为假——不碰撞。全部条件为真,才说明了碰撞。
由于每个Rectangle的“坐标点”在左上角,所以长宽的问题就是这样解决,如果坐标点在中心,可能就是中心在长宽的一半来计算。