Пример #1
0
        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
0
		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;
		}