Help with checking collision using the tileset physics polygon system

Just starting out with the engine, sorry for the beginner question. I’m struggling to wrap my head around all the features of the tilemap node in version 4.2.1.

I’m working on a little JRPG type of game to learn the ropes and I’ve managed to set up a tilemap where a few of the tiles have physics collision boxes set up, but I can’t figure out how to actually check for collision. My character controller works by setting a target position when you hit a movement key and then gradually moving the character towards it over 16 frames, is there a way to check if my target position collides with a physics polygon attached to a tile in a tilemap? Here’s the current code for the character controller:

func check_input():
if target_pos == position:
	if Input.is_action_pressed("move_up"):
		target_pos.y -= 64
	if Input.is_action_pressed("move_down"):
		target_pos.y += 64
	if Input.is_action_pressed("move_left"):
		target_pos.x -= 64
	if Input.is_action_pressed("move_right"):
		target_pos.x += 64

else:
	if target_pos.y < position.y:
		position.y -= speed
	elif target_pos.y > position.y:
		position.y += speed
	
	if target_pos.x < position.x:
		position.x -= speed
	elif target_pos.x > position.x:
		position.x += speed

Here’s the documentation pages to know what a kinematic body is Kinematic character (2D) — Godot Engine (stable) documentation in English and how to use a CharacterBody2D Using CharacterBody2D/3D — Godot Engine (stable) documentation in English

Although, if you are a new user to the engine, it’s better to start with the Getting Started part of the documentation here Introduction — Godot Engine (stable) documentation in English it will teach you how to use the editor, the concepts of Godot, and how to structure 2 small games one in 2D and another in 3D.

1 Like

Thank you very much for the response. I have read and followed the Getting Started part of the documentation, expected that would teach me enough to put something small together, but there’s quite a few important features it doesn’t touch on.

The 2 articles you linked certainly could be helpful, but I’m trying to do grid based movement, so the more “dynamic” collision offered by those isn’t really fit for my project. I’m more specifically looking for a feature like the SFML FloatRect’s .contains(Vector2f) function, where I can just do a quick check to see if a certain point is within the collision detection for a certain type of tile in a tilemap. I’ve read a lot of documentation, but I can’t seem to find any such feature anywhere. Or maybe I’m just overlooking a simple solution somewhere within these documents.

yes, there’s a way

		for i in get_slide_collision_count():
			var collision = get_slide_collision(i)
			if collision.get_collider() is TileMap:
				var tile_data=collision.get_collider().get_cell_tile_data(1,collision.get_collider().get_coords_for_body_rid(collision.get_collider_rid()))
				if tile_data.get_custom_data_by_layer_id(0)=="wall":
					time=10.0

put this on _physics_process block on where your characterbody2d’s script

the wall in tileset:

1 Like

Correct me if I’m wrong, but looking at the code, it looks to me like it just checks for collision as my object is already in motion. What I’m trying to do is scan to see if a standard Vector2 coordinate is inside of a physics polygon, to stop my character from moving if he would be moving into a tile where there’s collision. Thank you for taking the time to reply.

what does this mean?

when the character is moving, it should be able to detect the collision of tilemap’s physics layer that you have put in a tile
i dont see why cant it collide even without the code above

1 Like

I want my guy to move on a grid, like in say, the older Pokemon games, but the standard collision built into the CharacterBody2D is made for smoother movement, more like you see in platformers or something like the 2D Zelda games. My method of doing this (which could be silly and inefficient, I’m still working on building experience as a developer) is to set a target location and then gradually move my player character towards it. But right now, that target location will be set regardless of collision because I don’t have a way to check if the target location is inside a wall.

so rather than the characterbody2d to be able to detect the surrounding after colliding with it, you will want raycast 2d pointing on 4 direction that will determine if the input key is pressable or not

1 Like

Good solution, thanks a lot! I’ll look into trying that.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.