我最近一直在使用 Raylib 3.7.0 在 python 3.8.10 中进行轨道模拟。
“pip list”命令的片段:
raylib 3.7.0.post10
我一直遇到一个问题,即当以不同的帧速率运行时,轨道严重不同,即使我是按增量时间计算的。我对问题原因的第一个怀疑是游戏循环可能以与实际游戏不同的速度运行,但我不确定如何检查。
这是我用来计算轨道的代码。请注意:
- “太阳”对象固定在屏幕中央
- curr_quad 变量存储卫星相对于太阳的八个部分中的哪一个,其中 4 个部分是从“太阳”向上、向右、向下和向左的直线;其他 4 个部分是线之间的区域。
- 下面的代码是位于主文件之外的函数。
def calc_orbit(sat: Planet, sun: Planet, g_constant: float, first_frame: bool, delta_time: float) -> tuple:
axes_dist: Vec2i # Distance between two objects expressed on the x and y axes
line_dist: float # Distance between two objects expressed as a straight line
force: float # Gravitational force acting on the orbiting object: 'sat'
ratio: Vec2f # An x:y ratio to convert line_dist back into axes_dist
acceleration: float # Amount to add to 'sat.vel' (velocity)
curr_quad: int # Sector of the screen that 'sat' is in
vec_acceleration: Vec2f # Acceleration represented as a vector quantity
axes_dist, line_dist = get_dist(sat.pos, sun.pos) # Calculate axes and line distances between the satellite and the sun
ratio = movement_ratio(axes_dist) # Calculate x:y ratio
force = calc_force(sat.mass, sun.mass, line_dist, g_constant) # calculate force using positions, distance, and a gravitational constant
acceleration = calc_accel(force, sat.mass, ratio) # Calculate acceleration
curr_quad = get_quadrant(sat.pos, sun.pos) # return what sector 'sat' is in
if not first_frame: # Make sure it isn't the first frame, as there would be no delta time
acceleration = adjust_accel_for_dt(acceleration, delta_time) # Multiply acceleration by delta time
vec_acceleration = split_acceleration(ratio, acceleration) # Convert acceleration to a vector
vec_acceleration = adjust_accel_by_quad(curr_quad, vec_acceleration) # Adjust values according to the current quad
else:
vec_acceleration = Vec2f(0, 0)
return vec_acceleration, line_dist, False # Return vector acceleration, line distance, and the new value for first_frame
我很确定代码不是错误的原因,我只是认为它有助于澄清问题并帮助您掌握场景。
很抱歉这个非常模糊的问题,非常感谢任何帮助。