本文整理汇总了C#中CCRect.Intersection方法的典型用法代码示例。如果您正苦于以下问题:C# CCRect.Intersection方法的具体用法?C# CCRect.Intersection怎么用?C# CCRect.Intersection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CCRect
的用法示例。
在下文中一共展示了CCRect.Intersection方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetSeparatingVector
CCVector2 GetSeparatingVector(CCRect first, RectWithDirection second)
{
//返回一个向量,是player在碰撞之后反方向移动的向量
// Default to no separation
CCVector2 separation = CCVector2.Zero;
// Only calculate separation if the rectangles intersect
if (Intersects(first, second))
{
// The intersectionRect returns the rectangle produced
// by overlapping the two rectangles.
// This is protected by partitioning and deep collision, so it
// won't happen too often - it's okay to do a ToRect here
//得到两个rect重叠部分的rect
var intersectionRect = first.Intersection (second.ToRect());
float minDistance = float.PositiveInfinity;
float firstCenterX = first.Center.X;
float firstCenterY = first.Center.Y;
float secondCenterX = second.Left + second.Width / 2.0f;
float secondCenterY = second.Bottom + second.Width / 2.0f;
//second的方向和想要判断的方向(左)取交集,如果和想要判断的方向(左)一致,且第一个的中心点在第二个的中心点的(左边)
//则当碰撞时,player可以向想要的方向移动
bool canMoveLeft = (second.Directions & Directions.Left) == Directions.Left && firstCenterX < secondCenterX;
bool canMoveRight = (second.Directions & Directions.Right) == Directions.Right && firstCenterX > secondCenterX;
bool canMoveDown = (second.Directions & Directions.Down) == Directions.Down && firstCenterY < secondCenterY;
bool canMoveUp = (second.Directions & Directions.Up) == Directions.Up && firstCenterY > secondCenterY;
if (canMoveLeft)
{
//左重叠
//得到重叠的rect的X方向边长
float candidate = first.UpperRight.X - second.Left;
if (candidate > 0)
{
minDistance = candidate;
//x方向移动回到地图实体瓦片的外面,y方向不动
separation.X = -minDistance;
separation.Y = 0;
}
}
if (canMoveRight)
{
//右重叠
float candidate = (second.Left + second.Width) - first.LowerLeft.X;
if (candidate > 0 && candidate < minDistance)
{
minDistance = candidate;
//向右移动
separation.X = minDistance;
separation.Y = 0;
}
}
//其他方向同理
if (canMoveUp)
{
float candidate = (second.Bottom + second.Height) - first.Origin.Y;
if (candidate > 0 && candidate < minDistance)
{
minDistance = candidate;
separation.X = 0;
separation.Y = minDistance;
}
}
if (canMoveDown)
{
float candidate = first.UpperRight.Y - second.Bottom;
if (candidate > 0 && candidate < minDistance)
{
minDistance = candidate;
separation.X = 0;
separation.Y = -minDistance;
}
}
}
//左后返回player移动的vector
return separation;
}
示例2: GetSeparatingVector
CCVector2 GetSeparatingVector(CCRect first, RectWithDirection second)
{
// Default to no separation
CCVector2 separation = CCVector2.Zero;
// Only calculate separation if the rectangles intersect
if (Intersects(first, second))
{
// The intersectionRect returns the rectangle produced
// by overlapping the two rectangles.
// This is protected by partitioning and deep collision, so it
// won't happen too often - it's okay to do a ToRect here
var intersectionRect = first.Intersection (second.ToRect());
float minDistance = float.PositiveInfinity;
float firstCenterX = first.Center.X;
float firstCenterY = first.Center.Y;
float secondCenterX = second.Left + second.Width / 2.0f;
float secondCenterY = second.Bottom + second.Width / 2.0f;
bool canMoveLeft = (second.Directions & Directions.Left) == Directions.Left && firstCenterX < secondCenterX;
bool canMoveRight = (second.Directions & Directions.Right) == Directions.Right && firstCenterX > secondCenterX;
bool canMoveDown = (second.Directions & Directions.Down) == Directions.Down && firstCenterY < secondCenterY;
bool canMoveUp = (second.Directions & Directions.Up) == Directions.Up && firstCenterY > secondCenterY;
if (canMoveLeft)
{
float candidate = first.UpperRight.X - second.Left;
if (candidate > 0)
{
minDistance = candidate;
separation.X = -minDistance;
separation.Y = 0;
}
}
if (canMoveRight)
{
float candidate = (second.Left + second.Width) - first.LowerLeft.X;
if (candidate > 0 && candidate < minDistance)
{
minDistance = candidate;
separation.X = minDistance;
separation.Y = 0;
}
}
if (canMoveUp)
{
float candidate = (second.Bottom + second.Height) - first.Origin.Y;
if (candidate > 0 && candidate < minDistance)
{
minDistance = candidate;
separation.X = 0;
separation.Y = minDistance;
}
}
if (canMoveDown)
{
float candidate = first.UpperRight.Y - second.Bottom;
if (candidate > 0 && candidate < minDistance)
{
minDistance = candidate;
separation.X = 0;
separation.Y = -minDistance;
}
}
}
return separation;
}