views:

122

answers:

3

I'm not sure if I worded this properly, but basically I have an object at point X,Y and I want an algorithm that can get this point to X',Y' but like show its route so I can animate it. I'm building a tile game and when the game starts I want the tiles to magically place themselves into a nice 2d array. So I will generate a random coordinate and then tell it to go to its goal within 50 frames. Thanks

A: 

You'll want to use the A-star algorithm. See here

Alex Deem
+4  A: 

It sounds like you just want a linear transformation. Like

Xt = (((X'-X)/T)*t)+X, Yt = (((Y'-Y)/T)*t)+Y

Or in English the coordinate for a tile at time t is t/total_frames length along it's path. A* is overkill if you aren't trying to avoid obstacles.

Todd Gardner
In vector notation, `y = a+t*b`, where a, b and y are vectors and t is a scalar. a is a vector from the origin to any point on the line and b is parallel to the line. If a and c are two points on the line, `b=c-a` (which you see in Todd's formula, though his involves differentials). An alternate form is `y = a*t+c*(1-t)`.
outis
You can get more complex curves using polynomials: `y(t) = a*t**2 + b*t + c`, `y(t) = a*t**3 + b*t**2 + c*t+d`, where the coefficients are vectors. Combine the polynomials in a piecewise fashion and you have splines. Use two (or more) scalar variables and you get surfaces (including > 2 dimensional surfaces).
outis
A: 

So you want to linearly interpolate from (X, Y) to (X', Y') in 50 steps. First define the total distance to move in each direction:

dx = X' - X
dy = Y' - Y

assuming n = 50 frames, at the i-th frame (so with i in range [0, n-1]) put the tile at coordinate (x, y) defined as:

x = X + dx * (i / (n-1))
y = Y + dy * (i / (n-1))
catchmeifyoutry