Python Forum
VPython - Trying to make orbit simulation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
VPython - Trying to make orbit simulation
#1
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
Reply
#2
Ok, I got this!! Maybe....

So I did a basic orbital program, 1 sun, and 1 planet that orbits it using proper orbital formulas (I think).

Catch is, it's in QBASIC (QB64 specifically)! Haha..
However I'm hoping you'll be able to tell what the concept is and it might help you along, since it's MOSTLY math formulas that do the dirty work here:
(you can use the arrow keys to gently 'shift' the sun around and cause chaos!)

Quote:CLEAR
CLS

SCREEN 12

sunX = 320
sunY = 240
OsunX = sunX
OsunY = sunY

p1x = 320
p1y = 140
op1x = p1x
op1y = p1y

p1vx = 2
p1vy = 0

Op1vx = p1vx
Op1vy = p1vx

dist_x = 0
dist_y = 0
dist = 0

accel_x = 0
accel_y = 0
Naccel_x = accel_x
Naccel_y = accel_y

F = 0
G = 6.67

sun_mass = 1000
p1_mass = 1

time = TIMER
oldtime = time
t = 0.01

b$ = ""


1:

WHILE b$ <> CHR$(27) 'Sstart of Main loop

oldtime = time ' get time value
time = TIMER

b$ = INKEY$
LOCATE 1, 1
PRINT "Time:"; USING "#####.##"; t
PRINT "Dist:"; USING "####.##"; dist
PRINT "Force:"; USING "#####.##"; F
PRINT "Accel X"; USING "#####.###"; accel_x
PRINT "Accel Y"; USING "#####.###"; accel_y

IF OsunX <> sunX OR OsunY <> sunY THEN
CIRCLE (OsunX, OsunY), 20, 0 ' erase previous position
OsunX = sunX
OsunY = sunY
END IF

IF op1x <> p1x OR op1y <> p1y THEN
CIRCLE (op1x, op1y), 10, 0 ' erase previous position
op1x = p1x
op1y = p1y
END IF

'Orbital Physics go here ------------------------------------

dist_x = p1x - sunX
dist_y = p1y - sunY
dist = SQR(dist_x * dist_x + dist_y * dist_y) ' get distance between sun and planet

IF dist > 0 THEN 'avoid divide by 0 error
F = (G * sun_mass) / (dist ^ 2) ' Force = G * M / Distance^2
END IF

accel_x = dist_x / dist
accel_y = dist_y / dist

accel_x = -accel_x
accel_y = -accel_y

accel_x = accel_x * F
accel_y = accel_y * F

IF accel_x > 3 THEN accel_x = 3
IF accel_y > 3 THEN accel_y = 3
IF accel_x < -3 THEN accel_x = -3
IF accel_y < -3 THEN accel_y = -3

p1vx = Op1vx + accel_x * t
p1vy = Op1vy + accel_y * t

p1x = p1x + (0.5 * (p1vx + Op1vx))
p1y = p1y + (0.5 * (p1vy + Op1vy))

Op1vx = p1vx
Op1vy = p1vy

t = t + 0.001 ' increment time

'------------------------------------------------------------

'Read keyboard input to reposition Sun
IF b$ = CHR$(0) + "H" THEN sunY = sunY - 1
IF b$ = CHR$(0) + "P" THEN sunY = sunY + 1
IF b$ = CHR$(0) + "K" THEN sunX = sunX - 1
IF b$ = CHR$(0) + "M" THEN sunX = sunX + 1

' IF b$ = "r" THEN
' END IF

CIRCLE (sunX, sunY), 20, 11 ' draw current position
CIRCLE (p1x, p1y), 10, 2 ' draw current position

'SLEEP
' _DELAY 0.01
_LIMIT 50

WEND

PRINT "Quitting"
Reply
#3
You might get more help in a more specialized forum - https://groups.google.com/forum/?fromgro...ript-users
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I try to import Vpython and I get a "ZeroDivisionError" Jon2222 16 2,662 Jul-26-2023, 07:37 AM
Last Post: Jon2222
  Help with simulation Geeseball 0 2,078 Oct-18-2018, 12:19 PM
Last Post: Geeseball
  Vpython Delay in plotting points SohaibAJ 0 2,073 Jul-30-2018, 08:44 PM
Last Post: SohaibAJ
  Anaconda / Spyder / VPython mwatson87 3 90,257 May-24-2018, 01:51 PM
Last Post: snippsat
  Moon earth sun orbit newfoundme 13 10,611 Oct-01-2017, 10:14 AM
Last Post: Lamarre

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020