- Home /
2d planetary gravity
Hi there,
I'm quite far along making a 2d space shooter and have used the Unity Wiki gravity script to create some gravity-like effects on some planets that I have revolving around a sun (that orbit using simple rotateAround, as this isn't meant to be a realistic space sim!)
Now a feature of the games that I've played (SpaceWars! et al) and their "gravity wells" is the ability to perform a manoeuvre called a gravity slingshot, i.e. just zipping tangentially past the planet to gain some velocity beyond that which the ship is capable of. I've played a bit with the script and also the rigidbody parameters but either the gravity becomes too weak to have much effect, or the ship simply gets sucked towards the centre of the planet and bounces off it.
Does anyone have any idea how I might go about altering the Gravity.cs script to create this sort of effect, or a viable strategy for faking this effect (purely for gameplay purposes!)
Many thanks in advance for any thoughts, however random!
Answer by Wolfram · Oct 04, 2010 at 11:27 AM
A "real" gravity slingshot relies on a moving mass, it won't work on stationary planets. If your planetary mass is moving at velocity v, an object swinging by can gain up to 2*v speed, in the direction of the planet's movement. Maybe the Unity physics engine is realistic enough to produce a real slingshot if your planet does have a velocity.
If not, you would need some scripted trick to apply a "non-realistic" slingshot. For this you would need to think of some method for the player to enter his desired target direction, and then applying some rigidbody forces to create the slingshot effect towards that direction.
wow! offtopic, but simple and great explanation for slingshots. I used to play the Orbiter simulator and understood the underlying orbital mechanics, but I had never fully understood why slingshots are possible - until now.
Answer by runonthespot · Oct 04, 2010 at 11:36 PM
Ooh, I should add it is helpful to clamp it between 0 & 1
so
rb.AddForce( rb.velocity * Mathf.Clamp((range - (Vector3.Distance(rb.transform.position,transform.position))/range),0,1), ForceMode.Acceleration);
to prevent any weirdness!
Answer by runonthespot · Oct 04, 2010 at 11:11 PM
I settled for a scripted trick in the end :)
the "Leyland gravity whip" which is more or less the effect I'm going for effectively adds forward velocity to the object caught in the gravitational field, to do a pseudo slingshot effect. In games like Star Control, the closer you were to the planet, the more extreme the effect. My algorithm therefore added forward velocity to the object equal to the (range - distance from planet) / range, e.g. 100% speed increase if you are 0 distance from the planet, and proportionally less the further you are away.
The end result was that after this line in the UnityWiki Gravity.cs
rb.AddForce( offset / offset.sqrMagnitude * rigidbody.mass);
//I added this line
rb.AddForce( rb.velocity * (range - (Vector3.Distance(rb.transform.position,transform.position))/range), ForceMode.Acceleration);
I'm not going to mark this as the answer yet as some testing required and any thoughts welcome too.