游戏中的碰撞。
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的“坐标点”在左上角,所以长宽的问题就是这样解决,如果坐标点在中心,可能就是中心在长宽的一半来计算。
本文介绍了一个用于游戏开发中碰撞检测的函数实现。该函数通过比较两个矩形对象的坐标位置及尺寸来判断它们是否发生碰撞。当所有条件都满足时,则认为发生了碰撞。

被折叠的 条评论
为什么被折叠?



