⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 collisiondetection.cs

📁 这是网上下载的很好的串口发短信的程序! 是用VC++来做的
💻 CS
字号:
using System;
using System.Drawing;

namespace DDGameHelper
{
	/// <summary>
	/// Describes the relative horizontal
	/// position of one object to another.
	/// </summary>
	public enum RelXPos
	{
		Left    = 0,
		Mid     = 1,
		Right   = 2,
		Unknown = 3
	}

	/// <summary>
	/// Describes the relative vertical
	/// position of one object to another.
	/// </summary>
	public enum RelYPos
	{
		Top     = 0,
		Mid     = 1,
		Bottom  = 2,
		Unknown = 3
	}

	/// <summary>
	/// Checks for collisions between to bitmap objects.
	/// </summary>
	public class CollisionDetection
	{
		/// <summary>
		/// Checks wether two bitmap objects collide or not.
		/// </summary>
		/// <param name="FirstObject">
		/// The first bitmap object.
		/// </param>
		/// <param name="SecondObject">
		/// The second bitmap object.
		/// </param>
		/// <param name="TransparencyColor">
		/// The transparency color.
		/// Specify Color.empty to disable advanced
		/// collision detection.
		/// </param>
		/// <returns>
		/// True if collision is detected.
		/// </returns>
		public static bool DoIntersect(GameObject FirstObject, 
			GameObject SecondObject,
			Color TransparencyColor,
			ref RelXPos FirstToSecondX,
			ref RelYPos FirstToSecondY)
		{
			// Values for coordinate transformation
			// and intersection rectangle
			int addX1, addY1, addX2, addY2;			
			Rectangle intersection;

			BmpRectCombo FirstObject_BmpRectCombo = (BmpRectCombo)GameObject.BmpRectComboCollection[FirstObject.IndexOfBmpRectComboCollection()];
			Rectangle FirstObject_Rectangle = FirstObject_BmpRectCombo.RectangleArray.Rectangles[FirstObject.FrameCurrent];
			Rectangle FirstObject_Position = new Rectangle((int)FirstObject.XCurrent, (int)FirstObject.YCurrent, FirstObject_Rectangle.Width,FirstObject_Rectangle.Height);

			BmpRectCombo SecondObject_BmpRectCombo = (BmpRectCombo)GameObject.BmpRectComboCollection[SecondObject.IndexOfBmpRectComboCollection()];
			Rectangle SecondObject_Rectangle = SecondObject_BmpRectCombo.RectangleArray.Rectangles[SecondObject.FrameCurrent];
			Rectangle SecondObject_Position = new Rectangle((int)SecondObject.XCurrent, (int)SecondObject.YCurrent, SecondObject_Rectangle.Width,SecondObject_Rectangle.Height);


			// Get intersection rectangle
			intersection = Rectangle.Intersect(FirstObject_Position,
				SecondObject_Position);

			// Check dimensions of intersection rectangle
			if (intersection == Rectangle.Empty
				|| intersection.Width == 0
				|| intersection.Height == 0) return false;
			
			// Simple collision detection? Everything's done here.
			if (TransparencyColor == Color.Empty)
			{
				return true;
			}
			else
			{
				// Advanced collision detection

				// Calculate values for transformations
				if (FirstObject_Position.X > SecondObject_Position.X)
				{
					addX1 = 0;
				}
				else
				{
					addX1 = SecondObject_Position.X
						- FirstObject_Position.X;
				}

				if (FirstObject.YCurrent > SecondObject.YCurrent)
				{
					addY1 = 0;
				}
				else
				{
					addY1 = SecondObject_Position.Y
						- FirstObject_Position.Y;
				}

				if (addX1 == 0)
				{
					addX2 = FirstObject_Position.X
						- SecondObject_Position.X;
				}
				else
				{
					addX2 = 0;
				}

				if (addY1 == 0)
				{
					addY2 = FirstObject_Position.Y 
						- SecondObject_Position.Y;
				}
				else
				{
					addY2 = 0;
				}

				Bitmap FirstObject_Bitmap = ((BmpRectCombo)GameObject.BmpRectComboCollection[FirstObject.IndexOfBmpRectComboCollection()]).BitmapObject.BitmapSource;
				Bitmap SecondObject_Bitmap = ((BmpRectCombo)GameObject.BmpRectComboCollection[SecondObject.IndexOfBmpRectComboCollection()]).BitmapObject.BitmapSource;

				addX1 += FirstObject_Rectangle.Left;
				addY1 += FirstObject_Rectangle.Top;
				addX2 += SecondObject_Rectangle.Left;
				addY2 += SecondObject_Rectangle.Top;

				Color c1,c2;
				// Iterate over all pixels of
				// intersection rectangle
				for (int i = 0; i < intersection.Width; i++)
				{
					for(int j = 0; j < intersection.Height; j++)
					{
						// Check color of current pixel
						// for first and second object
						c1=FirstObject_Bitmap.GetPixel(i + addX1 , j + addY1);
						c2=SecondObject_Bitmap.GetPixel(i + addX2 , j + addY2);
						if (c1 != TransparencyColor && c2 != TransparencyColor)
						{
							// Check collision direction
							CheckDirection(FirstObject,
								SecondObject,
								ref FirstToSecondX, 
								ref FirstToSecondY);

							// Collision found
							return true;
						}
					}
				}

				// If we made it here, no "real" collision happened
				return false;
			}
		}

		private static void CheckDirection(GameObject FirstObject,
			GameObject SecondObject,
			ref RelXPos FirstToSecondX,
			ref RelYPos FirstToSecondY)
		{
			// Determine horizontal positions
			if (FirstObject.XCurrent < SecondObject.XCurrent
				&& FirstObject.XCurrent + ((BmpRectCombo)GameObject.BmpRectComboCollection[FirstObject.IndexOfBmpRectComboCollection()]).RectangleArray.Rectangles[0].Width
				< SecondObject.XCurrent + ((BmpRectCombo)GameObject.BmpRectComboCollection[SecondObject.IndexOfBmpRectComboCollection()]).RectangleArray.Rectangles[0].Width)
			{
				FirstToSecondX = RelXPos.Left;
			}
			else if ((FirstObject.XCurrent >= SecondObject.XCurrent
				&& FirstObject.XCurrent + ((BmpRectCombo)GameObject.BmpRectComboCollection[FirstObject.IndexOfBmpRectComboCollection()]).RectangleArray.Rectangles[0].Width 
				<= SecondObject.XCurrent + ((BmpRectCombo)GameObject.BmpRectComboCollection[SecondObject.IndexOfBmpRectComboCollection()]).RectangleArray.Rectangles[0].Width)
				|| (FirstObject.XCurrent <= SecondObject.XCurrent
				&& FirstObject.XCurrent + ((BmpRectCombo)GameObject.BmpRectComboCollection[FirstObject.IndexOfBmpRectComboCollection()]).RectangleArray.Rectangles[0].Width 
				>= SecondObject.XCurrent + ((BmpRectCombo)GameObject.BmpRectComboCollection[SecondObject.IndexOfBmpRectComboCollection()]).RectangleArray.Rectangles[0].Width))
			{
				FirstToSecondX = RelXPos.Mid;
			}
			else if (FirstObject.XCurrent > SecondObject.XCurrent
				&& FirstObject.XCurrent + ((BmpRectCombo)GameObject.BmpRectComboCollection[FirstObject.IndexOfBmpRectComboCollection()]).RectangleArray.Rectangles[0].Width 
				> SecondObject.XCurrent + ((BmpRectCombo)GameObject.BmpRectComboCollection[SecondObject.IndexOfBmpRectComboCollection()]).RectangleArray.Rectangles[0].Width)
			{
				FirstToSecondX = RelXPos.Right;
			}
			else
			{
				FirstToSecondX = RelXPos.Unknown;
			}

			// determine vertical positions
			if (FirstObject.YCurrent < SecondObject.YCurrent 
				&& FirstObject.YCurrent + ((BmpRectCombo)GameObject.BmpRectComboCollection[FirstObject.IndexOfBmpRectComboCollection()]).RectangleArray.Rectangles[0].Height 
				< SecondObject.YCurrent + ((BmpRectCombo)GameObject.BmpRectComboCollection[SecondObject.IndexOfBmpRectComboCollection()]).RectangleArray.Rectangles[0].Height)
			{
				FirstToSecondY = RelYPos.Top;
			}
			else if ((FirstObject.YCurrent >= SecondObject.YCurrent
				&& FirstObject.YCurrent + ((BmpRectCombo)GameObject.BmpRectComboCollection[FirstObject.IndexOfBmpRectComboCollection()]).RectangleArray.Rectangles[0].Height 
				<= SecondObject.YCurrent + ((BmpRectCombo)GameObject.BmpRectComboCollection[SecondObject.IndexOfBmpRectComboCollection()]).RectangleArray.Rectangles[0].Height)
				|| (FirstObject.YCurrent <= SecondObject.YCurrent 
				&& FirstObject.YCurrent + ((BmpRectCombo)GameObject.BmpRectComboCollection[FirstObject.IndexOfBmpRectComboCollection()]).RectangleArray.Rectangles[0].Height 
				>= SecondObject.YCurrent + ((BmpRectCombo)GameObject.BmpRectComboCollection[SecondObject.IndexOfBmpRectComboCollection()]).RectangleArray.Rectangles[0].Height))
			{
				FirstToSecondY = RelYPos.Mid;
			}
			else if (FirstObject.YCurrent > SecondObject.YCurrent
				&& FirstObject.YCurrent + ((BmpRectCombo)GameObject.BmpRectComboCollection[FirstObject.IndexOfBmpRectComboCollection()]).RectangleArray.Rectangles[0].Height 
				> SecondObject.YCurrent + ((BmpRectCombo)GameObject.BmpRectComboCollection[SecondObject.IndexOfBmpRectComboCollection()]).RectangleArray.Rectangles[0].Height)
			{
				FirstToSecondY = RelYPos.Bottom;
			}					
			else
			{
				FirstToSecondY = RelYPos.Unknown;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -