Example #1
0
        //---------------------------------------------------------------------------------------------------------
        // GetRandomPosition - Old method - don't use
        //---------------------------------------------------------------------------------------------------------
        // Parameters: CCsprite - size of the sprite to position
        //
        // Returns: CCPoint - random point within the display field
        //---------------------------------------------------------------------------------------------------------
//		CCPoint GetRandomPosition (CCSize spriteSize)
//		{
//			double rndX = CCRandom.NextDouble ();
//			double rndY = CCRandom.NextDouble ();
//			double randomX = (rndX > 0)
//				? rndX * VisibleBoundsWorldspace.Size.Width - spriteSize.Width / 2
//				: spriteSize.Width / 2;
//			if (randomX < (spriteSize.Width / 2))
//				randomX += spriteSize.Width;
//
//			double randomY = (rndY > 0)
//				? rndY * VisibleBoundsWorldspace.Size.Height - spriteSize.Height / 2
//				: spriteSize.Height / 2;
//			if (randomY < (spriteSize.Height / 2))
//				randomY += spriteSize.Height;
//			return new CCPoint ((float)randomX, (float)randomY);
//		}

        //---------------------------------------------------------------------------------------------------------
        // GetSeparatingVector - Not being used
        //---------------------------------------------------------------------------------------------------------
        // Parameters:	first - CCRect of the first object being compared
        //				second - CCRect of the second object being compared
        //
        // Returns:		CCVector2 - vector to direction to move second object so it won't collide with first
        //---------------------------------------------------------------------------------------------------------
        // Used to compare overlaping rectangles of two objects to determine if they collide or not.  If they do
        // return a vector to move the object so it won't overlap
        //---------------------------------------------------------------------------------------------------------
        static CCVector2 GetSeparatingVector(CCRect first, CCRect second)
        {
            CCVector2 separation = CCVector2.Zero;

            if (first.IntersectsRect(second))
            {
                var  intersecionRect      = first.Intersection(second);
                bool separateHorizontally = intersecionRect.Size.Width < intersecionRect.Size.Height;

                if (separateHorizontally)
                {
                    separation.X = intersecionRect.Size.Width;
                    if (first.Center.X < second.Center.X)
                    {
                        separation.X *= -1;
                    }
                    separation.Y = 0;
                }
                else
                {
                    separation.X = 0;
                    separation.Y = intersecionRect.Size.Height;
                    if (first.Center.Y < second.Center.Y)
                    {
                        separation.Y *= -1;
                    }
                }
            }
            return(separation);
        }
Example #2
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;
        }
Example #3
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);
        }
Example #4
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;
		}