Azal-Amer
Azal-Amer

Reputation: 11

Freefalling objects fly through a center of gravity in VPython, the moment they hit the center though they speed up and shoot out to the side

I have this function here

def gravitationalForce(p1,p2):
    G = 1=

    rVector = p1.pos - p2.pos 
    # if vp.mag(rVector) <.01:
    #   planet1.pos =- planet1.pos
    #   star.pos = - star.pos
    rMagnitude = vp.mag(rVector)
    rHat = rVector / rMagnitude
    F = - rHat * G * p1.mass * p2.mass /rMagnitude**2
    return F

and what I'm trying to do is make a single object/sphere that starts with a momentum of zero, some distance from a massive object. Over time the object pulls it in, and then it flies through to the other side, slowing down, and osculating back and forth. My issue is that while I am accurately simulating gravity between the two points, the moment the magnitude of the radius hits 0, the force seems to go to infinity, shooting the particle out at very high speeds in the following time steps, before it has a chance to be slowed down my the force of gravity on the other side. I tried to skip over the center when it was some small radius by implementing the conditional

    # if vp.mag(rVector) <.01:
    #   planet1.pos =- planet1.pos
    #   star.pos = - star.pos

but this made no change and the object still shoots out. Here are the given objects I generate

star = vp.sphere(pos=vp.vector(0,0,0), radius=0.2, color=vp.color.yellow,
               mass = 2.0*10000, momentum=vp.vector(0,0,0), make_trail=True)
planet1 = vp.sphere(pos=vp.vector(-1,0,0), radius=0.05, color=vp.color.green,
               mass = 1, momentum=vp.vector(10,0,0), make_trail=True)

Upvotes: 1

Views: 119

Answers (1)

user1114907
user1114907

Reputation: 992

You need to check to see whether the next step would, if carried out, put the planet inside the star. If so, you might move to the near side of the star, then move at (say) a constant speed to the other side of the star and turn on gravity again. The alternative would be to take very short steps while inside the star. This isn't quite physical, because you really need to use Gauss's law to calculate (in the approximation of a constant density for the star) the actual gravitational force.

Upvotes: 1

Related Questions