Sign in to follow this  

shadow cast quad

Recommended Posts

Seiko    122
I'm creating an over head 2d scroller and use quads to represent buildings. I want to now simulate a buildings shadow cast by using anothr quad. This quad will simply be alpha blended to the scene. Question is how can I calculate the shadow quad so it is rotated and sized correctly dependant on the lights position, buildings/quads position and height. Obviously the 2d quad doesn't have a height but I can add the variable from else where as this will be determined by artistic flare ;) As it's 2d albeit using a 3d engine I'm hoping that I can call upon some basic trig and just to be clear I'm not after a super accurate shadow with soft edges. Instead I just want to know how to have the shadow quad flare out from the buildings quad at the correct angles. This means I can't get away with a simple quad rotation and thus need a little help on the algorithims. BTW, I'll be using 1 shadow quad per light and it will be dynamic i.e. lights move so precalc stuff is out too. Many thanks.

Share this post

Link to post
Share on other sites
grhodes_at_work    1385
I would suggest you just create a 2D "light direction"---also in your 2D playing plane, and use that to indicate the orientation of the shadow quad. Implement a simple controller so that if you move the light direction, the shadow quads will be updated. That same controller could update the quads if you want the shadow lengths to change, giving the illusion that the light is moving higher or lower in the air.

If you're using a 3D API, and have enabled Z-buffering, then you'll need to enable polygon offset (OpenGL) or Z bias (Direct3D) to prevent or minimize Z-fighting that would make your shadows look bad.

Share this post

Link to post
Share on other sites
someusername    427
Hmmm, you're not after the super accurate shadow, but you still want the right angles for it to look realistic...

The angles in which the quad should be drawn aren't that difficult to find... if you know the appropriate height for your projected building's shadow...
This is what I have to suggest: I assume that the plane where the shadows are projected is supposed to be horizontal in your 2d scene (like a road for example), and that the bases of all your buildings are drawn at the same Y on screen. These restrictions are not vital. If you understand what I'm talking about, you should be able to adjust it easily...

First find an imaginary angle (that would make sense) for the sun's height in your scene. E.g. if the sun is at the horizon the angle should be zero, if it's supposed to be right on top of your building it should be 90degress etc.

The projected shadow's height, (in screen pixels) would be varying as the builing_height_in_pixels*(tangent of (90degrees minus 'sun angle')).
What I mean? if the sun was just rising (0 degrees) and was occluded by your 40 pixels tall building, its shadow's height (measured from the building's base to the bottom of your screen) should be "building_height*tan(90-sun_angle)" which is infinite, because the sun would be exaxtly at the horizon. At an angle of 45 degrees the shadow's height would be equal to the building's height. At 90, the shadow's height would be zero, because the sun would be on top of your building. It wouldn't cast any shadow...
Of course the angles for your sun shouldn't range in (0,90 degrees) by all means... I'm sure you'll get more realistic results for 30 degrees to 60.

Now that you know the shadow's height from the building's base to the bottom of your screen, you need to find the angles for the quad's sides. To do this is you must visualize a 2d coordinates system with its x axis parallel to the buildings' bases and tangent to them. The units can be pixels. Say you want to project one of the building's upper vertices to find the position of the respective vertex in the shadow...

You must find the position of the sun and the vertex in this 2d system. Then you must find the equation of the line that passes through these two points. If the points have coords A(x1,y1), B(x2,y2), then the equation is:
y-y1 = ((y2-y1)/(x2-x1))*(x-x1)
This should get down to sth like : A*x+B*y+C=0
Then you have to find the point of intersection of this line with the horizontal line: y = - shadow_height
I hope you're with me so far, its'not that difficult, I just don't know how to explain it.
You're actually finding the point where the line from the sun to the building's vertex, intersects the parallel line that passes through the "shadow building's roof!" This is the point where you should draw your projected 'shadow vertex'

Btw, this won't work the way it should if the sun is below a building's roof...

I hope you find this useful, I don't know how to explain it any better... If you have any questions, shoot. I believe this the best you can do with 2D

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this