简体   繁体   English

在Unity3D中使用物理学的恒定轨道

[英]Constant orbit using physics in Unity3D

I am playing around with Unity3D and adding physics to the objects in my scene. 我正在玩Unity3D,并将物理添加到场景中的对象上。 I currently have a sphere (planet) in the center of the screen, and I have another sphere (moon) positioned outside of it that is not moving. 我目前在屏幕中央有一个球体(行星),并且在其外部有一个不动的球体(月亮)。 When I run the game I want the moon to orbit the planet by applying a force to it. 当我运行游戏时,我希望月亮通过对其施加作用力使其绕行星运行。 I have been able to get the force added by calling rigidbody.AddForce() and that will move it in the specified direction but it won't orbit. 我已经能够通过调用rigidbody.AddForce()获得加力,它将沿指定方向移动它,但它不会绕轨道运动。 I'm not sure how to add force to it so that it will continuously orbit the sphere at a constant velocity. 我不确定如何对其施加力,以使其以恒定速度连续绕球旋转。

I've tried some examples using a ConfigurableJoint and it orbits, but it starts out bouncing a little and then starts the orbit. 我已经尝试过使用ConfigurableJoint一些示例,并且它绕轨道运行,但是它开始会稍微弹起,然后开始轨道。 My goal is to have a bunch of orbiting moons orbiting at their own speed that are able to bounce off eachother but not lose their velocity. 我的目标是让一束绕轨道运行的卫星以自己的速度运行,能够彼此反弹而不会失去速度。

Any ideas? 有任何想法吗?

For the moon to orbit you would need to give the moon an initial velocity. 为了使月亮绕轨道运行,您需要给月亮一个初始速度。 Then have it accelerate towards the planet, that is a constant force. 然后让它向行星加速,这是恒力。

在此处输入图片说明

在此处输入图片说明

gameObject.rigidbody.AddForce(1, 0, 0);
gameObject.constantForce.relativeForce = Vector3(0, 1, 0);

Generally speaking you will fail, eventually, because rounding errors in your integration method will slowly throw you out of orbit. 一般来说,您最终将失败,因为积分方法中的舍入误差将使您缓慢地脱离轨道。 You can get very close in the ways suggested, but you could consider doing something more like the Kerbal Space Program, which seems to precalculate the orbit as an ellipse and then follow that ellipse until it has a reason to believe it should stop, rather than strictly "simulating" the orbit ... 您可以按照建议的方式非常接近,但您可以考虑做更多类似“ Kerbal太空计划”的工作,该程序似乎将轨道计算为椭圆,然后跟随该椭圆直到有理由相信它应该停止而不是停止严格地“模拟”轨道...

If a collision occurs, allow normal physics to resolve the collision, and then recalculate your new orbit based on the result and start following that. 如果发生碰撞,请让普通物理学解决该碰撞,然后根据结果重新计算您的新轨道并开始跟踪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM