问题描述
我想实现在XNA游戏的弹道导弹,并试图找出最好的方法,使弹丸遵循重力曲线。
I would like to implement a ballistic trajectory in an XNA game and was trying to figure out the best way to make the projectile follow a gravitational curve.
最好的事情我能想到的是只计算在曲线类曲线第一和商店。然后到沿曲线移动精灵。
Best thing I can think of is to just calculate the curve first and store in a "Curve" class. Then get the sprite to move along that curve.
但我真的不能弄清楚如何实际移动沿曲线精灵。
But I can't really figure out how to actually move the sprite along that curve.
我将如何做到这一点,或者是有只是一个更好的办法?
How would I do this, or is there simply a better way?
推荐答案
基本上你想用运动(维基百科文章)你高一级学校的物理方程。
Basically you want to use your high school level physics equations of motion (Wikipedia article).
有关抛运动,这是最重要的公式:
For projectile motion, this is the important equation:
s = s₀ + v₀t + ½at²
的(排量等于:初始位移,加上初始速度乘以时间,加上半加速乘以时间的平方) 的
假设你有一个弹丸二维移动。你基本上运行该公式为每个维度。在X方向上,你将有一个初始位置和一些初始速度,但没有加速度
Say you have a projectile moving in 2D. You basically run this equation for each dimension. In the X direction you will have an initial position and some initial velocity but no acceleration.
在Y方向你将有一个初始位置,初始速度和加速度向下由于重力。
In the Y direction you will have an initial position, initial velocity, and the acceleration downwards due to gravity.
所有你需要做的就是保持跟踪,因为你的弹丸被解雇的时候,并绘制你的精灵在计算出的位置。
All you have to do is keep track of the time since your projectile was fired, and draw your sprite at the calculated position.
下面是一些粗糙的XNA代码 - 你可以看到我可以一次计算两个轴:
Here is some rough XNA code - as you can see I can just calculate both axes at once:
Vector2 initialPosition = Vector2.Zero;
Vector2 initialVelocity = new Vector2(10, 10); // Choose values that work for you
Vector2 acceleration = new Vector2(0, -9.8f);
float time = 0;
Vector2 position = Vector2.Zero; // Use this when drawing your sprite
public override void Update(GameTime gameTime)
{
time += (float)gameTime.ElapsedGameTime.TotalSeconds;
position = initialPosition + initialVelocity * time
+ 0.5f * acceleration * time * time;
}
随着一点点代数,您可以使用运动做事情的那些相同的公式如计算速度怎样启动您在弹丸击中特定点。
With a little algebra, you can use those same equations of motion to do things like calculating what velocity to launch your projectile at to hit a particular point.
这篇关于动画雪碧沿XNA的曲线路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!