Zylann / godot_voxel

Voxel module for Godot Engine
MIT License
2.72k stars 251 forks source link

VoxelAStarGrid3D seems not to work with negatives Vector3 #576

Closed MathieuRegis closed 1 year ago

MathieuRegis commented 1 year ago

Hello,

I'm trying to make VoxelAStarGrid3D works, their is not much doc on it so I'm testing the trial and error way. Feel free to correct me if I'm wrong somewhere or if I'm not using VoxelAStarGrid3D the way it's suppose to be used. For this I'm testing thing in the blocky_game example.

So I understood that I needed to set terrain and region. Terrain is the generated VoxelTerrain. Region is an AABB to define the boundaries where AStar looks for its algorithm. I'm using the find_path method that return Array[Vector3i] if a path has been found, else it return an empty Array

Describe the bug When I'm using negative Vector3, to find path in the -x or -z axis of the VoxelTerrain, it's doesn't find any path.

To Reproduce Here is the code I'm using : this works using positives values :

...
var _agrid: VoxelAStarGrid3D = null

func _ready():
    _agrid = VoxelAStarGrid3D.new()
    # set _terrain that is the main VoxelTerrain
    _agrid.set_terrain(_terrain)

    # little aabb box of 20 by 10 by 20
    var aabb := AABB(Vector3(0, 0, 0), Vector3(20, 10, 20)) 
    _agrid.set_region(aabb)

# I'm using _unhandled_input just to trigger the find_path
func _unhandled_input(event: InputEvent):
    if event is InputEventKey:
        if event.pressed:
            match event.keycode:
                KEY_UP:
                    # y = 6 because my terrain ground is at y = 5
                    var path = _agrid.find_path(Vector3(1,6,1), Vector3(6,6,3)) 
                    print('path ', path)

I got positive values, it works, the print in console : path [(1, 6, 1), (2, 6, 2), (3, 6, 3), (4, 6, 3), (5, 6, 3)]

But if I change the Vectors for : _agrid.find_path(Vector3(-1,6,-1), Vector3(-6,6,-3)), it return an empty array, witch I suppose mean that their is no path found. I tried very simple path like _agrid.find_path(Vector3(-1,6,-1), Vector3(-1,6,-2)), the print always return path [].

I made sure the terrain is flat and with no obstacle (since I'm using the blocky_game example, the grass for example are counted as obstacles).

Expected behavior The path should be found with the list of negative Vector3

Environment

Thanks for any help, fix or explanation !

Zylann commented 1 year ago

image

Works in my testing scene. Do you have a test project?

Actually, if you do a search in the negatives, your region must include those positions:

    # little aabb box of 20 by 10 by 20
    var aabb := AABB(Vector3(0, 0, 0), Vector3(20, 10, 20)) 
    _agrid.set_region(aabb)

This will not allow pathfinding in the negative.

MathieuRegis commented 1 year ago

Alright, Thanks for the answer,

After some testing I figure it out that position in AABB was not the center of the box but the corner of the box. So my AABB config was wrong as you mentioned.

I tested and it works with negatives values indeed. Also works with positives and a mix of negatives and positives values, very nice !

Quick question, how do you get the blue path to appear in your screen for debug ? is this debug_get_visited_positions method ?

Thanks !

Zylann commented 1 year ago

Quick question, how do you get the blue path to appear in your screen for debug ? is this debug_get_visited_positions method ?

The blue cubes are points of the returned path. I draw them using a MultiMeshInstance. debug_get_visited_positions is shown in my screenshot as transparent yellow cubes.

MathieuRegis commented 1 year ago

Tanks you very much for the answers !