3
\$\begingroup\$

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

enter image description here

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.

\$\endgroup\$
5
\$\begingroup\$

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

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. enter image description here

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

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.

\$\endgroup\$
3
\$\begingroup\$

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:

shadow halves

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.