python中的动画加速
我一直在用python对一个绕轨道运行的物体进行简单的模拟。我终于让它工作起来了,但随着时间的推移,动画似乎在加速。我可以看到,我的轨道物体的速度是周期性的和正确的,但在动画中,它似乎移动得越来越快。我弄不清楚为什么python中的动画加速,python,performance,animation,Python,Performance,Animation,我一直在用python对一个绕轨道运行的物体进行简单的模拟。我终于让它工作起来了,但随着时间的推移,动画似乎在加速。我可以看到,我的轨道物体的速度是周期性的和正确的,但在动画中,它似乎移动得越来越快。我弄不清楚为什么 import numpy as np import math from matplotlib import pyplot as plt from matplotlib import animation from time import sleep import sys fig =
import numpy as np
import math
from matplotlib import pyplot as plt
from matplotlib import animation
from time import sleep
import sys
fig = plt.figure()
"Defines useful information such as masses and initial velocities"
AU= (149.6e6 * 1000)
smass=1.98892*10**30 #kg mass of the sun
emass=5.9742*10**24 #kg mass of the earth
evx=0 #Earth velocity in the X direction
evy=29.783 * 1000 #Earth velocity in the Y direction
r=-1*AU #m #Earth Radius
G= 6.67408*10**-11 #m^3*kg^-1*s^-2
"Defines animated area and bodies"
ax = plt.axes(xlim=(-2*AU,2*AU), ylim=(-2*AU,2*AU))
earth = plt.Circle((0, 0), 6371000000, fc='b')
sun = plt.Circle((0,0), 6371000000, fc='y')
"Defines the starting positions of the bodies"
def init():
earth.center = (r,0)
sun.center = (0,0)
ax.add_patch(sun)
ax.add_patch(earth)
return earth,sun
"Calculates new positions of orbiting body (in this case Earth)"
def animate(i):
global evx
global evy
if i==0:
pass #this avoids a division by 0
else:
x, y = earth.center #Defines x and y
t=i*240 #Defines the time step
d=math.sqrt(x**2+y**2) #calculates distance between two objects
f=-(G*emass*smass)/(d**2) #calculates the force exerted on orbiting object
theta = math.atan2(y,x) #calculates the angle to the orbiting object
fx=math.cos(theta)*f #calculates the component of the force in the X direction on the orbiting object
fy=math.sin(theta)*f #calculates the component of the force in the Y direction on the orbiting object
evx+=fx/emass*t #calculates the velocity in the X direction of the orbiting object
evy+=fy/emass*t #calculates the velocity in the Y direction of the orbiting object
x += evx*t #converts velocity to X position using the time step
y += evy*t #converts velocity to Y position using the time step
earth.center = (x, y) #prints object at correct X and Y position
"Debug information"
#print ("distance is", d,"force is", f, "angle is", theta)
#print ("Speed is", math.sqrt(mvx**2+mvy**2))
#print ("x position is", x,"y position is",y,i)
"Watches for a collision between the two bodies distance is arbitrary"
if d<=6371000000:
print("Collision")
sys.exit()
else:
pass
return earth,sun
"Defines run time and animation information"
anim = animation.FuncAnimation(fig, animate,
init_func=init,
frames=360000,
interval=1,
blit=True)
plt.show()
将numpy导入为np
输入数学
从matplotlib导入pyplot作为plt
从matplotlib导入动画
从时间上导入睡眠
导入系统
图=plt.图()
“定义有用的信息,如质量和初始速度”
AU=(149.6e6*1000)
smass=1.98892*10**30#kg太阳质量
emass=5.9742*10**24#kg地球质量
evx=0#X方向的地球速度
evy=29.783*1000#Y方向的地球速度
r=-1*AU#m#地球半径
G=6.67408*10**-11#m^3*kg^-1*s^-2
“定义动画区域和实体”
ax=plt.轴(xlim=(-2*AU,2*AU),ylim=(-2*AU,2*AU))
地球=plt.圆((0,0),6371000000,fc='b')
太阳=plt.圆((0,0),6371000000,fc='y')
“定义实体的起始位置”
def init():
地球中心=(r,0)
sun.center=(0,0)
ax.添加补丁(太阳)
ax.添加补丁(接地)
回归地球,回归太阳
“计算轨道天体(本例中为地球)的新位置”
定义动画(i):
全球evx
全球evy
如果i==0:
通过#这避免了被0除
其他:
x、 y=地球。中心#定义x和y
t=i*240#定义时间步长
d=数学。sqrt(x**2+y**2)#计算两个对象之间的距离
f=-(G*emass*smass)/(d**2)#计算施加在轨道物体上的力
θ=数学。atan2(y,x)#计算与轨道物体的角度
fx=math.cos(θ)*f#计算轨道物体上X方向的力分量
fy=数学。sin(θ)*f#计算轨道物体在Y方向上的力分量
evx+=fx/emass*t#计算轨道物体X方向的速度
evy+=fy/emass*t#计算轨道物体Y方向上的速度
x+=evx*t#使用时间步长将速度转换为x位置
y+=evy*t#使用时间步长将速度转换为y位置
earth.center=(x,y)#在正确的x和y位置打印对象
“调试信息”
#打印(“距离为”,d,“力为”,f,“角度为”,θ)
#打印(“速度是”,数学sqrt(mvx**2+mvy**2))
#打印(“x位置为”,x,“y位置为”,y,i)
“监视两个物体之间的碰撞距离是任意的”
如果您尝试强制使用静态帧速率?我想这是可能的,也许行得通。我在matplot中找不到任何关于强制帧速率的好信息