# 2d planetary gravity

 0 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! more ▼ asked Oct 04 '10 at 10:23 AM runonthespot 514 ● 19 ● 21 ● 36 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

3 answers: sort voted first
 0 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. more ▼ answered Oct 04 '10 at 11:27 AM Wolfram 9k ● 8 ● 20 ● 52 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. Oct 04 '10 at 11:43 PM Edy add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 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! more ▼ answered Oct 04 '10 at 11:36 PM runonthespot 514 ● 19 ● 21 ● 36 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 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. more ▼ answered Oct 04 '10 at 11:11 PM runonthespot 514 ● 19 ● 21 ● 36 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
Your answer

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1035
x460
x7

asked: Oct 04 '10 at 10:23 AM

Seen: 2237 times

Last Updated: Oct 04 '10 at 10:23 AM