A
AndriySu
posted to: Creating the Unit

Path2D + PathFollow2D use questions

Hello! I am playing with RTS mechanics and try to better understand how Path2D and PathFollow2D work together and what are the proper practices here.

I have a simple RTS style project with AStar2D and TileMaps, trying to implement the following interaction:

- Unit - Path2D node, has PathFollow2D as a child, latter has sprites, hitboxes etc.

- TileMap

Unit script:

1. captures get_global_mouse_position

2. calls TileMap AStar pathfinding function, passes global coordinates of unit and mouse click

3. receives from AStar pathfinding global coordinates (via built-in map_to_world() conversion)

4. converts received from AStar Vector2 points to make them ‘local’ to the Unit (adjust_path_to_local(), see below)

5. adds converted in p4 above points to ‘curve’ property of Path2D

6. calls offset of the child PathFollow2D node

To make an easier fit with other code I wanted to make the Unit’s parent node to move, not only PathFollow2D and its children.

First problem I’ve faced - if I place/spawn units not at (0,0) position - everything breaks as I need to adjust/convert the global coordinates from pathfinding to local coordinates for PathFollow2D.

I made the following convertor, which receive array from Astar pathfinding and convert them to local to the node coordinates. I don’t remember such conversions were used in the JRPG movement series. What have I missed?

```

func adjust_path_to_local(path: PoolVector2Array) -> Curve2D:

    var out = Curve2D.new()

    for i in path.size():

       out.add_point(path[i] - position)

    return out

```

Second question - are there another ways to move Path2D to the target destination after PathFollow2D has moved? I’m concerned that such ‘jump’ may affect other mechanics - eg the jump of the parent node will move for a moment its children and may trigger its area nodes.

Third question - am I right that Tactical Space Combat doesn't change position of the Path2D nodes at all?

  • Nathan Lovato replied

    In short, I think the problem you're having with positions is that you're trying to move the parent of the path node rather than the children.

    If you move the children, their position will be relative to the path node. In our code, we move the path node to the position corresponding to the final grid cell our unit traveled. In other words, our Path2D node is always placed at the start of the path we want the unit to follow.

    If you change the setup and the way the system works, you need to change the calculations accordingly. And you'll have to work against the path follow node if you're to move its parent, as it's not designed for that.

    Are there another ways to move Path2D to the target destination after PathFollow2D has moved? I’m concerned that such ‘jump’ may affect other mechanics - eg the jump of the parent node will move for a moment its children and may trigger its area nodes.

    In this project, there are no collision shapes so there's no possibility of triggering area nodes. Then I don't understand the concern: did you try using area nodes and noticed that with a jump like this they weren't triggered by the unit entering the area? I don't think it should be a problem but even if it were you could just put the collision shape on the sprite which moves continuously.

    Third question - am I right that Tactical Space Combat doesn't change position of the Path2D nodes at all?

    Yes, it's quite possible. You could use this approach this model where you constantly offset the position of units as they move instead of moving the Path2D node itself. You can use whatever feels most comfortable for you and your project. Depending on your needs or taste, you can nudge the code to offset the units to accommodate the rest of your codebase.