math & physics
seiko at November 25th, 2005 09:22 — #1
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, 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 precise shadow with soft edges etc. I just want the shadow to flare out from the buildings base at the correct angles so it won't be enough just to rotate the original quad.
reedbeta at November 26th, 2005 04:45 — #2
I think you will not be able to draw a single shadow quad, but rather a quad for each edge (wall) of the building. If your buildings are represented by rectangles, and let's say the light is coming from the top left, then you need to draw a shadow quad each for the right and bottom edges (i.e. the ones facing away from the light). You simply take each edge, translate it away from the light by an amount proportional to the height, connect the vertices of the original edge to the vertices of the translated one, and you're done.
An attempt at ASCII-art to show what I mean:
\ light direction
| |a |
\ \2 |
\ shadow1\ |
As you see, you don't even need any trig, just a bit of algebra.
mattias_gustavsson at November 26th, 2005 05:15 — #3
One simple way of doing it is to define a cube (or several cubes?) for each building, and then call the D3DXMatrixShadow function, which calculates a matrix that projects geometry onto a plane, and then just render the cube with that matrix. As long as the shadow receiver is flat, this can produce very realistic shadows.