简体   繁体   English

将NumPy用于3D矢量

[英]Use NumPy for 3D vectors

I must admit, I'm not so bad in Python, but not so good in math. 我必须承认,我在Python上并不差,但是在数学上却不那么好。 There, I said it. 在那里,我说了。 I'm planning on building a game with a coordinate system in 3D. 我正计划用3D坐标系构建游戏。 Classic, really simple. 经典,真的很简单。 Like my first room would be 0, 0, 0, and the one on the east would be 1, 0, 0. 就像我的第一个房间是0、0、0,而东方的房间是1、0、0。

What would be a bit more difficult is that I would need to search through these coordinates. 更加困难的是,我需要搜索这些坐标。 Find, for instance, all rooms that are around a X,Y,Z coordinate in a 3-rooms radius, let's say. 举例来说,找出以3个房间为半径的X,Y,Z坐标周围的所有房间。 I may use it for pathfinding as well. 我也可以将其用于寻路。 So I was thinking of using NumPy for performance (since I have no idea how many coordinates there will be in the end) and so something quite simple: 所以我在考虑使用NumPy来提高性能(因为我不知道最后会有多少个坐标),所以事情很简单:

import numpy as np
a = np.array([0.0, 0.0, 0.0])
b = np.array([1.0, 0.0, 0.0])

But that's where my meager skills reach a dead end. 但这就是我微薄的技能达到死胡同的地方。 In theory I could substract one to the other to get the absolute distance, but even for that I'm stuck, it seems. 从理论上讲,我可以将两者相减以获得绝对距离,但即使如此,它似乎还是卡住了。 So I'll put my needs here... and hope someone can help me figure things out: 所以我将我的需求放在这里...并希望有人可以帮助我解决问题:

  1. Return the distance between two vectors (as an int, or float would be better). 返回两个向量之间的距离(以int或float更好)。
  2. Look for vectors close to another: the notion of close would be a distance, so in theory, that would mean browsing through all vectors and getting their distance to another one. 寻找接近另一个的向量:接近的概念是一个距离,因此从理论上讲,这意味着浏览所有向量并使其与另一个向量保持距离。 I don't know if it's great in terms of performance though. 我不知道它在性能方面是否很棒。
  3. Obtaining both the 2D direction (in degrees or radiants, between A and B) and the vertical direction (same thing, but using the Z coordinate). 获得2D方向(在A和B之间的度数或辐射度)和垂直方向(相同的东西,但使用Z坐标)。
  4. "Turning" a vector, keeping its distance (norm) but in a different direction, which would imply pivoting around the Z coordinate, if that makes sense. “旋转”矢量,保持其距离(范数)但沿不同的方向,这意味着绕Z轴枢转(如果可行)。 The same thing to pivot around X or Y would be great. 围绕X或Y旋转的同一件事会很棒。
  5. Normalize this vector, so it would be in the same "direction" but with only a distance (norm) of 1 from 0,0,0,. 归一化此向量,因此它将在相同的“方向”上,但与0,0,0,的距离(范数)仅为1。

I'm sorry if that doesn't make much sense. 很抱歉,如果这没有太大意义。 My use case is pretty clear in my head, but not knowing vectors very much, perhaps I'm missing on one or more simple concepts. 我的用例在我的脑海中非常清楚,但是对向量的了解却很少,也许我缺少一个或多个简单的概念。

Thanks for your help! 谢谢你的帮助!

A little bit of linear algebra will go a long way to do most of what you want. 一点线性代数将对您完成大部分所需的工作大有帮助。

  1. Distance between two vectors. 两个向量之间的距离。 You can define c = a- b and then find the magnitude of this difference vector. 您可以定义c = a- b ,然后找到该差矢量的大小。 Finding the magnitude of a vector is simple: mag = np.sqrt(np.dot(c,c)) 查找向量的大小很简单: mag = np.sqrt(np.dot(c,c))

  2. Now that you have a way to calculate a distance between two points, you can do what you suggested, though checking every possible vector pair will be O(N^2). 现在您已经有了计算两个点之间的距离的方法,尽管检查每个可能的向量对都是O(N ^ 2),但是您可以执行建议的操作。

  3. I'm not entirely sure what you mean by 2D direction and vertical direction. 我不太确定2D方向和垂直方向是什么意思。 But finding the angle between two vectors can be done using the fact that A dot B = |A|*|B|*cos(theta), where |A| 但是,可以使用以下事实来找到两个向量之间的夹角:A点B = | A | * | B | * cos(theta),其中| A | is the magnitude of A, and theta is the angle. 是A的大小,θ是角度。 So you could do something like: 因此,您可以执行以下操作:

    magA = np.sqrt(np.dot(A,A)) magB = np.sqrt(np.dot(B,B)) angle = np.arccos(np.dot(A,B)/(magA*magB))

  4. This is what rotation matrices are for. 这就是旋转矩阵的作用。 Given an angle, you can define a rotation matrix, M, and simply take np.dot(M, A) to get your rotated vector. 给定一个角度,您可以定义一个旋转矩阵M,然后简单地使用np.dot(M, A)来获得旋转矢量。

  5. To normalize a vector, you just divide each component by the magnitude. 要归一化向量,只需将每个分量除以幅度即可。 So normA = A / (np.sqrt(np.dot(A,A)) 所以normA = A / (np.sqrt(np.dot(A,A))

This isn't a complete answer, but hopefully it starts you in the right direction. 这不是一个完整的答案,但希望它能引导您朝正确的方向发展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM