轨道力学 [英] Orbital Mechanics
问题描述
有没有人有实施轨道力学(最好在XNA)的例子吗?我目前正在使用的代码如下,但它并没有感觉正确在执行时。对象只是不断弯曲非常轻微的星球,不管我多么调整变量我不能让它进入轨道,甚至部分轨道。
shot.Position + = shot.Velocity;
的foreach(雪碧星球行星)
{
Vector2 directionToPlanet =(planet.Position - shot.Position);
directionToPlanet.Normalize();
浮动距离= Vector2.DistanceSquared(shot.Position,planet.Position);
浮动gPull =(浮点)(planet.gravityStrength *(* planet.Mass shot.Mass)/距离)+ planet.gravityField;
shot.Position + =新Vector2(directionToPlanet.X * gPull,directionToPlanet.Y * gPull);
}
修改
标Mendelt的回答正确您指出我需要更新速度,而不是位置。我还需要gPull计算改为
浮动gPull = shot.Mass * planet.Mass / distanceSqr * planet.gStr ;
在您要更新的位置的最后一行拍摄。你应该更新速度。
您可能想看看代码在这个博文的 http://blog.mendeltsiebenga.com/post/Fun-with-planets.aspx 没有XNA,但工作轨道力学。 (虽然我从来没有摆脱了屏幕闪烁)
Does anyone have an example of implementing Orbital Mechanics (preferably in XNA)? The code I am currently using is below, but it doesn't "feel right" when it executes. The object just bends ever so slightly to the planet, and no matter how much I tweak the variables I cant get it to enter an orbit, or even a partial orbit.
shot.Position += shot.Velocity;
foreach (Sprite planet in planets)
{
Vector2 directionToPlanet = (planet.Position - shot.Position);
directionToPlanet.Normalize();
float distance = Vector2.DistanceSquared(shot.Position, planet.Position);
float gPull = (float)(planet.gravityStrength * (planet.Mass * shot.Mass) / distance) + planet.gravityField;
shot.Position += new Vector2(directionToPlanet.X * gPull, directionToPlanet.Y * gPull);
}
Edit Marking Mendelt's answer correct for pointing out that I need to update the velocity, not the position. I also needed to change the calculation of gPull to
float gPull = shot.Mass * planet.Mass / distanceSqr * planet.gStr;
In the last line you're updating the position of the shot. You should be updating the velocity.
You might want to take a look at the code in this blogpost http://blog.mendeltsiebenga.com/post/Fun-with-planets.aspx No xna, but working orbital mechanics. (although i never got rid of the screen-flicker)
这篇关于轨道力学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!