问题
So I'm trying to simulate the earth travelling around the sun where the velocity of the earth is determined by the angle its at to the origin and the horizontal. I did this by creating a function that uses tanh (opposite/adjacent) rule for triangles, O_correction(x,y)
. The problem is that instead of a circular orbit its instead spiralling out and I'm not sure why.
scene = canvas()
scene.background = color.white
O = 0
ball = sphere(pos=vector(10,0,0), radius=0.1, color=color.blue)
x = ball.pos.x
y = ball.pos.y
def O_correction(x,y):
O = math.atan((((y)**2)**0.5)/(((x)**2)**0.5))
answer = O
if x >= 0 and y >= 0:
answer = O
if x < 0 and y >= 0:
answer = pi - O
if x <= 0 and y < 0:
answer = O + pi
if x > 0 and y < 0:
answer =pi*2 - O
return answer
t =0
while t < 100:
x = ball.pos.x
y = ball.pos.y
print = (float(O_correction(x,y))
print = ((x**2) + (y**2))**0.5)
ball.pos.x -= sin(O_correction(x,y))
ball.pos.y += cos(O_correction(x,y))
print(" ")
t += 1
Would very much appreciate some help, Cheers
回答1:
I don't know python, but I know physics.
At each step you move the Earth a fixed distance along a tangent to the orbit, rather than along the orbit itself. That gives you an outward spiral (which will actually get less severe as you go out).
Try making the time increment smaller (e.g. by dividing the position adjustment by 100), and the spiral effect will get much smaller.
If you want to do better than that, you'll need a different formula. You could either impose a circular orbit, or do something based on conserved quantities (which requires a considerable understanding of basic physics).
来源:https://stackoverflow.com/questions/44978638/simulating-orbits