【发布时间】:2019-01-23 21:01:51
【问题描述】:
我在 Ruby 中编写了这个函数来查找两个 2D (x,y) 向量之间的目标角度,但现在我想了解如何以类似的方式在 3D 中执行此操作:
def target_angle(point1, point2)
x1 = point1[0]
y1 = point1[1]
x2 = point2[0]
y2 = point2[1]
delta_x = x2 - x1
delta_y = y2 - y1
return Math.atan2(delta_y, delta_x)
end
给定一个对象(在这种情况下就像一个子弹),我可以在给定玩家 (x,y) 和鼠标 (x,y) 之间的 target_angle 的情况下射击该对象,例如在子弹更新函数中:
def update
wall_collision
# the angle here is the target angle where point 1 is the player and
# point 2 is the mouse
@x += Math.cos(angle)*speed
@y += Math.sin(angle)*speed
end
是否有类似的方法来计算 3D 中的目标角度并以与我的更新函数类似的方式使用该角度(在 3D 中射击子弹)?如何使这适用于两个 3D 向量 (x, y, z),其中玩家位置 (x,y,z) 和其他一些远离玩家的任意 3d 点。
【问题讨论】:
-
这是 3D 而非 2D @ja72
-
同样的方法。取点积和叉积的大小,放入
atan2()。