fn field(model: &Model, x: f32, y: f32) -> f32 {
  let g = -(model.g * 10.0.powi(4));

  let radius = |x_i: f32, y_i: f32| (x - x_i).hypot(y - y_i);

  let x_force = |body: &GravitationalBody| -> f32 {
    g * body.mass * (x - body.x) / radius(body.x, body.y).powi(3)
  };

  let y_force = |body: &GravitationalBody| -> f32 {
    g * body.mass * (y - body.y) / radius(body.x, body.y).powi(3)
  };

  let g_x = model.bodies.iter().fold(0.0, |force, b| force + x_force(b));
  let g_y = model.bodies.iter().fold(0.0, |force, b| force + y_force(b));

  // x, y corresponds to the force vector acting on the point.
  // All we want right now is the angle of that vector, so we
  // just calculate the orientation from the point
  ((y - g_y) / (x - g_x)).atan()
}
// Currently this just returns an angle,
// meaning the resulting vector field will all be unit vectors.
// I'd love to build this out to accomodate for differing velocities
// of the objects moving through the field, and the resulting
// acceleration on the object.
// from the "Restricted three body problem"
// https://en.wikipedia.org/wiki/Three-body_problem
Currently this just returns an angle,
meaning the resulting vector field will all be unit vectors.
I'd love to build this out to accomodate for differing velocities
of the objects moving through the field, and the resulting
acceleration on the object.
Currently this just returns an angle meaning the resulting vector field will all be unit vectors . I ' d love to build this out to accomodate for differing velocities of the objects moving through the field and the resulting acceleration on the object .
fn field(model: &Model, x: f32, y: f32) -> f32 {
  let g = -(model.g * 10.0.powi(4));
  let radius = |x_i: f32, y_i: f32| (x - x_i).hypot(y - y_i);
  let x_force = |body: &GravitationalBody| -> f32 {
    g * body.mass * (x - body.x) / radius(body.x, body.y).powi(3)
  };
  let y_force = |body: &GravitationalBody| -> f32 {
    g * body.mass * (y - body.y) / radius(body.x, body.y).powi(3)
  };
  let g_x = model.bodies.iter().fold(0.0, |force, b| force + x_force(b));
  let g_y = model.bodies.iter().fold(0.0, |force, b| force + y_force(b));
  // x, y corresponds to the force vector acting on the point.
  // All we want right now is the angle of that vector, so we
  // just calculate the orientation from the point
  ((y - g_y) / (x - g_x)).atan()
}
fn field ( model : & Model , x : f32 , y : f32 ) -> f32 { let g = - ( model . g * 10.0 . powi ( 4 ) ) ; let radius = | x_i : f32 , y_i : f32 | ( x - x_i ) . hypot ( y - y_i ) ; let x_force = | body : & GravitationalBody | -> f32 { g * body . mass * ( x - body . x ) / radius ( body . x , body . y ) . powi ( 3 ) } ; let y_force = | body : & GravitationalBody | -> f32 { g * body . mass * ( y - body . y ) / radius ( body . x , body . y ) . powi ( 3 ) } ; let g_x = model . bodies . iter ( ) . fold ( 0.0 , | force , b | force + x_force ( b ) ) ; let g_y = model . bodies . iter ( ) . fold ( 0.0 , | force , b | force + y_force ( b ) ) ; // x, y corresponds to the force vector acting on the point. // All we want right now is the angle of that vector, so we // just calculate the orientation from the point ( ( y - g_y ) / ( x - g_x ) ) . atan ( ) }
Currently this just returns an angle,
meaning the resulting vector field will all be unit vectors.
Currently this just returns an angle meaning the resulting vector field will all be unit vectors .
model
&Model
x
f32
y
f32