Page 1 of 3

A way to do simple shadows

Posted: Tue Sep 20, 2022 8:49 pm
by Gunroar:Cannon()
Is there a way to emulate shadows of an image simply by drawing a black version of a sprite upside down (under the original), but with a slant calculated from where the "light source" is? Any ideas?

It seems like the slanting can be done wither with a shader or love.transform, the same with upside down (but maybe also using angles).

Re: A way to do simple shadows

Posted: Wed Sep 21, 2022 1:30 am
by ReFreezed
You can simply use the scale and shear arguments of love.graphics.draw() and some trigonometry to transform the shadow. Here's an example that handles a light source from any direction:
SimpleShadows.love
(3.23 KiB) Downloaded 211 times
_screenshot.png
_screenshot.png (46.23 KiB) Viewed 4326 times

Re: A way to do simple shadows

Posted: Wed Sep 21, 2022 1:34 am
by BrotSagtMist
This _example_ looks way more awesome than it should for such a little piece of code :D

Re: A way to do simple shadows

Posted: Wed Sep 21, 2022 9:54 am
by pgimeno
Absolutely. Brilliant.

Re: A way to do simple shadows

Posted: Wed Sep 21, 2022 4:37 pm
by milon
I love how stuff in front of the light source gets silhouetted. Amazing!

EDIT: Just for funsies, here's a modified version where the light source follows the mouse instead.

Re: A way to do simple shadows

Posted: Wed Sep 21, 2022 5:55 pm
by Gunroar:Cannon()
You are all beautiful people :shock:

Though it's looking... flat. Like it those paper -style GE's like Don't Starve and Paper Mario. I don't even know how one would go about making the shadows more 3D looking. Still looks wonderful though!

Re: A way to do simple shadows

Posted: Wed Sep 21, 2022 6:40 pm
by ReFreezed
Well, the sprites are flat, so all you can get are flat shadows. You'd need 3D geometry to some extent for more 3D-looking shadows. Since they look the most flat when the light comes from the side it could be a good idea to design your game/levels such that that situation doesn't happen.

Re: A way to do simple shadows

Posted: Wed Sep 21, 2022 6:48 pm
by darkfrei
milon wrote: Wed Sep 21, 2022 4:37 pm I love how stuff in front of the light source gets silhouetted. Amazing!

EDIT: Just for funsies, here's a modified version where the light source follows the mouse instead.
Just awesome!
Screenshot_20220921-204701.jpg
Screenshot_20220921-204701.jpg (253.95 KiB) Viewed 4221 times

Re: A way to do simple shadows

Posted: Wed Sep 21, 2022 7:40 pm
by pgimeno
Put simply, it's what can be done using slant and size alone, which is what the OP specified.

In fact, slant does not produce correct results even admitting that the object is 2D. Since the light is a point, each side of the object should have a different angle with respect to the light, resulting in a shape for the shadow with diverging sides, not parallel.

I've been thinking what it would take to make it look right in that sense, but I couldn't think of anything short of a shader. And even with a shader it sounds tricky.

Re: A way to do simple shadows

Posted: Wed Sep 21, 2022 9:17 pm
by ddabrahim
Wow, this example is amazing. There should be a repo to collect gems like this.