This is the code I have tried with the aim of one sphere accelerating towards the other as it would by F=G*M*m/r^2.
This is one of the first things I've tried in glowscript and I can't find the issue with the code. When I run it the smaller sphere disappears as soon as the time changes so I assume the problem is within the while loop.
GlowScript 2.6 VPython
M=sphere(pos=vec(0,0,0),radius=10,color=color.yellow)
m=sphere(pos=vec(-50,0,0),radius=2.5,color=color.red)
## distances in x and y
Rx=M.pos.x-m.pos.x
Ry=M.pos.y-m.pos.y
## initial velocities
mvx=vec(-5,0,0)
mvy=vec(0,10,0)
## accelerations in x and y
Ax=1/(mag(Rx)**2)
Ay=1/(mag(Ry)**2)
## time and timestep
t=0
dt=0.01
## loop
while t<10:
rate(2)
t=t+dt
## s=x+ut+0.5at^2
m.pos.x=m.pos.x+mvx*dt+0.5*Ax*dt**2
m.pos.y=m.pos.y+mvy*dt+0.5*Ay*dt**2
This is one of the first things I've tried in glowscript and I can't find the issue with the code. When I run it the smaller sphere disappears as soon as the time changes so I assume the problem is within the while loop.
GlowScript 2.6 VPython
M=sphere(pos=vec(0,0,0),radius=10,color=color.yellow)
m=sphere(pos=vec(-50,0,0),radius=2.5,color=color.red)
## distances in x and y
Rx=M.pos.x-m.pos.x
Ry=M.pos.y-m.pos.y
## initial velocities
mvx=vec(-5,0,0)
mvy=vec(0,10,0)
## accelerations in x and y
Ax=1/(mag(Rx)**2)
Ay=1/(mag(Ry)**2)
## time and timestep
t=0
dt=0.01
## loop
while t<10:
rate(2)
t=t+dt
## s=x+ut+0.5at^2
m.pos.x=m.pos.x+mvx*dt+0.5*Ax*dt**2
m.pos.y=m.pos.y+mvy*dt+0.5*Ay*dt**2