Why is my RigidBody2D glitching through my TileMap?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Astro Games

Hello,

I started on my first bigger project today. I’ve got the player & tilemap setup, but now there’s a problem. Whenever I run the game, the Player (eventually) glitches through the floor, as seen in the Gif here. How can I fix this?

If you need more info, let me know so I can provide it!

EDIT:
PasteBin: Crate
PasteBin: Player

That looks pretty strange. A few questions:

are you using a custom gravity amount that’s totally absurd?
what mode are the two rigidbodies in?
are there any custom physics configuration options in the project settings?
what does the player script look like?
what does the crate script look like?
does the same thing happen to the crate eventually too?
are there staticbodies that are perfectly overlapping where the player touches the ground there?

markopolo | 2018-04-08 19:28

Hi,

My GRAVITY constant is 10, and it is added every frame calling _physics_process(Delta) like so: motion.y += GRAVITY. In the end of the script I move the player something like this (I’m not at home with my laptop, so I don’t have the exact code): set_linear_velocity(motion).

Both RigidBodies (Player & Crate) are in the Rigid Mode.

No, I don’t have custom physics config options.

I’m not at home with my laptop. I’ll put a pastebin link in my post later today.

I’m not at home with my laptop. I’ll put a pastebin link in my post later today.

No. The Crate’s script only has that Gravity line. The same one as the Player, but with GRAVITY as an exported variable.

No. The crate (weirdly) doesn’t fall through the floor. That’s something that made me think…

There are no “seperate” StaticBody2D’s. I’m using a tilemap. Each tile defined in the Tileset’s scene has this structure:
| Sprite
±-- StaticBody2D
|
±-- CollisionShape2D → Rectangle

I hope that this information helps!

Astro Games | 2018-04-09 07:47

Most of that seems normal to me, but one thing that stands out is the gravity handling. It sounds like the scripts are calculating the effect of gravity on these objects, which seems a bit odd since the engine itself has gravity settings. I’ve not seen this behavior specifically before, but I do know it’s better to avoid directly modifying the state of physics objects (i.e. better to apply impulses than to set linear velocity directly) so that might also be what’s causing this. I’ll be able to confirm when I see the actual scripts.

markopolo | 2018-04-09 15:11

Hello,
I’ve put two links in my original post (where I asked the question). You mean those two?

Astro Games | 2018-04-09 15:14

:bust_in_silhouette: Reply From: Astro Games

I have decided to use a Kinematic Body for the Player.