How do you script navigating and checking for collisions for an AI in a less manual way?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dadango
:warning: Old Version Published before Godot 3 was released.

This is how my code looks right now. It does’nt work at all.
I have a simple pathfinding system that says:
Is it my turn? if yes;
am i NOT next to the players pos and do i have “movement” left? if yes;

the “enemy_alternative_route()” func leads to the above mentioned code:

which has an IF or ELIF statement for each four directions (only working on 1 for now, for testing)
Here i make it move it’s directional raycasts in an alternative direction (up/down instead of left in this case) and see if it will collide with anything if it tries moving (left) from there, and if so start moving towards that point instead.
This would allow it to overcome 1 “tile” worth of obstacles and i would be able to expand this formula up to any number of tiles… i think.

But this doesn’t work, it moves the raycast and it obviusly collides with the obstacle, yet it prints “false” when i ask if it has collided, and as such only takes the “north” direction and never the “south” even if the path is shorter.

I bet there is a much simpler way to do this… If so, I would LOVE to hear it.
Navmesh or polygoninstance and nav2d takes the corners and not the centers of my tiles, so I can’t make that work either. (Tiles are 50x50pixels atm)

Any help is appreciated and congratz if you actually read all this nonsense and made sense of it :slight_smile:
Cheers

I don’t get what you mean by “Navigation2D takes the corners and not the centers”, because that’s actually the thing you need, as well as a Tilemap if you use tiles.

Zylann | 2016-11-26 16:42

Wow yeah reading back again that was horribly worded.
As i’ve come to understand Navigation 2d, from “Gamesfromscratch”? maybe?, it takes the absolute shortest route, meaning if I made a polygon of the walkable area, the navigation would cut corners near obstacles. It wouldn’t move my node through each tile’s center, onto the next and so on but rather move through the entire walkable area with the shortest confirmed route.
http://prntscr.com/dc26gh
^from a video explaining 2d nav (https://www.youtube.com/watch?v=b7rhFPcj230)
It does seem to take into account the size of the node moving around nor the size of the “hallways”, rather just whatever point to point path is easiest.
I hope that made more sense

Dadango | 2016-11-26 16:57

I understand the size of the node may not be taken into account. There are several ways of dealing with that:

  • Run your own machinery so you can process the path in order to make it pass through closest waypoints allowing your node to pass through them (so running raycasts along the path points to adjust its positions toward walls)
  • Have another layer where colliders are bigger (but ignored for physical objects) so the path will be generated as if it had to pass through narrower areas, forcing the path to not cut the real corners
  • Manually place waypoints on your map and use the AStar utility class

I didn’t investigated a lot about this issue but you can do those things to improve path computation. I don’t remember having this problem that much when testing it, I would be curious to see a screenshot of your level where you draw the path that Godot gives you (using _draw())

Zylann | 2016-11-26 17:24

Astar requires a newer update of godot, 2,2 i believe, which i can’t find anywhere sadly. Great idea with the extra collider layer, I thought of doing it in another way, but yours might actually work :smiley:
Manually placing waypoints, is not something I would mind doing, but I fail to see how it will work dynamically, differently than a 2dpath to follow.

Ty for the quick comments :slight_smile:
I’ll attempt to make a quick 2d nav and dump a screenshot here

Dadango | 2016-11-26 17:33

Here is a fresh level created for this thresh:
Screenshot by Lightshot (sick Paint.net skills I know)

Here is the code:
Screenshot by Lightshot

Also there is another script, which changes “turn” to 2, and sets “PC_pos” to the blue guys position

The red guy is the start pos (Sprite)
As i play the scene:
Screenshot by Lightshot
It marks out the corners of the obstackle so if i move the guy, it will NOT pass through the middle of the open tile, but rather just barely skip past the obstacles corner.
For some reason it instantly jumps to the points, I’ll have it move 1 tile at a time, but again, I don’t know how to make it through the center of the tiles, except maybe with your idea of making a new coll layer :slight_smile:

Here I’ve tried drawing out the path I would like it to take in lightshot:
Screenshot by Lightshot

Dadango | 2016-11-26 18:02

What do you get if you center points of the path using tilemap’s point = world_to_map(point) followed by point = map_to_world(point) + tile_size/2.0?

Zylann | 2016-11-26 19:40

I don’t understand. How would you get “point”? Would that be the individual parts of the path? path[i]?
I’m guessing that would look like this in the code:
Screenshot by Lightshot
I have no clue. This ends up like this:
Screenshot by Lightshot

Dadango | 2016-11-26 20:15

You missed half of the formula:

var tilemap = get_parent().get_node("tilemap")
pos = tilemap.map_to_world(tilemap.world_to_map(path[i])) + tilemap.get_cell_size()/2.0
draw_circle(pos, 10, Color(1,1,1))

Zylann | 2016-11-26 22:23

Screenshot by Lightshot
You are a god Zylann! Thank you so much, this has saved my week!
Now I just need to make it follow that path for 50 pixels a turn and I can get rid of my 80 lines of shit script xD

Thanks a bunch mate :slight_smile:
Really, taking your time out to teach a random stranger to do something is quite something ^^

Dadango | 2016-11-26 22:50

I was only 50% sure that this would work, but it does so I’m happy too^^

Zylann | 2016-11-26 22:56