英文:
Struggling to convert cartesian elements into orbital elements in Unity2D + keplerian solution to the two-body problem
问题
我正在制作一个在Unity 2D中模拟行星轨道的游戏,使用C#编写。我之前使用了牛顿引力定律,但很快发现浮点数的舍入误差可能在数百或数千轨道后导致轨道大幅偏离。对我来说,这是一个大问题,因为我需要模拟在每次运行时尽可能一致。与其试图大幅复杂化事情,尝试用双精度替换浮点数来覆盖物理系统,不如研究一下开普勒轨道和二体问题。
开普勒轨道的实现要比牛顿的复杂得多,我很难找到关于如何做到以下几点的资源:
- a) 准确模拟扩展时间段内的2D轨道,使用开普勒元素。
- b) 将刚体的当前位置和速度(笛卡尔元素)转换为开普勒轨道元素,以便我可以根据外部因素(如给刚体施加力、碰撞或另一个引力体吸引刚体,如月球或其他物体)对轨道模拟进行更改。
我不太擅长直观理解长而复杂的方程,所以如果您能在答案中使用有意义的变量名,并尽量解释代码的每个代码块是做什么的,我将不胜感激。我想要能够理解代码,而不只是复制粘贴解决方案,因为未来可能需要回来进行调整。
英文:
Im building a game where i simulate planetary orbits in Unity 2D C#. I previously used newtons laws of gravitation but quickly discovered that rounding errors in floats can lead to massive orbital drift after hundreds or thousands of orbits. This is a big problem for me as I need the simulation to be as consistent as possible every time. Rather than massively complicate things by trying to overwrite the physics system to use doubles instead of floats, I did some research into Keplerian Orbits and the 2 body problem.
Keplerian orbits are far more complex to implement than newtonian and I am struggling to find resources on how to:
- a) accurately simulate a 2D orbit for extended periods of time using Keplerian elements.
- b) convert a rigidbodys current position and velocity (Cartesinal elements) into Keplerian orbital elements so I can make changes to the orbit simulation based on external factors such as adding force to the rigidbody, collisions or another gravitational body attracting the rigidbody (like a moon or something).
Im not very good at understanding long and complex equations intuitively so id appreciate it if you could use meaningful variable names in the answer and try and explain what each block of code does. I want to be able to understand the code rather than just copy and paste a solution as its likely im going to have to come back and make tweaks in the future.
答案1
得分: 1
这里涉及到很多内容,恐怕我没有足够的关于您具体用例的信息,无法给出明确的答案。
我将特别解答您问题(a)中关于“长时间模拟”的部分。
听起来您对开普勒轨道和二体问题的研究都是为了创建一个不会随时间累积误差的轨道模拟。将所有坐标更改为开普勒元素不会为您解决这个问题,使用双精度浮点数而不是单精度浮点数也不会。
除非您模拟中的每个物体只会受到单一物体的引力影响(例如,仅考虑太阳对行星的引力,忽略它们对太阳和彼此的影响),否则这不能达到您的目标。看起来这种约束对您来说可能不适用。如果您决定可以接受这种约束,那么您可以使用开普勒轨道的精确解。
如果没有这种约束,那么您基本上只能使用数值解。数值方法会有误差,这是不可避免的事实。实时物理引擎(如Unity中的PhysX)在计算物理领域通常是高度改进的欧拉方法模拟。
有几种方法可以减小欧拉模拟固有误差。最简单的方法是通过增加Unity的物理帧速率/固定时间步长来减小模拟步长。更复杂的方法,如龙格-库塔方法,可以通过在每帧向刚体施加的引力中添加校正项来实现。
**总结:**首先,决定您是否可以接受必须遵守的约束,以便使用精确解。如果不能接受这些约束,那么请开始研究数值模拟方法中的误差校正。
英文:
There's a lot going on here, and I'm afraid I don't have enough information about your specific use case to give you a clear-cut answer.
I will specifically address the "for extended periods of time" part of your question (a).
It sounds like all your investigation into Keplerian orbits and the 2-body problem is for the purpose of creating an orbit simulation that does not accrue error over time. Changing all your coordinates to Kepler elements won't solve this problem for you, nor would using doubles instead of floats.
Unless each body in your simulation will only ever be affected by the gravitational force of a single other body (e.g., modeling the solar system exclusively considering the Sun's gravitational force on the planets, and ignoring their forces on the Sun and each other), this won't get you what you're looking for. It sounds like that kind of constraint would be a no-go for you. If you decide that you're OK with this constraint, then you can use an exact analytical solution of Keplerian orbits.
Without that constraint, you're pretty much stuck using numerical solutions. Numerical methods have error. That's an inescapable fact. Real-time physics engines (such as PhysX in Unity) are, in computational physics lingo, mostly glorified Euler method simulations.
There are several ways to go about mitigating the error inherent to Euler simulations. The easiest is to decrease the simulation step size by increasing Unity's physics framerate/fixed timestep. More complex approaches like the Runge-Kutta methods can be implemented by adding a corrective term to the gravitational force you apply to a Rigidbody each frame.
tl;dr: First, decide whether you're OK with the constraints you have to abide by to let you use an exact solution. If you're not, then start looking at error correction in numerical simulation methods.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论