[英]Compute the direction of the force in 3d
I'm trying to build a 3D simulation of a solar system but i'm having difficulty in finding the 3D equivalent of this 2D python code 我正在尝试建立太阳系的3D模拟,但是我很难找到与该2D python代码等效的3D
# 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
The 2-D code is ridiculously inefficient. 二维代码效率极低。 You don't need any trigonometry at all. 您根本不需要任何三角函数。 It's just multiplying the magnitude of the of the force, f
, by a unit vector in the (dx, dy) direction. 它只是将力f
大小乘以(dx,dy)方向上的单位矢量。 Given that you already know the length of the vector, d
, all you need is 假设您已经知道向量d
的长度,那么您所需要做的就是
fx, fy = f*dx/d, f*dy/d
In 3-D 在3-D中
fx, fy, fz = f*dx/d, f*dy/d, f*dz/d
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.