interacting with 2D shadows

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

I am trying to make a 2d game, where you have to stay in the shadow, but I can’t figure out how to interact with shadows made by a LightOccluder2D.

Is there a way you can interact with the shadows or give them a collision shape?

:bust_in_silhouette: Reply From: rolfpancake

This is not an answer to your question but maybe a solution for the underlying problem:

When dealing with light it is often like dealing with (enemy) sight. You have some vectors starting from a specific point (light source == enemies eyes) and they are travelling through the room and if they cross the players body something happens (casting shadow == AI behaviour change).

These vectors are called rays and the algorithm behind it is called Ray casting. In Godot you are able to use RayCast2D to shoot single rays from a starting point to an end point. If the ray collides with an object it returns the objects reference.

Regarding your basic game design you can cast a ray from the light source to the players position. If the ray hits the player there is nothing in between them and therefore the player does not stand in the shadow. If the ray collides with an object the emitted light of this light source doesn’t hit the player. You then have to ask every frame if any of the available light sources have hit the player with a ray.

This approach has some drawbacks when it comes to reflection and diffraction but it should be faster in its simplest form than asking if a players position is inside a shadows polygon.

LightOccluder2D may not work for this out of the box because you will need the polygon created by the shadows.

To add to what rolfpancake said, it essentially turns into a line of site problem. Check out this page discussing visibility:

Red Blob Games 2d visibility

You use this to generate your polygon, which can be used to make your collision shapes, etc. And you also use it to do the masking for your shadows. There are variations to his solution that are more efficient, but those may not be needed for your project.

2D||!2D | 2018-02-19 12:27