Applies gravitational force between each pair of objects proportional to the inverse square of distance between them. The equation for the force is

F = G m1 m2 / r^2

for two objects with masses m1, m2 separated by distance r, with strength of gravity G.

The set of objects can be specified with the addBody method, or the Gravity2Law can observe a SimList and automatically add all bodies that have mass to the set of objects.

Parameters Created

Hierarchy (view full)




  • Adds any MassObjects with finite mass among the given list of SimObjects.


    • bodies: SimObject[]

      set of SimObjects to possibly add

    Returns void

  • Adds the SimObject to list of objects that Gravity2Law applies forces to, but only if it has positive finite mass. *


    Returns void

  • Connect to the given SimList, so that the force applies to all objects in the SimList. Also adds all existing bodies on that SimList. *


    • simList: SimList

      the SimList to connect with

    Returns void

  • Ensures this ForceLaw is not connected to anything and so can be garbage collected. For example, if this ForceLaw is an Observer, this will stop observing its Subject.

    Returns void

  • Returns the strength of gravity, the G factor in F = G m1 m2 / r^2.

    Returns number

    the strength of gravity

  • Returns potential energy from gravity of the collection of MassObjects. Finds potential energy for each pair of objects and adds all these. Zero potential energy for each pair of objects is when they are at the minimum possible distance, as given by their MassObject.getMinHeight methods. If both objects have zero minimum radius, then this method will throw an exception.

    Background on the calculations:

    what is the potential energy of gravity?
    suppose 2 point mass objects m1, m2 are separated by r. Then they move so that they
    are separated by 0.
    Assume that m2 is fixed in space, so only m1 moves.  The force on m1 is
    G m1 m2 / r^2
    So the work done is the integral of force times distance:
    integral (G m1 m2 / x^2 ) dx  from r to 0
    - G m1 m2 / x  from r to 0  = G m1 m2 / r
    Check that this corresponds to potential energy of gravity near earths surface:
    Suppose m1 moves from r2, slightly closer to r1.
    Then the change in potential energy is (sign is probably off:)
    G m1 m2 (1/r2 - 1/r1) = G m1 m2 (r1 - r2 / r1 r2)
    At earths surface r1 and r2 are practically the same number:
    Let r2 = R + e2 and r1 = R + e1,   where e2 << R and e1 << R.
    Then change in PE = G m1 m2 (e2 - e1 / R^2)
    This matches the rule: PE = m g y, so that change in PE is m g (y2 - y1)
    where g = G m2 / R^2 and m2 = mass of the earth, R = radius of the earth.
    Actually it looks like this is correct:
    PE = - G m1 m2 / r

    At infinite separation, the PE is zero, and the PE becomes a bigger negative number as the objects approach.

    For each pair, the zero energy is reached when they are at their closest possible separation. Take whatever that number for PE is (a negative number) and subtract this from the PE to get a positive PE that goes to zero as the objects approach each other. As long as one of the objects has a non-zero minimum radius, then we can get a non-infinite number for the minimum PE.

    Returns number

    the potential energy due to this ForceLaw

  • Notifies this Observer that a change has occurred in the Subject.


    • event: SubjectEvent

      contains information about what has changed in the Subject: typically either a one-time GenericEvent, or a change to the value of a Parameter

    Returns void

  • Specifies the set of MassObjects to apply forces on.


    • bodies: MassObject[]

      the set of MassObjects to apply forces on.

    Returns void

  • Sets whether this Subject will broadcast events, typically used to temporarily disable broadcasting. Intended to be used in situations where a subclass overrides a method that broadcasts an event. This allows the subclass to prevent the superclass broadcasting that event, so that the subclass can broadcast the event when the method is completed.


    • value: boolean

      whether this Subject should broadcast events

    Returns boolean

    the previous value

  • Sets the strength of gravity, the G factor in F = G m1 m2 / r^2.


    • gravity: number

      the strength of gravity

    Returns void

  • Returns a minimal string representation of this object, usually giving just identity information like the class name and name of the object.

    For an object whose main purpose is to represent another Printable object, it is recommended to include the result of calling toStringShort on that other object. For example, calling toStringShort() on a DisplayShape might return something like this:


    Returns string

    a minimal string representation of this object.

Generated using TypeDoc