Eegxeta
Eegxeta

Reputation: 115

python find vector direction between two 3D points

I'm trying to calculate the direction of a 3D vector starting at point (x, y, z) and ending at point (a, b, c) for the navigation in my spaceship game, but I have been unable to find anything helpful. So far I have tried using two circles, one for figure out x and y and another for z, to figure it out and the code only works if the two vector's distances are very similar.

Here is what I'm using:

def setcourse(self, destination):
    x1, y1, z1 = self.coords
    x2, y2, z2 = destination

    dx = x2 - x1
    dy = y2 - y1
    dz = z2 - z1

    self.heading = math.atan2(dy, dx)
    self.heading2 = math.atan2(dz, dy)

    self.distance = int(math.sqrt((dx) ** 2 + (dy) ** 2))
    self.distance2 = int(math.sqrt((dy) ** 2 + (dz) ** 2))

def move(self):
    if self.distance > 0 and self.distance2 > 0:
        if self.atwarp == True:
            x, y, z = self.coords
            x += math.cos(self.heading) * self.speed
            y += math.sin(self.heading) * self.speed
            z += math.sin(self.heading2) * self.speed

            self.coords = (x, y, z)
            print(str(self.coords))
            self.distance -= self.speed
            self.distance2 -= self.speed

    elif self.distance <= 0 and self.distance2 <= 0 and self.atwarp == True:
        self.atwarp = False
        self.speed = 0
        print("Reached Destination")

    else:
        self.atwarp = False

I'm not sure how much of it is a math error and how much is a programming one, but the z winds up way off and I'm not sure how to go about fixing it. No matter what I do the z is always off if its input more than slightly different from the others.

Here is examples starting from (0, 0, 0). I'm trying to get the output to be similar if not the same as the input.

Input: (100, 200, -200)

Vector1 heading in radians: 1.1071487177940904 Vector2 heading: 2.356194490192345

Vector1 distance: 223 Vector2 distance: 282

Output: (99.7286317964909, 199.4572635929818, 157.68481220460077) The x and y are fine, but the z is off.

Input: (-235, 634, -21)

Vector1 heading in radians: 1.9257588105240444 Vector2 heading: 1.6039072496758664

Vector1 distance: 676 Vector2 distance: 634

Output: (-220.3499891866359, 594.4761410396925, 633.6524941214135) The z off.

Upvotes: 1

Views: 9800

Answers (1)

jcoppens
jcoppens

Reputation: 5440

The direction of the movement is the trio dx, dy, dz you calculated. This vector is not pure: it contains distance and direction. If you want direction alone, you have to normalize this:

The distance is sqrt(dx^2 + dy^2 + dz^2).

For the normalized direction, you divide each dx, dy, and dz by this number.

If you want to move in that direction, the new position is the old position plus the the direction vector times the distance you want to travel:

newpos = oldpos + dist * dirvector

I'm not sure what you mean by input: (100, 200, -200) if that is the direction, your direction vector would be 300 long, and the actual direction vector is 100/300, 200/300, and -200/300 (so 0.333, 0.667 and -0.667)

If you want to travel 500 along that direction, the new position is 0+166.67, 0+333.33, and 0-333.33

Upvotes: 6

Related Questions