Topic was automatically imported from the old Question2Answer platform.
Asked By
Paolo Montesel
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?
Create a Polygon2D or CollisionPolygon2D node and draw a polygon for it.
Edit the Vector2Array, click the cog at the top-right of the inspector, then click “Copy Params” (first image).
Create a LightOccluder2D, edit the Vector2Array, click the cog at the top-right of the inspector, then click “Set Params” (second image).
You should now have identical Polygon2D/CollisionPolygon2D and LightOccluder2D.
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.
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)