CollisionPolygon2D and mouse_enter/exited fired with offset

Godot Version

4.6

Question

Hi!
I'm new here, and it's my first post!

I have a turn-based hexagonal-grid-based game.

I have a Tile Scene and a Dungeon scene where I spawn the tiles procedurally.

I know I can convert pixel screen coordinates to tile position.

That said I figured I could make an even simpler system:

  1. Add a CollisionPolygon2D to my Tile scene
  2. I them just set the visibility of a HighlightSprite2D on/off according to mouse.

The problem: the mouse “collisions” seems to be happening with an offset.
It’s like the collision shape was actually placed further down.
See animated GIF bellow and follow the mouse cursor:

Godot Mouse Signals Offset

Tile scene consists of a root Area2D node. It has

  1. Polygon2D (just a white hexagon used for debugging, ignore it).
  2. BaseSprite2D - The actual pixel art sprite of the tile
  3. HighlightSprite2D - The highlight that is shown on mouse over
  4. Camera 2D - just a camera centered on the tile
  5. CollisionPolygon2D - Just a collision shape in the shape of the Hexagon outline.

Here is the CollisionPolygon2D, for reference:

(It aligns perfectly with the Hexagon outline)

The callbacks are fairly simple:

Tile_Scene.gd

extends Area2D

func _on_mouse_entered() -> void:
	set_highlight(true)


func _on_mouse_exited() -> void:
	set_highlight(false)


func set_highlight(visible: bool) -> void:
	var h = get_node("HighlightSprite2D") as Node2D
	h.visible = visible

Why does it happen? I know i could just move the CollisionPolygon2D up to make a workaround but I want to:

  1. Do it the correct, cleaner way
  2. Understand why it happens.

My guess is that something has its Transform position off from where you think it is. Based on your node tree, I would see if you CollisionPolygon2D has a modified Transform. If it does, zero it out and then update the points in the polygon.

Hi!

Both Tile and CollisionPolygon2D transforms are at the Defaults.

(The pixel art tile is a 128x128 sprite and I’ve drawn the tile centered in the canvas intentionally so it’s already imported centered without the need for transforms).

When selecting the CollisionPolygon2D in the editor the outline it shows is bigger than the shape itself. Is that normal or might it be a clue to what is wrong?

I’m a bit lost. I did found the culprit, but I don’t understand why it’s causing this behavior.

I was designing at Full HD and then changed to 4k. Then I’ve set scale to 2 to get the same “real” sizes for the sprites. Then Scale Mode to Integer, since it is the best option for pixel art.

I did mess around with the settings and then discovered:

If “Stretch->Mode” is not disable (set to “canvas_items” or “viewport”) and Scale Mode to Integer, then that happens. Even if scale is set to 1 (one).

Why does that happen? Is that a bug?

Glad you found it. I do not know if that is a bug TBH. I’d say log it and see what they say.

I’ve opened an issue on github. It contains the results of A LOT of testing (81 different variations on configurations) and an MRP.

1 Like