pythonphysicsvpythonglowscript

Why has this piece of code to be there?


I try to understand this code and I actually understood the whole thing except these 2 line:

f_grav = gravity * sun.mass * earth.mass * (sun.pos - earth.pos).norm() / (sun.pos - earth.pos).mag2
earth.vel = earth.vel + (f_grav/earth.mass) * dt

Why couldn't it just be: f_grav = gravity * sun.mass * earth.mass / (sun.pos-earth.pos)**2

I also dont get the role of .norm() and .mag2

Here is the whole code snippet of the program(GlowScript):

sunRadius = 10 * realSunRadius   # the size for our solar system
earthRadius = sunRadius * 0.25    # note: real value would be sunRadius * 0.01, a good choice for sim is * 0.25
astronomicalUnit = 212.7 * realSunRadius     # the distance from Sun to Earth - the Sun is about 100 Sun diameters away      
gravity = 6.6e-11   # sets the strength of the gravitational constant to 6.6x10-11 Newton x meters squared per kilograms squared

# create the Sun object
sun = sphere( radius = sunRadius, opacity = 0.7, emissive = True, texture = "http://i.imgur.com/yoEzbtg.jpg" ) 
sun.mass = 2e30   # mass of the Sun in kilograms is 2,000,000,000,000,000,000,000,000,000,000 kg
sun.pos = vec(0,0,0)
sun.vel = vec(0,0,0)

# place a few sources of light at the same position as the Sun to illuminate the Earth and Moon objects
sunlight = local_light( pos = vec(0,0,0), color=color.white )
more_sunlight = local_light( pos = vec(0,0,0), color=color.white )  # I found adding two lights was about right

# create the Earth object
earth = sphere ( radius = earthRadius, texture = "http://i.imgur.com/rhFu01b.jpg",make_trail=True)
earth.mass = 6e24   # mass of Earth in kilograms
earth.pos = vec(astronomicalUnit, 0, 0)
earth.vel = vec(0,0,-30000)   # the Earth is moving around 30000 m/s


dt = 10000


# below is the main loop of the program - everything above is "setup" and now we are in the main "loop" where all the action occurs
while (True):   # this will make it loop forever

    rate(100)   # this limits the animation rate so that it won't depend on computer/browser processor speed

    # calculate the force of gravity on each object
    f_grav = gravity * sun.mass * earth.mass * (sun.pos - earth.pos).norm() / (sun.pos - earth.pos).mag2
    earth.vel = earth.vel + (f_grav/earth.mass) * dt


    # update the position of the Earth and Moon by using simple circle trigonometry
    earth.pos = earth.pos + earth.vel * dt 

Solution

  • (sun.pos-earth.pos) is a vector. I don't think you can do (sun.pos-earth.pos)**2 because you can't square a vector. Unless you're trying to do a dot product of the vector with itself? But the result of a dot product is a scalar, so f_grav would be a scalar. Forces are vectors, so it doesn't make sense to use a dot product there.

    In comparison, f_grav = gravity * sun.mass * earth.mass * (sun.pos - earth.pos).norm() / (sun.pos - earth.pos).mag2 makes sense because you're multiplying (sun.pos - earth.pos).norm(), a vector, by three scalars, and dividing by one scalar. So the result is a vector as desired.