How to apply same Polygon to both a KinematicBody and to a Light Collider?

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

Hi guys,

I’m playing around with this 2d game idea in which I have walls made out of polygons and the same polygons should also block light from passing through.

How can I apply the same polygon of a LightOccluder2D to a Body?

Or else, do you know if there’s a way for a Body2D to also be rendered and casting shadow?

Thank you

:bust_in_silhouette: Reply From: Calinou
  1. Create a Polygon2D or CollisionPolygon2D node and draw a polygon for it.
  2. Edit the Vector2Array, click the cog at the top-right of the inspector, then click “Copy Params” (first image).
  3. Create a LightOccluder2D, edit the Vector2Array, click the cog at the top-right of the inspector, then click “Set Params” (second image).
  4. You should now have identical Polygon2D/CollisionPolygon2D and LightOccluder2D.

First image

Second image

Thank you very much. Still, I don’t get why I shouldn’t be able to do this programmatically. Should I open a ticket on GitHub?
I guess something like node.set_polygon(node2.get_polygon()) would be useful in some contexts.

Paolo Montesel | 2016-05-23 16:06

It would definitively be useful!

timoschwarzer | 2016-11-11 14:46

:bust_in_silhouette: Reply From: ilawicki

I actually had reversed problem. I had light occluder and wanted to have collision with it. I solved it programmatically. Solution easily can be reversed, because both LightOccluder2D and CollisionPolygon2D store vertices in PoolVector2Array, so to make one like the other you just assign one PoolVector2Array to the other PoolVector2Array. Take a look at attached code to see what I mean:

var levelCollisionData: StaticBody2D = StaticBody2D.new()
var collisionPolygon: CollisionPolygon2D = CollisionPolygon2D.new()

collisionPolygon.polygon = get_node("LightOccluder2D").occluder.polygon
levelCollisionData.position = get_node("LightOccluder2D").position

levelCollisionData.add_child(collisionPolygon)
add_child(levelCollisionData)