简体   繁体   中英

Algorithm for finding path to point

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

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.

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

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))

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