我正在尝试在我的XNA 2D游戏中通过重力来为一个二维精灵添加动画效果。我已经开发了一个非常基本的类来实现这个仿真效果。以下是我的示例代码。
namespace Capture
{
class PhysX
{
static Vector2 g = new Vector2(0.0f, 10.0f);
public Vector2 pos, vel, acc, accumForce;
public float mass;
public bool USE_GRAVITY = true;
/* Constructor */
public void Engage(ref GameTime gameTime, uint maxX, uint maxY)
{
Vector2 F = Vector2.Zero;
if (USE_GRAVITY)
{
F = accumForce + mass * g;
}
acc = F / mass;//This is the Net acceleration, Using Newtons 2nd Law
vel += acc * gameTime.TotalGameTime.Seconds;// v = u + a*t
pos += vel * gameTime.TotalGameTime.Seconds;// s = u*t + 0.5*a*t*t,
pos.X %= maxX;
pos.Y %= maxY;
}
public void ApplyForce(ref Vector2 f)
{
accumForce += f;
}
}
}
我在Game#Update(GameTime gt)
方法中调用了PhysX#Engage()
方法。问题在于动画不够流畅。这是因为位置坐标很快变得非常大。为了克服这个问题,我尝试使用取模运算,如代码中所示的Viewport.Width,Viewport.Height
,但是位置坐标仍然不够平滑。我该怎么办?如何使动画更加流畅?请帮我解决问题。