4

(Working in 2d for simplicity) I know that the force exerted on two spherical bodies by each other due to gravity is G(m1*m2/r**2) However, for a non-spherical object, I cannot find an algorithm or formula that is able to calculate the same force. My initial thought was to pack circles into the object so that the force by gravity would be equal to the sum of the forces by each of the circles. E.g (pseudocode),

def gravity(pos1,shape):
     circles = packCircles(shape.points)
     force = 0
     for each circle in circles:
           distance = distanceTo(pos1,circle.pos)
           force += newtonForce(distance,shape.mass,1) #1 mass of observer
     return force 

Would this be a viable solution? If so, how would I pack circles efficiently and quickly? If not, is there a better solution?

Edit: Notice how I want to find the force of the object at a specific point, so angles between the circle and observer will need to be calculated (and vectors summed). It is different from finding the total force exerted.

Max
  • 434
  • 1
  • 3
  • 13
  • Is this helpful: https://en.wikipedia.org/wiki/Centroid – xiaoyu2006 Jan 16 '20 at 11:48
  • 1
    Isn't this formula applicable to every bodies? – Clément Jan 16 '20 at 11:49
  • 6
    Seems more of a physics question than a programming question. [related](https://physics.stackexchange.com/questions/77182/how-does-the-gravity-of-a-massive-non-spherical-object-act-on-things-around-it) – Ahmet Jan 16 '20 at 11:51
  • I think it's easy: work out each's centroid and apply his `newtonForce` to each one as a particle(`(centroid->pos,mass)`). – xiaoyu2006 Jan 16 '20 at 11:52
  • 2
    It is in essence an integrale over the volumes of the two items (each "point" from one object attracts each "point" from the other object). – Willem Van Onsem Jan 16 '20 at 11:54
  • Ycao, The centroid is the center of gravity, it does not solve forces of gravity by the object. Clement, the formula is only applicable for sphere's, although it is used to approximate it for planets (which may not be perfectly spherical). Ahmed, I understand the physics, but am unsure how implement an algorithm to solve the problem. – Max Jan 16 '20 at 11:54
  • @WillemVanOnsem exactly, do you have any resources on how I could program that? – Max Jan 16 '20 at 11:55
  • In terms of the physics, if the body is rigid, I think the total force exerted should be the same regardless of whether you calculate the force on individual elements and add them up, or whether you treat the object like a point mass (calculate the centre of mass). However, calculating the centre of mass will likely require you to dissect the body into multiple elements anyway... Circle packing is quite a difficult problem - would a square packing (pixellisation) be sufficient? – Rob Jan 16 '20 at 11:55
  • @Rob I thought about square packing, but the formula to calculate gravitational forces by a cube (or square in this case) Is quite complex and I don't understand it fully. It may be more computationally expensive: https://gizmodo.com/how-gravity-would-be-different-if-the-world-were-a-cube-1492018223 – Max Jan 16 '20 at 11:58
  • 3
    centroids and `G(m1*m2/r**2)` work only for distant enough objects ... for close ones you need to integrate (area or volume) ... or convert your objects to set of mass points instead (with decreased accuracy but much faster and simpler to compute)... – Spektre Jan 16 '20 at 12:20
  • @Spektre so assuming the object is of the same density at every point, you can just turn it into a grid of points and use the Newtonian formula (treating every point as a circle with no size?) – Max Jan 16 '20 at 12:30
  • 1
    @MaxDoesStuff yes ... and then you compute the resulting force/torque on the centroid of the object from the forces of its mass points ... so you can accelerate and rotate ... – Spektre Jan 16 '20 at 13:14
  • You're saying you want to use vectors, but your `force` variable is scalar. Maybe you should edit your code to use a 2d force vector. – Stratubas Jan 16 '20 at 13:36
  • 1
    *Would this be a viable solution?* Yes, this is essentially using superposition to find the net gravitational interaction between the two bodies. However, it is massively overkill - all you need to do for uniform density bodies of any shape (2D or 3D) is find the centers of mass. The gravitational force will **always** act in the direction of the ray formed by going from one COM to the other. This is true for any two bodies in a system of any number of bodies, and their interactions are mutually independent – William Miller Jan 17 '20 at 01:28
  • @WilliamMiller It says at https://en.wikipedia.org/wiki/Newton's_law_of_universal_gravitation#Bodies_with_spatial_extent that, quote " it can be shown that an object with a spherically-symmetric distribution of mass exerts the same gravitational attraction on external bodies as if all the object's mass were concentrated at a point at its center.[2] (This is not generally true for non-spherically-symmetrical bodies.) ". This would imply your solution would not work for non-spherically-symmetrical bodies, correct? – Max Jan 17 '20 at 11:23
  • @MaxDoesStuff I can't answer that succinctly enough for a comment, I would recommend you read my answer and see if that explains it more clearly, if not feel free to ask follow up questions – William Miller Jan 18 '20 at 02:27
  • @Spektre *centroids and G(m1*m2/r**2) work only for distant enough objects* can you explain why this fails for close objects? – William Miller Jan 19 '20 at 00:23
  • 1
    @WilliamMiller for example imagine `U` shape object and some ball in it some forces are negating others so the total force on ball is smaller than `G(m1*m2/r**2)` single mass point per object approximation ... the distortions are the bigger the closer the objects are relative to their size, orientation and shape and density inhomogenity. – Spektre Jan 19 '20 at 08:37
  • @WilliamMiller here [example simulation result](https://i.stack.imgur.com/HPrLi.png) so red object is more complicated shape and blue ball is nearby. The yellow line is resulting force of all the mass points simulated as you can see the direction is clearly not lieing on the line going through COM of both objects ... the size of force is also different ... both objects have the same constant mass density – Spektre Jan 19 '20 at 09:33
  • 1
    @Spektre It is intuitive to me that the centroid approach breaks down in the inhomogeneous case, and for distributions whose shape and separation result in geometric enclosure (even partially) it is intuitive that the COM approach breaks down - the force on an object at the COM (`r=0`) is not infinite. The one which was less intuitive to me is why the physical separation alone (independent of enclosure) would cause the COM approach to fail - but it is clearer after considering that the contributions from the nearer masses must be larger. I will need to modify my answer to clarify the caveats – William Miller Jan 19 '20 at 09:45
  • @WilliamMiller yep ... even symmetrical shapes are a problem if not oriented in convenient positions which is all the time.... beware that these errors are present at all distances even infinitte but the amount of error they creating (to approximated result) is diminishing with distance very quickly ... You can compute the visual size (angle) of one object from the other one and if the angle is big the error will be be big too ... – Spektre Jan 19 '20 at 09:50
  • @Spektre That certainly makes sense, I think my lack of intuitive understanding there is rooted in my orbital dynamics background - I never deal with circumstances where the shape effect is non-negligible. Thanks for taking the time to elaborate – William Miller Jan 19 '20 at 09:52
  • @WilliamMiller this [Is it possible to make realistic n-body solar system simulation in matter of size and mass?](https://stackoverflow.com/a/28020934/2521214) might interest/amuse you – Spektre Jan 19 '20 at 09:53
  • @Spektre That does indeed, my thesis work is actually working on a hybrid symplectic integrator.... but I won’t get into that since I’ve already digressed enough for one comment thread - don’t need to clutter OP’s inbox with thesis talk on top of it – William Miller Jan 19 '20 at 09:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206248/discussion-between-william-miller-and-spektre). – William Miller Jan 20 '20 at 00:59

1 Answers1

8

Background

Some of this explanation will be somewhat off-topic but I think it is necessary to help clarify some of the things brought up in the comments and because much of this is somewhat counterintuitive.

This explanation of gravitational interactions depends on the concept of point masses. Suppose you have two point masses which are in an isolated system separated from each other by some distance, r1, with masses of m1 and m2 respectively,

enter image description here

The gravitational field created by m1 is given by

enter image description here

where G is the universal gravitational constant, r is the distance from m1 and is the unit direction along the line between m1 and m2.

The gravitational force exerted on m2 by this field is given by

enter image description here

Note   -   Importantly, this is true for any two point masses at any distance.1

The field nature of gravitational interactions allows us to employ superposition in calculating the net gravitational force due to multiple interactions. Consider if we add another mass, m3 to the previous scenario,

enter image description here

Then the gravitational force on mass m2 is simply a sum of the gravitational force from the fields created by each other mass,

enter image description here

with ri,j = rj,i. This holds for any number of masses at any separations. It also implies that the field created by a collection of masses can be aggregated by a vector sum, if you prefer that formalism.

Now consider if we had a very large number of point masses, M, aggregated together in a continuous, rigid body of uniform density. Then we wanted to calculate the gravitational force on a single spatially distinct point mass, m, due to the aggregate mass, M:

enter image description here

Then instead of considering point masses we can consider areas (or volumes) of mass of differential size and either integrate or sum the effect of these areas (or volumes) on the point mass. In the two dimensional case, the magnitude of the gravitational force is then

enter image description here

where σ is the density of the aggregate mass.2 This is equivalent to summing the gravitational vector field due to each differential mass, σdxdy. Such equivalence is critically important because it implies that for any point mass far enough outside of a mass distribution, the gravitational force due to the mass distribution is almost exactly the same as it would be for a point mass of mass M located at the center of mass of the mass distribution.3 4

This means that, to very good approximation, when it comes to calculating the gravitational field due to any mass distribution, the mass distribution can be replaced with an equivalent-mass point mass at the center of mass of the distribution. This holds for any number of spatially distinct mass distributions, whether those distributions constitute a rigid body or not. Furthermore, it means that you can even aggregate groups of distributions into a single point mass at the center of mass of the system.5 As long as the reference point is far enough away.

However, in order to find the gravitational force on a point mass due to a mass distribution at any point, for any mass distribution in a shape and separation agnostic manner we have to calculate the gravitational field at that point by summing the contributions from each portion of the mass distribution.6

Back to the question

Of course for an arbitrary polygon or polyhedron the analytical solution can be prohibitively difficult, so it is much simpler to use a summation, and algorithmic approaches will similarly use a summation.

Algorithmically speaking, the simplest approach here is not actually geometric packing (with either circles/spheres or squares/cubes). It's not impossible to use packing, but mathematically there are significant challenges to that approach - it is better to employ a method which relies on simpler math. One such approach is to define a grid encompassing the spatial extent of the mass distribution, and then create simple (square/cubic or rectangular/cuboidic) polygons or polyhedrons with the grid points as vertices. This creates three kinds of polygons or polyhedrons:

  1. Those which do not encompass any of the mass distribution
  2. Those which are completely filled by the mass distribution
  3. Those which are partially filled by the mass distribution

enter image description here

Center of Mass - Approach 1

This will work well when the distance from the reference point to the mass distribution is large relative to the angular extent of the distribution, and when there is no geometric enclosure of the reference by the mass distribution (or by any several distributions).

You can then find the center of mass, R of the distribution by summing the contributions from each polygon,

enter image description here

where M is the total mass of the distribution, ri is the spatial vector to the geometric center of the ith polygon, and mi is the density times the portion of the polygon which contains mass (i.e. 1.00 for completely filled polygons and 0.00 for completely empty polygons). As you increase the sampling size (the number of grid points) the approximation for the center of mass will approach the analytical solution. Once you have the center of mass it is trivial to calculate the gravitational field created: you simply place a point mass of mass M at the point R and use the equation from above.

For demonstration, here is an implementation of the described approach in two dimensions in Python using the shapely library for the polygon operations:

import numpy as np
import matplotlib.pyplot as plt
import shapely.geometry as geom

def centerOfMass(r, density = 1.0, n = 100):
    theta = np.linspace(0, np.pi*2, len(r))
    xy = np.stack([np.cos(theta)*r, np.sin(theta)*r], 1)

    mass_dist = geom.Polygon(xy)
    x, y = mass_dist.exterior.xy

    # Create the grid and populate with polygons
    gx, gy  = np.meshgrid(np.linspace(min(x), max(x), n), np.linspace(min(y),
                          max(y), n))
    polygons = [geom.Polygon([[gx[i,j],    gy[i,j]], 
                              [gx[i,j+1],  gy[i,j+1]], 
                              [gx[i+1,j+1],gy[i+1,j+1]], 
                              [gx[i+1,j],  gy[i+1,j]],
                              [gx[i,j],    gy[i,j]]])
                for i in range(gx.shape[0]-1) for j in range(gx.shape[1]-1)]

    # Calculate center of mass
    R = np.zeros(2)
    M = 0
    for p in polygons:
        m = (p.intersection(mass_dist).area / p.area) * density
        M += m
        R += m * np.array([p.centroid.x, p.centroid.y])

    return geom.Point(R / M), M

density = 1.0     # kg/m^2
G = 6.67408e-11   # m^3/kgs^2
theta = np.linspace(0, np.pi*2, 100)
r = np.cos(theta*2+np.pi)+5+np.sin(theta)+np.cos(theta*3+np.pi/6)

R, M = centerOfMass(r, density)
m = geom.Point(20, 0)
r_1 = m.distance(R)
m_1 = 5.0         # kg
F = G * (m_1 * M) / r_1**2
rhat = np.array([R.x - m.x, R.y - m.y])
rhat /= (rhat[0]**2 + rhat[1]**2)**0.5

# Draw the mass distribution and force vector, etc
plt.figure(figsize=(12, 6))
plt.axis('off')
plt.plot(np.cos(theta)*r, np.sin(theta)*r, color='k', lw=0.5, linestyle='-')
plt.scatter(m.x, m.y, s=20, color='k')
plt.text(m.x, m.y-1, r'$m$', ha='center')
plt.text(1, -1, r'$M$', ha='center')
plt.quiver([m.x], [m.y], [rhat[0]], [rhat[1]], width=0.004, 
           scale=0.25, scale_units='xy')
plt.text(m.x - 5, m.y + 1, r'$F = {:.5e}$'.format(F))
plt.scatter(R.x, R.y, color='k')
plt.text(R.x, R.y+0.5, 'Center of Mass', va='bottom', ha='center')
plt.gca().set_aspect('equal')
plt.show()

enter image description here

This approach is a bit overkill: in most cases it would suffice to find the centroid and the area of the polygon multiplied by the density for the center of mass and total mass. However, it would work for even non-uniform mass distributions - that's why I have used it for demonstration.

Field Summation - Approach 2

In many cases this approach is also overkill, especially in comparison to the first approach, but it will provide the best approximation under any distributions (within the classical regime).

The idea here is to sum the effect of each chunk of the mass distribution on a point mass to determine the net gravitational force (based on the premise that the gravitational fields can be independently added)

class pointMass:
    def __init__(self, mass, x, y):
        self.mass = mass
        self.x = x
        self.y = y

density = 1.0     # kg/m^2
G = 6.67408e-11   # m^3/kgs^2

def netForce(r, m1, density = 1.0, n = 100):
    theta = np.linspace(0, np.pi*2, len(r))
    xy = np.stack([np.cos(theta)*r, np.sin(theta)*r], 1)

    # Create a shapely polygon for the mass distribution
    mass_dist = geom.Polygon(xy)
    x, y = mass_dist.exterior.xy

    # Create the grid and populate with polygons
    gx, gy  = np.meshgrid(np.linspace(min(x), max(x), n), np.linspace(min(y), 
                          max(y), n))
    polygons = [geom.Polygon([[gx[i,j],    gy[i,j]], 
                              [gx[i,j+1],  gy[i,j+1]], 
                              [gx[i+1,j+1],gy[i+1,j+1]], 
                              [gx[i+1,j],  gy[i+1,j]],
                              [gx[i,j],    gy[i,j]]])
                for i in range(gx.shape[0]-1) for j in range(gx.shape[1]-1)]

    g = np.zeros(2)
    for p in polygons:
        m2 = (p.intersection(mass_dist).area / p.area) * density
        rhat = np.array([p.centroid.x - m1.x, p.centroid.y - m1.y]) 
        rhat /= (rhat[0]**2 + rhat[1]**2)**0.5
        g += m1.mass * m2 / p.centroid.distance(geom.Point(m1.x, m1.y))**2 * rhat
    g *= G
    
    return g

theta = np.linspace(0, np.pi*2, 100)
r = np.cos(theta*2+np.pi)+5+np.sin(theta)+np.cos(theta*3+np.pi/6)
m = pointMass(5.0, 20.0, 0.0)
g = netForce(r, m)

plt.figure(figsize=(12, 6))
plt.axis('off')
plt.plot(np.cos(theta)*r, np.sin(theta)*r, color='k', lw=0.5, linestyle='-')
plt.scatter(m.x, m.y, s=20, color='k')
plt.text(m.x, m.y-1, r'$m$', ha='center')
plt.text(1, -1, r'$M$', ha='center')
ghat = g / (g[0]**2 + g[1]**2)**0.5
plt.quiver([m.x], [m.y], [ghat[0]], [ghat[1]], width=0.004, 
           scale=0.25, scale_units='xy')
plt.text(m.x - 5, m.y + 1, r'$F = ({:0.3e}, {:0.3e})$'.format(g[0], g[1]))
plt.gca().set_aspect('equal')
plt.show()

Which, for the relatively simple test case being used, gives a result which is very close to the first approach:

enter image description here

But while there are cases where the first approach will not work correctly, there are no such cases where the second approach will fail (in the classical regime) so it is advisable to favor this approach.


1This does break down under extremes, e.g. past the event horizon of black holes, or when r approaches the Planck length, but those cases are not the subject of this question.

2This becomes significantly more complex in cases where the density is non-uniform, and there is no trivial analytical solution in cases where the mass distribution can not be described symbolically.

3It should probably be noted that this is effectively what the integral is doing; finding the center of mass.

4For a point mass within a mass distribution Newton's Shell Theorem, or a field summation must be used.

5In astronomy this is called a barycenter, and bodies always orbit the barycenter of the system - not the center of mass of any given body.

6In some cases it is sufficient to use Newton's Shell Theorem, however those cases are not distribution geometry agnostic.

William Miller
  • 9,839
  • 3
  • 25
  • 46
  • This is an *absurdly* detailed answer, How does this work for multiple mass distributions? – Ghost Jan 19 '20 at 00:57
  • 1
    It should work correctly, but you would have to pass each polygon representing the different mass distributions into the function to find their centers of mass and total masses of the different bodies – William Miller Jan 19 '20 at 01:05
  • 1
    @WilliamMiller Yes I saw it before It looks OK to me but the stuff after: `However, in order to find` is a bit hard for rookie to grasp maybe better definition of the condition when the mass point approximation is working would be that `max_size_of_objects << min_distance_of_objects` or `atan(max_size_of_objects/min_distance_of_objects) < accuracy_angle` maybe image would be good to add to show it. I did not check the code as Python is foreign to me and too lazy to decipher :). Also beware site does not notify from the chat ! only if you are physicaly logged in the chat page. – Spektre Jan 20 '20 at 08:38
  • @Spektre Thanks for taking a look, I'll add some diagrams and try to explain the conditions for using the COM approximation better like you suggest – William Miller Jan 20 '20 at 08:45
  • 1
    I wasn't expecting such a detailed answer! I am sorry for not noticing sooner, Since I was working by myself to complete the question, and had stumbled upon (a more primitive) version of your solution by summing the vector forces in a grid. Is it correct in saying you can extend the function to work with two objects instead of one being a point mass? Such is not the subject of the question however - with the knowledge in the question I think I can answer it myself, though. Thanks so much :) – Max Jan 20 '20 at 10:18
  • @MaxDoesStuff It is correct that you could extend these approaches to work with two objects instead of an object and a point mass - for the COM approach that is quite trivial since each object is being approximated as a point mass anyways - but the COM approach will only work if the two objects are sufficiently distant. The fields approach will work for any configuration of masses at any distances (even for finding internal forces). I am planning to add some additional examples consisting of multiple objects – William Miller Jan 20 '20 at 22:16