Is it possible to have pathfinding on a moving surface?

I’m trying to make a game with boats and I want the sailors to be able to navigate the deck and the hold of the ship, is it possible to use the A* project for this and simply move the whole thing around?

The ship itself will be moving and will also be rotating slightly as waves hit it.

Well. Currently it is possible to move the graphs around, but it is an O(n) operation, so it won’t be very nice to the CPU.

Cheating to the rescue!
What you could do instead is to create a graph for the ship at some position in the world (keep track of it). Then right before you request a path, is to multiply the target and start points with a matrix which translates the on-ship-position to on-graph-position. This way, you don’t need to move the graph around, you simply do everything in ship-space instead of in world-space.

`
//Assume this is attached to the ship root at a position so that it would be the center of a grid graph
//class: ShipData

public static Matrix4x4 shipMatrix;

public void Update () {
//Assume first graph
GridGraph graph = AstarPath.active.astarData.graphs[0] as GridGraph;
Matrix4x4 m2 = Matrix4x4.TRS (graph.center,Quaternion.identity,Vector3.one);
shipMatrix = m2 * transform.worldToLocalMatrix; //hm, maybe switch order
}
`

`
//Movement script

public void StartPath() {
// To graph space
Matrix4x4 m = ShipData.shipMatrix;
seeker.StartPath (m.MultiplyPoint(transform.position),m.MultiplyPoint(target.position));
}

public void Update () {

Vector3 targetPoint = p.vectorPath[…];

//Revert to ship-space
Matrix4x4 m = ShipData.shipMatrix.inverse;
targetPoint = m.MultiplyPoint(targetPoint);
}`

I haven’t tested the above code, but hopefully you will get a grasp on the main idea.

1 Like