Sunday, October 15, 2017

Particularly Wavy Updates

hey all,

I've spent the last week nailing down a black hole-like effect for Particularly Wavy. It is still extremely rough, and needs a lot of ironing out of details, bugs, and at the moment is probably very expensive in terms of processor use.


Vector3 gravityVec;
  float d;

  Vector3 accel = Vector3.zero;
  Vector3 previous = pos;
  float time = 0.05f;
  float diff = 0.1f;
  for (int i = 0; i < 150; i++)
  {

   gravityVec = center - previous;
   d = Vector3.Distance(center, previous);
   accel.x = blackHoleMass / (d * d * d * d * d);
   accel.y = blackHoleMass / (d * d * d * d * d);
   accel = Vector3.Scale(accel, gravityVec);
   vel += accel * time;
   previous += vel * time;

   if (i % 2 == 0)
   {
    tempList.Add(previous);
   }
   Debug.DrawLine(center, previous, Color.red, 1f);
   if (Vector3.Distance(previous, center) < rad + diff)
   {
    tempList.Add(previous);
    node.hitObject = go;
    hitHole = true;
    break;
   }
   else if (Vector3.Distance(previous, center) > blackHoleDistance + rad)
   {
    tempList.Add(previous);
    break;
   }
  }

I though it might be a bit fun to share some of the code that I've been working on to make this happen. To begin with, the acceleration is set to zero, the velocity is set to a normalized vector indicating the direction of travel multiplied by speed, and a starting position for the light to enter the influence of the black has been found. A time step of 0.05 seconds is used to simulate the physics.

In the loop, the acceleration is the first thing computed. This is found by subtracting the position vectors between the black hole and the light particle, then getting the distance between the previous position and the center of the black hole. Under this model, acceleration is proportional to the mass of the black hole divided by the distance to the 5th power. This acceleration is multiplied by the time step and added to the previous velocity to get the new velocity. Similarly, the new velocity is multiplied by the time step and added to the previous position to yield the new position.

If, while going through the loop, the new position is inside the radius of the black hole or is outside the influence zone of the black hole, we break out. From here, the new positions need to be applied to the line renderer's positions.

This is obviously not exactly how gravity works in the real world. For starters, it is proportional to the masses of the two objects divided by the square of the distance between them, Newton's famous relationship that Robert Hooke accused him of stealing. Of course, light particles have no mass, but their interaction with black holes is caused by the intense gravity actually curving space-time itself.

No comments:

Post a Comment