Local vs. global position

I am really enjoying this course, the Barbarian Blaster section in particular. But I was wondering why Bram kept referring to the position for the turret as “global” when we obtained it from a function called “map_to_local”.

It is in fact a local position relative to the GridMap’s transform. The code from the video would stop working if the GridMap’s transform was ever different from the world origin.

I would suggest a solution that makes sure local and global position are converted into each other properly (could be made clearer but longer with multiple variables named to emphasize whether it’s local or global). It’s simple:

  • When resolving our raycast hit, convert it to a local position of the GridMap first:
    collision_point = gridmap.to_local(collision_point)
  • For getting the desired turret position globally:
    tile_position = gridmap.to_global(tile_position)

Quite handy - we can call those methods for any of our Node3D-based objects to juggle transform data: Node3D#methods

Concerns lecture Picking Turret Positions from the Complete Godot 3D course.

3 Likes

You saved me! I was getting all sorts of strange behaviors until I made this change! :slight_smile:

1 Like

Welcome to the community!

Thanks for the feedback! I am glad the post is helpful. :relaxed:

Privacy & Terms