1
votes

I am playing around with Unity3D and adding physics to the objects in my scene. 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. 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. 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?

2
Realistically you would continuously apply a force on the moon towards the planet, with the moon's initial velocity roughly perpendicular to that force. No guarantee the orbits will be stable, though. Especially when moons start bouncing off of each other. Joints may be a more practical approach. Not sure what the "bouncing" is about.JosephHirn
+1 Like Ginosaji said my 1st idea is to use the known formulas from the real world with Newtons gravitation law as Centripetal force. Set Bounciness = 1and hope it's not too jerkyKay
I know its not using the physics you wanted but would Transform.RotateAround work? docs.unity3d.com/Documentation/ScriptReference/…Shredder2500
Hmm I will probably continue to go down the road of Joints for now and see what I can learn about that... @Shredder2500 I was using Transform.RotateAround() orginally but then I decided I wanted physics and realistic collision responses.DRiFTy

2 Answers

0
votes

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.

enter image description here

enter image description here

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

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 ...

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.