Godot Version
4.4.1.stable
Question
More of a general question here: I am trying to figure out how I can detect if a tile with varying sizes (1x1, 2x2, and 3x3, but I eventually want to expand to tiles that have Tetris-like shapes) is outside of a certain area on the screen. If you look at the screenshot below, the dark grey area is where the buttons will be “spawned” and when you click on that button, a tile pops up and follows the mouse. Then you have to place the tile within the turquoise rectangle (which is the CollisionShape2D). I want the tile to highlight red when some of the tile is outside the CollisionShape2D shown in the screenshot. Currently, I have a TileMapLayer that each tile size and I have the code to where the tile “follows” the mouse. I even wrote code where you can rotate the tile. However, I have no idea how to program this to where the game detects if the tile enters the CollisionSpape2D area.
If you want to contain a physics body within a designated area it may be better to use four bodies to represent the outside, you can use WorldBoundry shapes to help draw your rectangle’s border.
1 Like
Gotcha, I’ll try that, thanks!
Gonna post this here, just in case it helps someone in the future.
I did some tinkering and found the following code works. I just defined the ‘borders’ with x and y coordinates in an if statement:
func _process(_delta: float):
set_cell(mouse_position, source_id, Vector2i(x, y))
mouse_position = local_to_map(get_local_mouse_position())
if old_mouse_position != mouse_position:
erase_cell(old_mouse_position)
old_mouse_position = mouse_position
if mouse_position.x < 7 or mouse_position.x > 66 or mouse_position.y < 9 or mouse_position.y > 23:
source_id = -1
else:
source_id = 0
I’m curious how the mouse position itself would let you know…
If your mouse is in the center of a tile then couldn’t part of the tile be outside of the rectangle?
I can update the code so the x and y coordinate limits are updated based on the size of the tile selected.
HI,
I did some more tinkering and decided that using the WorldBoundry shape would make more sense and may even simplify the code. So I apologize for ignoring your advice initially.
I decided to make another Godot project just to test this out, so I don’t accidentally mess up the game I was originally working on. However, as I move the tiles via mouse around the screen, they still move past the boundary I placed. I have the physics layer painted on my tiles that will move around:
Collision Layer and Collision Mask are both set at 1.
I have the WorldBoundary set on the left side of the screen:
But when I move the tile, it still moves past the boundary:
Ah despite writing “tile” and “TileMapLayer” so much I thought you’d be using a different body type. TileMapLayer’s physics layers behave like a static body and doesn’t check for other static bodies.
Tracking the mouse sounds like the best way to go, you can use Marker2Ds to visualize and mark your bounds. If you need collision detection then maybe using an Area2D attached to the mouse to determine if it’s overlapping static bodies.
1 Like