user3787774
user3787774

Reputation: 71

move from one point to another in 3 dimensions (x,y,z) in increments

I have a question. For example i have two points in 3d space:

    float X1 = 100.44f, 
          X1 = 454.33f, 
          Z1 = 344.32f,

          X2 = 120.1f, 
          Y1 = 454.30f, 
          Z2 = 344.32f;

What i want to do is to move from point A (X1,Y1,Z1) to point B(X2,Y2,Z2) in increments Inc and also calculate heading.

I was googling quite a bit to find a function that would do that or something similar.

Thanks to anyone who will provide any help (and sorry for my bad English ... i hope its clear what i want to achieve).

Upvotes: 1

Views: 802

Answers (1)

Avish
Avish

Reputation: 4626

Basically, you want to move along the vector from A to B using linear interpolation:

// Compute vector from A to B by subtracting A from B
var dX = X2-X1;
var dY = Y2-Y1;
var dZ = Z2-Z1;

// Start at A and interpolate along this vector
var steps = 10;
for (var step = 0; step <= steps; step++) 
{
  var factor = step / steps; // runs from 0 to 1 inclusive
  var x = X1 + dX * factor;
  var y = Y1 + dY * factor;
  var z = Z1 + dZ * factor; 
  DoSomethingAt(x, y, z);
}

However, I highly suggest you take the advice in the comments and read up on vector math and interpolation to understand more about what you're trying to do.

Upvotes: 2

Related Questions