Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Vortex/Black Hole Physics

Discussion in 'Physics' started by KamiKaze425, Feb 12, 2015.

  1. KamiKaze425

    KamiKaze425

    Joined:
    Nov 20, 2012
    Posts:
    207
    So before I start, I did try googling this a bit. And there's really nothing that gets the effect right. Unless there's an extra step I'm missing.
    This is meant as a 2D game, but I'm using 3D physics with an orthographic camera.
    Basically, I have this vortex on the screen. Just a sphere for now. When objects get near it, I want them to be pulled towards the center. The strength of the pulling force should be strongest near the center of the vortex and slowly decrease as you extend out.
    If an object is moving fast enough, it should be able to avoid being sucked into the vortex (depending on how close it is to the center), but may have its path altered by the force of the pull.
    It is possible to shoot another object through the center of the vortex with enough speed to potentially knock out the objects that are trapped in its center.

    my code on my vortex is:
    Code (CSharp):
    1. void FixedUpdate()
    2.     {
    3.         Vector3 explosionPos = thisTransform.position;
    4.         Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
    5.         foreach(Collider hit in colliders)
    6.         {
    7.             if(hit && hit.transform != thisTransform && hit.rigidbody)
    8.             {
    9.                 Vector3 difference = hit.transform.position - thisTransform.position;
    10.                 hit.rigidbody.AddForce(difference.normalized * power, ForceMode.Force);
    11.             }
    12.         }
    13.     }
    My objects are spheres with sphere colliders and rigidbodies. They are initially launched by setting rigidbody.velocity.
    My vortex is a sphere with a sphere collider set as a trigger (to avoid actual objects colliding with the vortex itself)
    This almost works... but it causes more of an orbit type of effect, that eventually launches the objects out on its own. I'm assuming the amount of force that gets added ends up pushing the object passed the center, causing the opposite force to apply on the next check, making it sort of ping pong back and forth.

    Any ideas?
     
    Last edited: Feb 13, 2015
  2. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
  3. KamiKaze425

    KamiKaze425

    Joined:
    Nov 20, 2012
    Posts:
    207
    I think my problem is still that AddForce is pushing the object passed the target's center.
    I added an extra trigger to the center of the vortex that will stop velocity and disable gravity on enter. Then re-enable gravity on exit. That seems to get it working for the most part
     
  4. Partel-Lang

    Partel-Lang

    Joined:
    Jan 2, 2013
    Posts:
    2,566
    This is how you calculate gravitational acceleration:

    Code (CSharp):
    1. /// <summary>
    2.     /// The Newton's constant, an empirical physical constant involved in the calculation(s) of gravitational force between two bodies.
    3.     /// </summary>
    4.     private const float gravitationalConstant = 6.672e-11f;
    5.  
    6.     /// <summary>
    7.     /// Calculates gravitational acceleration for a Rigidbody under the influence of a large "mass" point at a "position".
    8.     /// Use this in each FixedUpdate(): rigidbody.velocity += GAcceleration(planetPosition, planetMass, rigidbody);
    9.     /// </summary>
    10.     /// <returns>The acceleration.</returns>
    11.     /// <param name="position">Position of the planet's center of mass.</param>
    12.     /// <param name="mass">Mass of the planet (kg). Use large values. </param>
    13.     /// <param name="r">The Rigidbody to accelerate.</param>
    14.     public static Vector3 GAcceleration(Vector3 position, float mass, Rigidbody r) {
    15.         Vector3 direction = position - r.position;
    16.  
    17.         float gravityForce = gravitationalConstant * ((mass * r.mass) / direction.sqrMagnitude);
    18.         gravityForce /= r.mass;
    19.  
    20.         return direction.normalized * gravityForce * Time.fixedDeltaTime;
    21.     }
    Note that mass is in kg so it needs to be a very large value.

    That is standard gravitation that will make your spaceship orbit around the black hole. If you need it to be sucked in like a vortex, all you need to do is add a bit of drag to the Rigidbody. Slowing down will gradually lower it's orbit which will simultaneously speed it up the closer it gets to the centre like in a real vortex.

    Cheers,
    Pärtel
     
    luwop likes this.