0 votes

I'm playing around with tilemaps to create levels and have run into a problem with collisions on tiles. I've created the tiles with a sprite/staticBody2D/CollisionPolygon2D, and converted to a tres file, before creating the level itself in a separate scene (see linked image for reference).


So far, so good, however, when I get to instancing the scene in main, my player's raycast seems only to collide with certain blocks. When I go back to the source and check the collisions, the ones that are colliding with the raycast all have the same colour (pink/purple) at the top of the tile (again, see image - blocks 1,4 and 8 going left to right, top to bottom). I also have a 'bad polygon' error, but I can't find anything on that particular error message.

My question is, do these colours actually mean anything, and how can I more carefully determine what ones will come out when i'm creating CollisionPolygons? Alternatively, if i'm barking up the wrong tree could anyone suggest where i'm going wrong (already tried playing about with the RayCast2D length, and have double-checked layer masks).

Thanks in advance!

EDIT - My raycast was starting underneath the block it was trying to detect - by shortening and moving up slightly it worked almost perfectly.

in Engine by (30 points)
edited by

as far as I'm concerned the colours are there to separate the polygons that create the bigger one... I have no idea what is wrong with your raycast even tho when i started using the godot engine i noticed a bug that occurred when using the tilemaps, sometimes the collision shapes would rotate themself... try using nodes instead of a tilesets and try making the convex polygon collision shape as simple as possible("round" corners where you dont need that much of accuracy)

thats all i can tell you

Appreciate the response - I've started changing what I can to CollisionShape2Ds (so that I can more easily make and edit levels - using individual nodes I think would get too messy) and they seem to work fine.

That's another reason why I can't make sense of it - of all of them, I thought the flat edge CollisionPolygon2Ds would work perfectly!

Yeah, colors are just showing what the triangulated polygon look like, nothing else. Which Godot version are you using?

Not sure what would be wrong with tilemaps and CollisionPolygon2D. My game heavily relies on both (same as you, really), with rotations, and mixing with nodes and tiles etc. And I never had any problem with raycasts (my gameplay is exclusively based on these!).

One thing though, I usually simplify the shape when making these, mostly because the game doesn't need such a high precision. Also, I use pixel snapping to make sure tile collisions connect perfectly. Even if you don't have a pixel-art game, it's a good idea to snap borders to round coordinates so that they perfectly match the grid.
I would post a screenshot for comparison if I could but I'm not at home right now.

Is your raycast starting above the ground or ON the ground? In the latter case, could be that you are missing it because of float precision / lack of margin?

'bad polygon' might be because one of your polygon has an overlap, or its triangulation failed somehow (which could come up with holes in it).

Yeah... feel pretty stupid for framing the question that way now. I'm using the latest stable release -

Thanks for your comments though - I've simplified all the shapes but the raycast still doesn't seem to detect any CollisionPolygon2D, but works perfectly with any that have a CollisionShape2D. I used pixel snap on everything (but on a grid of 1x1 so it was far more complicated than it needed to be).

However, you seem to have diagnosed the problem - my raycast was always starting below the ground so I guess was casting within a single element and thus it wasn't detecting a collision. I think the character I have makes it really awkward (little legs leave little room to have a raycast apart from the body), but with a little tweaking I got it working on a tile that previously didn't. Thank you! A+

Still a minor niggle though - when the character first lands I can see that, when printing is_colliding(), it flits between true and false for a few milliseconds, meaning i'm not able to jump away from that block straight away all the time. Any experience with this when creating your game? No worries if not - sure i'll solve it with a bit of tweaking.

Thanks again!

If you experience "oscillation" when landing, you should investigate your physics: do you handle bouncing? Are you using a rigid body or kinematic? I would probably just capture the first "landed" state and force the character in a "landed" state where it has no way of getting off the ground, unless jumping or falling (like, stop applying gravity). Just a suggestion though, game physics are often different^^

There's no bounce on any of the physics layers (character is kinematicBody2D), but when I make collision shapes visible and run the game I can see that the rays all go up and down a few times.

Forcing a state sounds like a good idea though - I'll implement over the weekend. Thanks again for the help!

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.