计算三维力的方向

2024-04-25 11:37:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试建立一个太阳系的三维模拟,但是我很难找到这个2dpython代码的三维等价物

# Compute the force of attraction f = G * self.mass * other.mass / (d**2)

# Compute the direction of the force.
    theta = math.atan2(dy, dx)
    fx = math.cos(theta) * f
    fy = math.sin(theta) * f
    return fx, fy

Tags: ofthe代码selfmathmassfxother
1条回答
网友
1楼 · 发布于 2024-04-25 11:37:15

二维码的效率低得离谱。你根本不需要三角学。它只是把力的大小乘以(dx,dy)方向的单位向量。假设你已经知道向量的长度,d,你所需要的就是

fx, fy = f*dx/d, f*dy/d

在三维中

fx, fy, fz = f*dx/d, f*dy/d, f*dz/d

相关问题 更多 >