简体   繁体   中英

Use NumPy for 3D vectors

I must admit, I'm not so bad in Python, but not so good in math. There, I said it. I'm planning on building a game with a coordinate system in 3D. Classic, really simple. Like my first room would be 0, 0, 0, and the one on the east would be 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. 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:

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).
  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).
  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. The same thing to pivot around X or Y would be great.
  5. Normalize this vector, so it would be in the same "direction" but with only a distance (norm) of 1 from 0,0,0,.

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. Finding the magnitude of a vector is simple: 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).

  3. I'm not entirely sure what you mean by 2D direction and vertical direction. But finding the angle between two vectors can be done using the fact that A dot B = |A|*|B|*cos(theta), where |A| is the magnitude of A, and theta is the angle. 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.

  5. To normalize a vector, you just divide each component by the magnitude. So normA = A / (np.sqrt(np.dot(A,A))

This isn't a complete answer, but hopefully it starts you in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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