Search code examples
pythonphysicsvpythonglowscript

Cannot add scalar and a vector error in VPyhton (GlowScript)


I'm implementing a solar system with VPython in GlowScript. Now I have received this error when running: Error cannot add scalar and a vector. I think I've done all correctly. Do I have to change something with the pos. ?

Here is the code:

GlowScript 2.7 VPython
from visual import *

scene = display(width = 800, height = 800, center = vec(0,0.5,0))

#sun
sonne = sphere(pos = vec (0,0,0), radius=8, color = color.orange, shininess=1)

#earth
erde = sphere(pos = vec (50,0,0), radius=1.5, color = color.blue, make_trail=True)

erdeV = vector(0,0,5)

#masses
erdeM = 5.97*10**24
sonneM = 1.989*10**30

#Grav-constant
G = 6.67259*10**-11

for i in range (1000):
    rate(1000)
    erde.pos = erde.pos + erdeV

    #distance
    entfernung = sqrt(erde.pos.y**2 + erde.pos.z**2)


    #Gravitational law F = G * m * M / r*r --> G*s*e/AE*AE ae=Astr. Einheit
    Fgrav = G *( erdeM * sonneM) / (entfernung*entfernung)
    erdeV = erdeV + Fgrav
    erde.pos += erdeV

    if entfernung <= sonne.radius: break

Solution

  • Problem lines:

    Fgrav = G *( erdeM * sonneM) / (entfernung*entfernung)
    erdeV = erdeV + Fgrav
    

    Fgrav here is a scalar (strength of gravitational force) whereas erdeV is a vector. To remedy this, include the direction of the force:

    Fgrav = (-G * (erdeM * sonneM) / (entfernung ** 3)) * erde.pos
    erdeV = erdeV + Fgrav