Pixel-perfect collisions on sprites in Godot

preview_player
Показать описание
Whether you want accurate collision bodies or pixel-perfect mouse detection, this video is for you. Let's look at our options using both the editor and GDScript to create collision polygons from sprites in Godot.

Intro: 00:00
Via the editor: 00:24
Via code: 01:23
Objects with holes: 03:25

Рекомендации по теме
Комментарии
Автор

I didn't know that this feature was included in the engine by default, thank you for showing us!

LucyLavend
Автор

I've been making all my polygon shapes manually, it's been so tedious. Found your video while looking for a solution today. Thank you for sharing!!!

mellowminx
Автор

Love it!.. This stuff is hard to find... i got many ideas on hold till now!

highscore
Автор

Your videos should be seen by much more people, great stuff that I never saw anywhere else!

Noccai
Автор

Really insightful, even after 2 years and some engine changes.

pros_
Автор

Thank you for showing that this feature exists, I had no idea, it's really gonna come in handy!

diariodeumdesenvolvedor
Автор

What the heck. I needed to do this exact thing but didn't know about this. So I made the vertices one by one. It was pain.

Thanks for making this vid :)

nyuh
Автор

Is it viable to do this for animated sprites too? In the sense of is there any method good enough to be recommended doing, or just sticking with simpler shapes on animated sprites
It probably involves generating a different collision shape for each sprite frame, storing it and changing into it each frame while somehow not getting stuck into terrain because of it?

Edit1: To people using this in Godot 4: get_data() got renamed to get_image() and bitmap.get_size() needs to be converted to Vector2(bitmap.get_size()) (not Vector2i). After this, it works in GD4
Edit2: I... Have actually done it, first try. Animated sprite collision seems to be working really well. I'll probably cache shapes later so they don't have to be recalculated too for better performance. Without the future optimizations, it takes ~0.0005s to calculate an average pixel art shape per physics tick. For comparison, the video method was taking ~0.0001s only on startup. Both are pretty fast tho

BrianSantosF
Автор

You are a godsend, I tried searching for something like opaque_to_polygons months ago and came up nil

Sharkalien
Автор

Great video, I really appreciate that you went through each step completely (including some context), but didn't waste any time. Too many "guides" are full of filler and end up being useless.

Are you aware that your mouse isn't being captured in your video? There's a couple moments that it seems like you're gesturing with the mouse but we can't see it.

Daelon_Suzuka
Автор

I know this video is 1 year old at this point but I'm wondering if I could get some help. I've implemented the code as shown and everything works except the collisionPolygon is offset from the sprite. This only happens when the sprite is centered so I probably need to also offset the image when using get_data() in some way. I know I could just offset the transform of collisionPolygon but would prefer a more elegant solution tbh. Any suggestions?

matthewj
Автор

Is there a way to do this for each sprite of an animated sprite? My collision box is common for all the frames for some reason.

edgelord
Автор

can we do something similar but with a tilemap and have 1 way collision? ...since I painted all the tiles, but will be annoying making a collision box for all of them and the clicking the 1 way box, figured I could just add code to the tile map to have it generate a 1 way collision box, not sure how to do it though, the tilemap uses png sprites, kind of a newbie still

bes
Автор

Can you make a video on how can i reduce noise from my audio in godot. I want to record audio using godot and tgen remove the noise

BHanif-zcpr
Автор

Great video! I was wondering if you would know how to do something similar with objects drawn by a player. Currently, I have some code that lets a player draw a new polygon shape on the screen, then I convert that into a collision polygon shape. This sort of works but runs into a problem, if the lines of the polygon crossover themselves, the engine gets confused and throws out a "Convex decomposing failed" error. I tried to turn a player drawn polygon into a bytearray, convert that to an image, then follow the tutorial to create new polygons, but it got overly complicated and just didn't work, any ideas?

PlayOuterWilds
Автор

Hello Jason McCollum. I need your help. i finally made your Pixel-perfect collision via code but center-of-gravity is weird...everytime those are stading even on ball, edge whatever. do you know about it?

ThroatsqeezyKimchicken
Автор

Heya, glad I found your tutorial.

I've been trying to make it work with a sprite that has holes in it, but I've yet to come to a solution.
Would you be able to help me out with that ? Thanks mate

Fegit
Автор

isn't this performance intensive though

scarm_rune