How to solve the jiggle of a RigidBody2D in a TileMap?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mateusak
:warning: Old Version Published before Godot 3 was released.

When a force is applied to a RigidBody2D with a square collider standing in a TileMap platform, it’ll sometimes get stuck within the end and beginning of the tiles.

That happens in other engines as well, and the solution is rather simple: use a capsule shape. But that doesn’t seem to solve it very well here, as it will jump a little instead of getting stuck when the force is applied.

How can I prevent this bump between tiles? If I can’t, how can I prevent the jump? I can’t use Kinematics in my game.

:bust_in_silhouette: Reply From: mateusak

Okay, so it seems there’s this awesome collision shape named RayCast2D which prevents this from happening. It’s very simple, put a RayCast2D collision shape on the feet of your player and you’re set.

This ended my 3 day struggle to find a solution to this problem. Thank you!

Taylor Hansen | 2017-05-05 03:09

Man, this doesn’t work for me. My character still gets stuck on the platform.

inkmotor | 2017-05-09 00:20

It could help if you included the player.tscn file. Plus this is mostly for rigid bodies which handle all that physics and gravity stuff for you.

Taylor Hansen | 2017-05-09 00:25

Hi Taylor,

thank you for your time. Here’s the tscn

inkmotor | 2017-05-09 00:29

It looks like you didn’t actually do what was suggested here :stuck_out_tongue:

Taylor Hansen | 2017-05-09 00:33

I did, but it still didn’t work so I took it off. Just to be clear, the raycast goes above the collisionShape2D in the tree right?

inkmotor | 2017-05-09 00:36

The tree order doesn’t matter, and it’s a CollisionShape2D with a RayShape2D set as its shape, not an actual RayCast2D node.

Taylor Hansen | 2017-05-09 00:37

Ahhhhhhhh! THAT’S the part I was missing. Thanks!

inkmotor | 2017-05-09 00:39

Hmmm, what am I doing wrong?

inkmotor | 2017-05-09 00:44

You need both the RayShape2D and the RectangleShape2D (2 CollisionShape2D nodes as a child of your KinematicBody2D, which could probably be a RigidBody2D).

Taylor Hansen | 2017-05-09 00:46

Ok thank you very much

inkmotor | 2017-05-09 00:47

The collision_capsule should have a rectangle, not a capsule. Also I would recommend making the raycastshape a length of 1 and using the inspector to perfectly position it such that it’s right in the bottom center, protruding out of the bottom by a pixel. Here’s a rough description of what you should have, assuming the scale of each node is (1, 1):

root (RigidBody2D)
    sprite (Sprite2D)
    hitbox (CollisionShape2D)
        shape = RectangleShape2D
            extents = (32, 32)
        pos = (0, 0)
    bottom (CollisionShape2D)
        shape = RayShape2D
            length = 1
        pos = (0, 32)

Taylor Hansen | 2017-05-09 01:30

Dude, thank you so much, I very much appreciate your help. I will try that this afternoon, cheers!

inkmotor | 2017-05-09 02:27

Dudes!

There’s a full explanation of why the thingy was sticking on the thingy here!:

should start at the right spot, about 13:50

it’s all about slide() -ing the vector

Here’s the docs explanation

inkmotor | 2017-05-10 01:36