C# 二维平面运动引擎
时间: 2023-07-12 07:08:17 浏览: 83
C# 二维平面运动引擎主要用于实现游戏中物体在平面上的运动、碰撞检测、物理效果等功能。以下是一个简单的二维平面运动引擎示例:
```csharp
public class Vector2D
{
public float X { get; set; }
public float Y { get; set; }
public Vector2D(float x, float y)
{
X = x;
Y = y;
}
public static Vector2D operator +(Vector2D a, Vector2D b)
{
return new Vector2D(a.X + b.X, a.Y + b.Y);
}
public static Vector2D operator -(Vector2D a, Vector2D b)
{
return new Vector2D(a.X - b.X, a.Y - b.Y);
}
public static Vector2D operator *(Vector2D a, float scalar)
{
return new Vector2D(a.X * scalar, a.Y * scalar);
}
public static Vector2D operator /(Vector2D a, float scalar)
{
return new Vector2D(a.X / scalar, a.Y / scalar);
}
public float Length()
{
return (float)Math.Sqrt(X * X + Y * Y);
}
public Vector2D Normalize()
{
float length = Length();
return new Vector2D(X / length, Y / length);
}
public float Dot(Vector2D other)
{
return X * other.X + Y * other.Y;
}
public float Cross(Vector2D other)
{
return X * other.Y - Y * other.X;
}
}
public class GameObject
{
public Vector2D Position { get; set; }
public Vector2D Velocity { get; set; }
public float Mass { get; set; }
public float Radius { get; set; }
public void Update(float deltaTime)
{
Position += Velocity * deltaTime;
}
public bool IsColliding(GameObject other)
{
float distance = (Position - other.Position).Length();
return distance < Radius + other.Radius;
}
public void ResolveCollision(GameObject other)
{
Vector2D normal = (other.Position - Position).Normalize();
Vector2D relativeVelocity = other.Velocity - Velocity;
float impulse = -2 * relativeVelocity.Dot(normal) / (Mass + other.Mass);
Velocity -= impulse * other.Mass * normal;
other.Velocity += impulse * Mass * normal;
}
}
public class PhysicsEngine
{
private List<GameObject> objects = new List<GameObject>();
public void AddObject(GameObject obj)
{
objects.Add(obj);
}
public void RemoveObject(GameObject obj)
{
objects.Remove(obj);
}
public void Update(float deltaTime)
{
for (int i = 0; i < objects.Count; i++)
{
objects[i].Update(deltaTime);
}
for (int i = 0; i < objects.Count; i++)
{
for (int j = i + 1; j < objects.Count; j++)
{
if (objects[i].IsColliding(objects[j]))
{
objects[i].ResolveCollision(objects[j]);
}
}
}
}
}
```
这个引擎包含了三个类:`Vector2D`、`GameObject` 和 `PhysicsEngine`。`Vector2D` 类表示平面上的二维向量,包含了向量加、减、乘、除、求长度、归一化、点积、叉积等基本操作。`GameObject` 类表示游戏中的物体,包含了位置、速度、质量、半径等属性,以及更新、碰撞检测、碰撞响应等方法。`PhysicsEngine` 类表示物理引擎,包含了游戏中所有物体的列表,以及更新物体位置、检测碰撞、响应碰撞等方法。
使用时,可以先创建一个物理引擎实例,然后向其中添加物体,每帧调用引擎的 `Update` 方法更新物体位置、检测碰撞、响应碰撞等操作。例如:
```csharp
PhysicsEngine engine = new PhysicsEngine();
GameObject ball1 = new GameObject();
ball1.Position = new Vector2D(100, 100);
ball1.Velocity = new Vector2D(50, 0);
ball1.Mass = 1;
ball1.Radius = 10;
GameObject ball2 = new GameObject();
ball2.Position = new Vector2D(200, 100);
ball2.Velocity = new Vector2D(-50, 0);
ball2.Mass = 1;
ball2.Radius = 10;
engine.AddObject(ball1);
engine.AddObject(ball2);
while (true)
{
engine.Update(0.01f);
// 渲染物体...
}
```
这个示例创建了两个球体,分别从左右两侧向中间运动,当两个球体碰撞时会发生弹性碰撞并反弹。
阅读全文