我有一个物理模拟,它允许你放置区域约束,这样里面的物体就不会离开那个区域。但是,如果一个原子越过区域约束的“墙”之一,它就会破坏物理模拟。为什么这样做?更新方法:
if (!atom.IsStatic)
{
Vector2 force = Vector2.Zero;
bool x = false, y = false;
if (atom.Position.X - atom.Radius < _min.X)
{
force = new Vector2(-(_min.X - atom.Position.X), 0);
if (atom.Velocity.X < 0)
x = true;
}
if (atom.Position.X + atom.Radius > _max.X)
{
force = new Vector2(atom.Position.X - _max.X, 0);
if (atom.Velocity.X > 0)
x = true;
}
if (atom.Position.Y - atom.Radius < _min.Y)
{
force = new Vector2(0, -(_min.Y - atom.Position.Y));
if (atom.Velocity.Y < 0)
y = true;
}
if (atom.Position.Y + atom.Radius > _max.Y)
{
force = new Vector2(0, atom.Position.Y - _max.Y);
if (atom.Velocity.Y > 0)
y = true;
}
atom.ReverseVelocityDirection(x, y);
if (!atom.IsStatic)
{
atom.Position += force;
}
}