Make Rigidbody2d spiral around point

Hi!

I would like to have a Rigidbody2D orbit around a point in space. I know there are tons of posts about orbital gravitation, but that’s not quite what i’m after. I’ve tried using the point effector and also applying gravitational force to the rigidbody towards a point, but that makes for a very uncontrollable scenario which is not what i’m after. Here’s a simple illustration to explain what I would like to create:

107254-explanation.jpg

So the arrow is the Rigidbody2D.

When it comes within a certain radius of the point in the middle, I would like it to have forces applied so that it starts going around the point in a shrinking spiral, going faster and faster until it either collides with the point, or (by some user interaction) it gets flung out of the “gravitational” field.

I of course understand that no one will write the code for me. I’m just at a bit of a loss as how to approach this. As I said, using traditional inverse-squared gravitational pull is way too uncontrollable for this, as I always want the Rigidbody to start going in a spiral no matter its speed or mass.

I can identify two things that need to happen:

  1. The Rigidbody2D (once in range) needs to be pulled closer and closer to the point.
  2. The Rigidbody2D needs to orbit the point in a circle, no matter its speed or mass.

Any help on where to start in terms of math would be greatly appreciated.

Thank you!

I’m pretty new to this, but here’s my thought process:

  1. Attract towards the object with universal law of attraction: BUT, when within a certain distance, make use of Kinematic feature. You can save the velocity.magnitude of your rigidbody and use it for your speed in the next step as well.

  2. You can use some math to build a circular path around the object. You may need to do some googling on this, because I can’t test right now, but:

    float timeCounter; // because it’s a time-based circuit

    float speed = 5f // minimum speed, override with velocity.magnitude if you want
    
    float radius = 15f // start radius, will be changed dynamically
    
    void Update() {
    timeCounter += Time.deltaTime * speed;
    
    float x = Mathf.Cos(timeCounter) * radius;
    float y = Mathf.Sin(timeCounter) * radius;
    
    transform.position = new Vector2(x,y);
    }
    
  3. You can then have the radius lerp downwards towards 0. Then, as the player does your input, or whatever you said was the plan, you can just take them off the track and add some escape force.

You may want to use a similar approach to what i’ve posted over here. Though in this specific case it’s rather difficult to actually change the radius as the mechanics that was requested over there needed actual circular motion.

In general a spiral behaviour can be easily archieved by waiting for the object to pass the tangential point and then just use the normal gravitational law. However you should adjust the velocity to match the correct tangential velocity for a proper circular orbit. This will make your object orbit in a more or less perfect circle. You automatically get a spiral when you add some artificial “drag” (just slow it down slightly over time). This will make the orbit to spiral inwards.

Though if you’re dealing with very high velocities and relatively small objects this might not be a good idea. For really fast rotation speeds letting the object rotate around it’s own center of mass and shifting the center of mass to the desired center is the most stable approach. However to get a spiral you would need to reassign and slightly displace the center of mass each frame. So then keeping the COM at the objects center it would orbit correctly. However is you place the COM slightly “behind” the center (offset into the negative tangential direction) the object would get closer to the center

However many details of your situation are missing:

  • Where does the object come from? Can it approach the “planet” / point from any angle? If so it could simple move directly towards the center and crash without any spiral / orbit.
  • Since you only want the effect when the object comes into a certain distance it’s difficult to get a smooth transition.
  • Should the simulation “look” physically correct or is this irrelevant?
  • Do you actually need collisions with other objects? The condition if the object crashes onto the planet can be easily done manually without actual physics collisions. If you don’t need any other collisions something like @theterrificjd posted should work as well as long as you calculate the right start phase and angular velocity