# Coding: orbiting planets using vpython

Watch this threadPage 1 of 1

Go to first unread

Skip to page:

poop yakuza

Badges:
9

Rep:

?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#1

Im stuck on animating the orbit of a planet around a star. No specific values are given and i was asked to write a general code. Ive checked that the first two functions linked below are indeed correct but im having trouble with the last one(part 3) which is animating.

Any help is appreciated, if you would like the theory, please say so and ill post it but its just newton's law of gravitation.

Any help is appreciated, if you would like the theory, please say so and ill post it but its just newton's law of gravitation.

Last edited by poop yakuza; 1 year ago

0

reply

Eimmanuel

Badges:
13

Rep:

?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#2

Report

#2

(Original post by

Im stuck on animating the orbit of a planet around a star. No specific values are given and i was asked to write a general code. Ive checked that the first two functions linked below are indeed correct but im having trouble with the last one(part 3) which is animating.

Any help is appreciated, if you would like the theory, please say so and ill post it but its just newton's law of gravitation.

**poop yakuza**)Im stuck on animating the orbit of a planet around a star. No specific values are given and i was asked to write a general code. Ive checked that the first two functions linked below are indeed correct but im having trouble with the last one(part 3) which is animating.

Any help is appreciated, if you would like the theory, please say so and ill post it but its just newton's law of gravitation.

0

reply

0le

Badges:
21

Rep:

?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#3

Report

#3

Your last line is incorrect.

It should be:

Code:

// time = (+dt) time =+ dt

It should be:

Code:

//time = time + dt time += dt

0

reply

poop yakuza

Badges:
9

Rep:

?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#4

(Original post by

Your last line is incorrect.

It should be:

**0le**)Your last line is incorrect.

Code:

// time = (+dt) time =+ dt

It should be:

Code:

//time = time + dt time += dt

Last edited by poop yakuza; 1 year ago

0

reply

poop yakuza

Badges:
9

Rep:

?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#5

(Original post by

What is the trouble that you are having? Or at least post what the VPython is complaining about your code.

**Eimmanuel**)What is the trouble that you are having? Or at least post what the VPython is complaining about your code.

Last edited by poop yakuza; 1 year ago

0

reply

Badges:
9

Rep:

?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#6

here is the code:

from vpython import vector, mag, sphere, rate, color, canvas

G = 1. # gravitational constant [using units where gravitational constant is 1.0]

def force(pos1, pos2, m1, m2):

"""

Returns the gravitational force exerted by object 2 on object 1.

Input:

- pos1 = position vector of first object

- pos2 = position vector of second object

- m1 = mass of first object

- m2 = mass of second object

Depends on:

- G = gravitational constant (global variable)

"""

##################

#the position vector from object 2 to object 1

pos = pos2 - pos1

f = G*m1*m2*pos/(mag(pos))**3 #newton's gravitational force equation in vector form

return f

##################

def move_planet(position, velocity, m_star, dt):

"""

Calculate motion of planet in the gravitational field of a star with given mass

at the origin, using Euler's method.

Input:

- position: position vector of planet at start of time step

- velocity: velocity vector of planet at start of time step

- m_star: mass of star

- dt: time step

Output: (position_new, velocity_new)

- position_new: position vector of planet at end of time step

- velocity_new: velocity vector of planet at end of time step

Depends on:

- force = function to calculate the gravitational force between two objects

"""

##################

#the star is at the origin

star_pos = vector(0,0,0)

#arbritary number, there is no real reason other than defining it for calculating the acceleration in the line below

m_planet = 1

acc = (force(position, star_pos, m_planet, m_star))/m_planet

position_new = position + (velocity)*dt #the new position

velocity_new = velocity + acc*dt #the new velocity

return position_new, velocity_new

##################

def animate_planet(position, velocity, m_star, dt):

"""

Animate planetary orbit from given starting position, with given time step.

"""

##################

planet = sphere(pos=position,radius=0.2,make_trail=True, color=color.red)

dt = 0.05

time = 0

while time<= 10:

rate(1/dt)

planet.pos, velocity = move_planet(position, velocity, m_star, dt)

time += dt

##################

# Initialize canvas, and set parameters of star and planet.

canvas()

pos_planet = vector(0,2,0) # initial position of planet

v_planet = vector(-22,0,0) # initial velocity of planet

m_star = 1000. # mass of star (units where G=1)

sphere(pos=vector(0,0,0), color=color.yellow, radius=0.1) # draw star

# Animate orbit of planet

animate_planet(pos_planet, v_planet, m_star, 1e-4)

from vpython import vector, mag, sphere, rate, color, canvas

G = 1. # gravitational constant [using units where gravitational constant is 1.0]

def force(pos1, pos2, m1, m2):

"""

Returns the gravitational force exerted by object 2 on object 1.

Input:

- pos1 = position vector of first object

- pos2 = position vector of second object

- m1 = mass of first object

- m2 = mass of second object

Depends on:

- G = gravitational constant (global variable)

"""

##################

#the position vector from object 2 to object 1

pos = pos2 - pos1

f = G*m1*m2*pos/(mag(pos))**3 #newton's gravitational force equation in vector form

return f

##################

def move_planet(position, velocity, m_star, dt):

"""

Calculate motion of planet in the gravitational field of a star with given mass

at the origin, using Euler's method.

Input:

- position: position vector of planet at start of time step

- velocity: velocity vector of planet at start of time step

- m_star: mass of star

- dt: time step

Output: (position_new, velocity_new)

- position_new: position vector of planet at end of time step

- velocity_new: velocity vector of planet at end of time step

Depends on:

- force = function to calculate the gravitational force between two objects

"""

##################

#the star is at the origin

star_pos = vector(0,0,0)

#arbritary number, there is no real reason other than defining it for calculating the acceleration in the line below

m_planet = 1

acc = (force(position, star_pos, m_planet, m_star))/m_planet

position_new = position + (velocity)*dt #the new position

velocity_new = velocity + acc*dt #the new velocity

return position_new, velocity_new

##################

def animate_planet(position, velocity, m_star, dt):

"""

Animate planetary orbit from given starting position, with given time step.

"""

##################

planet = sphere(pos=position,radius=0.2,make_trail=True, color=color.red)

dt = 0.05

time = 0

while time<= 10:

rate(1/dt)

planet.pos, velocity = move_planet(position, velocity, m_star, dt)

time += dt

##################

# Initialize canvas, and set parameters of star and planet.

canvas()

pos_planet = vector(0,2,0) # initial position of planet

v_planet = vector(-22,0,0) # initial velocity of planet

m_star = 1000. # mass of star (units where G=1)

sphere(pos=vector(0,0,0), color=color.yellow, radius=0.1) # draw star

# Animate orbit of planet

animate_planet(pos_planet, v_planet, m_star, 1e-4)

Last edited by poop yakuza; 1 year ago

0

reply

Badges:
9

Rep:

?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#7

update: it works now,

all i had to do was change

"planet.pos, velocity = move_planet(position, velocity, m_star, dt)" to "planet.pos, velocity = move_planet(planet.pos, velocity, m_star, dt)"

all i had to do was change

"planet.pos, velocity = move_planet(position, velocity, m_star, dt)" to "planet.pos, velocity = move_planet(planet.pos, velocity, m_star, dt)"

1

reply

Badges:
9

Rep:

?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#8

Ive another question on this topic.

How would you go about adapting the code for two planets and taking into account the gravitational attraction between the

planets as well as between each planet and the star? specifically how would the equations of motion change?

How would you go about adapting the code for two planets and taking into account the gravitational attraction between the

planets as well as between each planet and the star? specifically how would the equations of motion change?

Last edited by poop yakuza; 1 year ago

0

reply

0le

Badges:
21

Rep:

?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#9

Report

#9

(Original post by

Ive another question on this topic.

How would you go about adapting the code for two planets and taking into account the gravitational attraction between the

planets as well as between each planet and the star? specifically how would the equations of motion change?

**poop yakuza**)Ive another question on this topic.

How would you go about adapting the code for two planets and taking into account the gravitational attraction between the

planets as well as between each planet and the star? specifically how would the equations of motion change?

https://en.wikipedia.org/wiki/Three-body_problem

0

reply

Badges:
9

Rep:

?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#10

(Original post by

It might be the three body problem(?):

https://en.wikipedia.org/wiki/Three-body_problem

**0le**)It might be the three body problem(?):

https://en.wikipedia.org/wiki/Three-body_problem

0

reply

Badges:
9

Rep:

?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#11

currently, I have no idea how to adapt the move_planet function (2nd pic) for this case. the picture below shows what i need to do without giving specific instructions. id appreciate any help on this

part A refers to the code from the first post

part A refers to the code from the first post

0

reply

Eimmanuel

Badges:
13

Rep:

?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#12

Report

#12

(Original post by

currently, I have no idea how to adapt the move_planet function (2nd pic) for this case. the picture below shows what i need to do without giving specific instructions. id appreciate any help on this

part A refers to the code from the first post

**poop yakuza**)currently, I have no idea how to adapt the move_planet function (2nd pic) for this case. the picture below shows what i need to do without giving specific instructions. id appreciate any help on this

part A refers to the code from the first post

The modifications is replicating the code in the move_planet by adding the new interaction.

The N2L will be

For m1,

Force between m1 and m2 plus force between m1 and star equal m1×acceleration.

Do the same for m2.

Then apply Euler algorithm to move the planets.

Add the trailing accordingly in the main program.

0

reply

Badges:
9

Rep:

?
You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#13

(Original post by

Warning, I may not correctly read the detail as I am typing using a phone.

The modifications is replicating the code in the move_planet by adding the new interaction.

The N2L will be

For m1,

Force between m1 and m2 plus force between m1 and star equal m1×acceleration.

Do the same for m2.

Then apply Euler algorithm to move the planets.

Add the trailing accordingly in the main program.

**Eimmanuel**)Warning, I may not correctly read the detail as I am typing using a phone.

The modifications is replicating the code in the move_planet by adding the new interaction.

The N2L will be

For m1,

Force between m1 and m2 plus force between m1 and star equal m1×acceleration.

Do the same for m2.

Then apply Euler algorithm to move the planets.

Add the trailing accordingly in the main program.

the code works now too!

0

reply

X

Page 1 of 1

Go to first unread

Skip to page:

### Quick Reply

Back

to top

to top