# How to implement a clipped shadow in 2D?

I want to create a shadow effect similar to the one found in JoyMasher's Blazing Chrome.

Notice that the shadow clips when you're on a ledge, and projects to the geometry below.

Currently, my shadows are just sprites which I disable when the character is not grounded.

I don't care how JoyMasher did it, I just want to achieve something similar.

How might I achieve an effect similar to the one in Blazing Chrome?

I am working in Godot, but would be fine with an engine-neutral description of a technique.

I have access to all the normal stuff (raycasts, shaders, and so on). That said, I am working in pure 2D, so I can't use any kind of 3D light source emitting downward to achieve the effect.

1)Cast a set of rays from origin to the ground evenly distributed through the width of your object.

2)Draw your shadow as a small portions of black sprites with varying height so you can round the edges. Make sure width of a sprite portion is enough to cover distance between rays.

3)Tweak height of shadow sprite portions and make sure your shadow looks as you want it to look.

As a way to optimize algorithm, you can also pre-calculate ray hits for the whole stage and then only check X coordinate of the object along with its width to find out which area object covers and then draw shadow according to the ray hits data (shadow portion heights) in the covered area.

If you're rendering the shadow as a texture in hardware (which most 2D renderers do as well), you can specify texture coordinates so you only render part of the texture.

So imagine you have a shadow split into two halves here:

You need to render two shadow textures; the left half will have texture x-coordinates going from 0 to 0.5; the right half 0.5 to 1.

3D renderers like OpenGL use ratios for the numbers, i.e. 1 is 100%. 2D renderers tend to specify the actual size of the source and destination rectangles in pixels. For example in SDL's SDL_RenderCopy you specify a source and destination rectangle. I'm not familiar with Godot but it appears draw_rect_region has a src_rect parameter you can try.