Godot: CollisionShape2D not work in tilemap

Created on 29 Jan 2016  路  23Comments  路  Source: godotengine/godot

Hi all,

I have encountered a weird bug when I am using a sprite with collision shape in a tile set.

The node tree is Node2D -> Sprite -> StaticBody2D -> CollisionShape2D, and the sprite and collision shape's offsets are modified, as the following screenshot shows.

image

Then I convert the scene to a tile set and use this tile in a tile map, the collision shape just goes to a wrong position when I play it.

image

And in this situation, the debug option "visible collision shapes" won't show the collision shape, even at a wrong position.

bug editor

Most helpful comment

Hey guys, so this happened to me. I fixed it by remaking tscn file. Take your time when you make this file and the components within it. If you have a Sprite -> Static body 2D -> CollisionShape2D setup then this should fix your issues. Do not change the shape or edit the size of the static body. This is what caused me problems. Follow this tutorial and you should be fine, it helped me https://www.youtube.com/watch?v=BfQGXtlmE7k

All 23 comments

Did you scale the collision shape by any chance?

Also, make sure your StaticBody2D's position is 0,0

@akien-mga No, I only modified the position of the collision shape.

@bojidar-bg Yes, it's 0,0.

I also noticed that it works fine if I don't modify the sprite's offset.

I'm now using the the new 2.0 beta version. The problem can be reproduced in 2.0 alpha version too.

I am not sure, but this may be related to #2231

make an example scene that fails so i can check it out, can't tell much from the description

I have one example project where this happens.
Please clone or download the following project:
https://github.com/genete/JetPac
Now open Godot editor and open the tileset_edit.tscn scene. Notice that it has the sprites offset and the collision shapes offset too in order to match the sprites boundaries.

Go to Convert to->Tileset and choose the filename 'tileset.res'. Overwrite it to be sure that it is the result of the conversion and not a merge of the previous one.

Now open scene level.tscn and run the scene with debug option to visible collision shapes and you'll see what's happening.

The TileSet.res Shape Offsets hasn't been properly converted and are all them -0, -0 instead of 8,8 what was the value at tileset_edit.tscn

Hey folks, this looks like it's still happening in 2.1.

Basically, if you have a CollisionShape2D as a child under a StaticBody2D and change its position down a little bit, and then once you convert the tileset (@genete explained) it will revert back to default and not take into account offsets.

This makes using segmented CollisionPolygon2D for tilemap collisions the only way. I'm not sure if this is intended but I think the problem is when converting the scene into a tileset. For example, in the 2d Lightning Isometric example, there is a CollisionShape2D that is used, but it's a imported scene, not from a converted tilemap resource. Maybe the person who made that example knew that was the only way for the offsets to work :P

This is still a problem in v2.1.3. CollisionPolygon2D shapes work correctly, but CollisionShape2D rectangles become misaligned when converted to a tilemap.

Check out a simple project to demonstrate the problem: https://www.dropbox.com/s/f4galxinppcdev5/CollisionShapeBug.zip?dl=0. The scene contains a tilemap that is made from 2 tiles: one is shaped using CollisionPolygon2D and the other uses a CollisionShape2D rectangle. NB that the balls collision is misaligned in the CollisionShape2D tiles.

This problem still happens in Godot 3.0.

Indeed I have this problem in Godot 3.0 too

I have just come across this myself in Godot 3.0

i have tried recreating with colisionpolygon2D rather than collisionshape2d, and the same thing occurs.
looking at the polygon indices shows that the values in there do not match the size displayed visually. when i set them to the actual size of the sprite, the collider is the right size.

Also getting this issue in v3.0.2

It seems that if you do not offset the StaticBody2D and only the CollisionShape2D, collision works fine. For reference I was following this video: https://youtu.be/BfQGXtlmE7k?t=6m30s

@AVenzor I had this issue as well. Your solution worked when using a CollisionPolygon2D and CollisionShape2D.

Using 3.0.2 on macOS 10.12.6

I have a similar issue - not sure if it's exactly the same. I've made a simple tile map according to instructions http://docs.godotengine.org/en/3.0/tutorials/2d/using_tilemaps.html and CollisionShape2D doesn't have the right size:

image

The one with the correct size is a polygon shape, but I'd prefer not to use it for simplicity's and of course performance's sake.

The error seems to be in the Export to Tileset, because when I look at the exported tres it says:

[sub_resource type="RectangleShape2D" id=1]

custom_solver_bias = 0.0
extents = Vector2( 10, 10 )

Even though the tiles are 64x64.

The tile refers to it like this:


2/shapes = [ {
"autotile_coord": Vector2( 0, 0 ),
"one_way": false,
"shape": SubResource( 1 ),
"shape_transform": Transform2D( 3.2, 0, 0, 3.2, 32, 32 )

I have 1,1 scale everywhere, no offsets as far as I can see.

Hey guys, so this happened to me. I fixed it by remaking tscn file. Take your time when you make this file and the components within it. If you have a Sprite -> Static body 2D -> CollisionShape2D setup then this should fix your issues. Do not change the shape or edit the size of the static body. This is what caused me problems. Follow this tutorial and you should be fine, it helped me https://www.youtube.com/watch?v=BfQGXtlmE7k

Having the same problem on v3.0.2

@SoSubtle solution works fine.

Would be nice if someone gives a 3.0.x or 3.1 tscn which doesn't give the expected results. Then it might be easier to look into it.

Not sure if that's still something that's needed, @bojidar-bg , but I put up a very small project that demonstrates the problem in 3.0.2.stable.official:

https://github.com/z4rdoz/Godot-TilemapBug

It certainly threw me for a loop. Fortunately, as has been said, @SoSubtle 's solution is easy enough to remember.

Hope that proves helpful,

Just a note: Scaling StaticBody2D or CollisionShape2D nodes is highly discouraged, no matter if it is buggy or not. The reason is that the physics engine tends to have slight problems with precision when interacting with scaled shapes.


@z4rdoz Thanks for the example project! :+1:

I tested it on master (e19da5ab6ac1acd965842c5deaa329dad8ea3299), and regenerating the tileset there produced a proper tileset.
So, I guess I fixed it in #19343 properly; the fix should become available in 3.1 or perhaps 3.0.4.

I am going to close this issue for now, since it seems to have been fixed. If someone is still able to replicate this bug on master, feel free to open a new issue or comment here.

FWIW the issue still occurs on 3.0.4

I thought I had this issue, but my TileMap's collision_layer was different from the tile's collision_layer, and it's the TileMap's that matter in the end.

If you're here, check your TileMap's collision_layer. :wink:

Was this page helpful?
0 / 5 - 0 ratings