calculating shadow quad

A51aa92e7b9c5f59f3a83c1f6f47e54e
0
seiko 101 Nov 25, 2005 at 14:22

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.

Many thanks

2 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 168 Nov 26, 2005 at 09:45

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
 \

    |--------|\
    |        |s\
    |building|h \
    |        |a |
    |--------|d |
    \        \2 |
     \ shadow1\ |
      \--------\|

As you see, you don’t even need any trig, just a bit of algebra.

8563f7b73aeb34bb8604f1dd8f546c88
0
Mattias_Gustavsson 101 Nov 26, 2005 at 10:15

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.