In the docs it says that CollisionPolygon2D is an "editor only class". Does this really mean that if I create a KinematicBody2D and give it a child polygon, all through code, it won't work? It's not working for me, as I'm using move_and_slide() and my objects are still not colliding with anything, but I just want to confirm if I'm getting this right.

If that's so, then how can I create objects that can collide via code?

(I realize one way is how it's demonstrated in the Bullet Shower example, but it's rather convoluted.)

The CollisionPolygon2D just adds a collision shape to the parent node. You have to create a collision shape (e.g. ConvexPolygonShape2D) via code instead and use add_shape() on your KinematicBody2D. (The command is different in Godot 3.)

I think you're confusing CollisionPolygon2D with CollisionShape2D. I'm using the first one, the one you draw the polygon by placing vertices in the editor. I'm building the polygon correctly, though, at least as far as I can tell.

var w = img.get_width()
var h = img.get_height()
var poly = CollisionPolygon2D.new()
poly.set_polygon(Vector2Array([Vector2(0, 0), Vector2(w, 0), Vector2(w, h), Vector2(0, h)]))
add_child(poly)

I'm not confused, but not very understandable I guess. You can use CollisionPolygon2D in the editor, but you can't add collision shapes this way via code. Instead you have to use CollisionShape2D. So it should be var w = img.get_width() var h = img.get_height() var poly = ConvexPolygonShape2D.new() poly.set_points(Vector2Array([Vector2(0, 0), Vector2(w, 0), Vector2(w, h), Vector2(0, h)])) add_shape(poly)

Yea, I misunderstood you there. Sorry about that.

I tried what you suggested and it gives me an error (parameter 'p_child' is null). Meanwhile I remembered I could turn on "Visible Collision Shapes" for testing, and the way I was doing it does have the collision polygons in-game, they just don't seem to collide with anything...

Here's the entire code if you or anyone else is interested in seeing or testing it. (It's all in one file, so it should work in just a simple Node2D, no need to even create a new project -- you'll just need to add a StaticBody to test collisions, though).

Just a simple explanation of what I'm doing: it basically creates a bunch of "particles" (which are KinematicBodies) when the mouse is clicked. The intention is to test ways to create persistent blood effects, and for now I'm using an Image where I draw all the particles once they stop moving, so I can get rid of them and still preserve their final effect. However I'd like those particles to collide with walls and stuff while they're moving... This is for a top-down game.

(And by the way, just in case you're wondering, the StaticBodies I'm using to test collisions are properly set up. I have two of them, plus a RigidBody that falls on them and collides, and a simple Player character that collides too. So on that side of things everything is working fine...)

The reason why the ConvexPolygonShape2D was giving me an error is because I was adding it as child. I noticed later I'm supposed to do add_shape() on the KinematicBody as you said, and not add_child(). I was doing that because I hadn't noticed before that the ConvexPolygonShape2D is a resource, not a node...

It does collide with stuff, though, but it all behaves weirdly, as particles don't seem to be spawning exactly at the mouse, when it's many of them. See if I can figure out why...(EDIT: it's because they're colliding with each other and getting scattered by the physics engine.)

Thanks for the help.

5 years later