Hi, everyone, I encountered an issue while creating a solar system animation in Python mode. When running the program, instead of the new planets orbiting the sun, they rapidly flicker and then disappear. Upon single-frame observation, I noticed that the positions of the planets keep changing in all four directions around the sun, and the distances increase rapidly over time. However, I couldn’t identify the problem in my code. I hope someone can help me. Thank you very much!
Below is the code:
from Planet import planet
add_library('peasycam')
def setup():
size(800,600,P3D)
global sun
cam = PeasyCam(this, 400)
sun = planet(50,0,0)
sun.spawnMoons(3,1)
# frameRate(1)
def draw():
background(0)
lights()
sun.show()
sun.orbit()
# push()
# fill(255)
# text (len(sun.planets), 10, 10)
# text (sun.planets[0].v.x, 20, 20)
# text (sun.planets[0].v.x,20, 40)
# text (sun.planets[0].v.x, 30, 60)
# pop()
class planet:
def __init__(self, r, d, o):
self.v = PVector.random3D()
self.radius = r
self.distance = d
self.v.mult(self.distance)
self.angle = random(TWO_PI)
self.orbitspeed = o
self.planets = []
def orbit(self):
self.angle = self.angle + self.orbitspeed
if len(self.planets) > 0:
for i in self.planets:
i.orbit()
def spawnMoons(self, total, level):
for i in range(total):
r = self.radius / (level * 2)
# d = random(self.radius + r, (self.radius + r) * 2)
d = random(self.radius + r, self.radius + r * 3)
o= random(-0.01, 0.01)
self.planets.append(planet(r, d, o))
if level <2 :
num = int(random(0,3))
self.planets[i].spawnMoons(num, level + 1)
def show(self):
pushMatrix()
noStroke()
v2 = PVector(1, 0, 1)
p = self.v.cross(v2)
rotate(self.angle, p.x, p.y, p.z)
stroke(255)
line(0,0,0, self.v.x, self.v.y, self.v.z)
line(0,0,0, p.x, p.y, p.z)
translate(self.v.x, self.v.y, self.v.z)
noStroke()
fill(255)
sphere(self.radius)
if len(self.planets) > 0:
for i in range(len(self.planets)):
self.planets[i].show()
popMatrix()