This project is read-only.

Limit shadow size in "top down world" game

Mar 28, 2011 at 12:31 PM

I've been keeping an eye on krypton for a while and decided to try to integrate it in my 2d shooter game. My game is a top down world, everything is seen from above. To make it fit in my game I have created a sun object that fills the world with light.

 

Light2D sun = new Light2D()
            {
                Texture = lightTexture,
                Range = 10000,
                Color = new Color(255, 255, 255),
                Intensity = 1,
                Angle = MathHelper.ToRadians(0),
                Position = new Vector2(1000, 1000),
                Fov = MathHelper.ToRadians(360)
            };

 

All the building objects in the world adds a krypton hull object reflecting the building objects size and rotation.

 

var hull = ShadowHull.CreateRectangle(new Vector2(building.Width, building.Height));
            hull.Position = building.Position;
            hull.Angle = building.Rotation;

            _krypton.Hulls.Add(hull);

 

 

Now I want to emulate the sun height to limit the shadow size. Is that possible? See explanation below.

 

Apr 3, 2011 at 12:35 AM

Emanuel,

There's absolutely a way to do this. Right now, the shadow stretch (or distance) is determined by a variable in the Effect file.

You should be able to adjust the following setting to see if it suites your needs:

float    ShadowStrech = 1000000;

However, this may not work exactly how you'd like it to, so if it doesn't please let me know and I'll try to work something out. :)

Thanks!

Apr 4, 2011 at 11:11 AM

Thumbs up. That solved my problem. Ill let you know when my game is ready for test. ^^

Apr 4, 2011 at 9:02 PM

Isn´t this method a little bit unflexible?
I mean, maybe you need a short shadow and a very long shadow, but if its static determined, you can´t change, can you?

Would be nice, if you could determine this in the ShadowHull / Light or so on :)

 

Correct me if i´m wrong :D

Apr 4, 2011 at 10:10 PM

Nope its not very flexible. Had to make different effect files to simulate sunset/sundown/morning/evening etc. Sounds like a good idea to make the hull object hold information about its shadow behaviour.

Apr 5, 2011 at 3:52 PM

@Razer,

Absolutely. This is a very static way of stretching the light.

Originally, the idea was to stretch the shadows far enough that no-one would ever see the end of them. However, as you see with the example above, sometimes we want to see the end of the shadow. For this particular example, it seemed simple enough to just modify the existing stretch distance.

There are a couple of issues when stretching the shadow...

1.) How high off the "ground" (or wall, in some cases, for side-scrollers), is the object that needs to be in shadow. Does it even touch the ground?

2.) How high is the light from the ground / wall? Is it at such an angle that shadow will stretch indefinitely, and appear as if the shadows are infinite?

3.) How do we calculate these scenarios? :)

I've been thinking about a few ways of doing this, but for this version of Krypton, we'll probably skip the bolded question, and assume everything is touching the ground. After all, we're going for 2D games here, anyways.

Apr 6, 2011 at 3:07 PM
Edited Apr 6, 2011 at 3:07 PM

By changing the ShadowStretch variable in the effect file I get this strange behaviour on the shadows.

Heres another problem I discovered today when I implemented a 2d camera transformation matrix and assigned it to krypton. It seems like the shadows shrinks and then goes back to normal when rotating the world with the rotation angle between +-46 and +-43.

I have been debugging this problem for hours now. Any ideas? //Thanks

Apr 8, 2011 at 5:40 PM

I'd need to see your actual drawing code, but from the looks of things, it seems that you're rotating the shadow map after generation, which would cause clipping. Drawing the shadow map should happen *after* everything is translated. (Krypton should handle its own transformations, if setup properly).

It really does look like your drawing the shadow map and then rotating it before adding it to the final draw.

Apr 8, 2011 at 5:41 PM

As for the first error, can you show me the code where you generate the vertices for your shadow hulls?