# Real time shadow casting in a 2D isometric game

I'm writing a small 2d isometric engine in C++ and I'm trying to implement real time shadow casting. I followed a simple approach described on this page and here is the result (light is located at the same position than the yellow cube):

The result is very nice but there are missing shadows on walls and on the top of cubes. Here is an example of how it should look like (I've draw expected shadows in green):

All the cubes drawn are simply made of 3 2D quads located at a X-Y position and with a Z depth (z = x + y). I use OpenGL with an orthographic matrix (glOrtho). Shadows are drawn using the stencil buffer.

I'm looking for resources or solutions that would help me complete this shadow casting implementation.

Thanks a lot!

-
Your link to "this page" is pointing to an image and not a page. Also, I'm just guessing here, but it might be easier to solve that problem in 3D and just project the camera iosmetrically. – Tetrad Apr 27 '11 at 15:08
Oups, you are right, I fixed the link. It would certainly be easier with a 3D projection but 2D has its advantages too and I'm sure there is solutions to implement this in 2D. – XPac27 Apr 27 '11 at 19:34

For roof and walls, you may try to determine intersections between your wall segments and generated shadows regions. You need two pass rendering of your shadows. First one will do what you are doing today (in memory storage). Second pass will calculate intersections of roof and wall. Last stage is real rendering. DeadMG is wrong, you can do that.

Note: for roof you have to intersect shadow area and roof area.

-
Thank you for the good advice ! If may be able to optimize this second pass by checking which walls should be computed using the dot product of their segment compared to shadows segments ones. I will try that and post back if it works. – XPac27 Apr 28 '11 at 0:54
It's been a while but I finally got it to work thanks to your suggestions! I needed only 2 geometry methods (one to know if a point is within a polygon and one to get the intersection of two segments). I still need to handle roofs but it looks easy to do know. You can watch the result on this video and look at the source code here (i just worked 1 day on it so it may be possible to optimize it more). – XPac27 May 15 '11 at 19:31
@XPac27 that is amazing. Thanks for sharing. – ashes999 Aug 19 '13 at 23:20

Given that you want this game to be small, what about just saying that the current shadow implementation is good enough?

I admit that I have not seen this in action, that is, I have not seen how disturbing or non-disturbing the shadow imperfections will be when this games is running live with dynamic objects and dynamic light sources but judging from your images, I'm very tempted to say "it's good enough, now focus on getting the game completed". If the current implementation happen to be small and execute fast and fixing what you perceive to be problems will turn the code big and slow, I think you are better of not worrying about those shadows.

I know, I know, suggesting "don't try to solve the problem" as a solution to a problem might be a bad practice. Still, I honestly want to say that it really is good as it is if you want a small footprint for your code.

-

Solution described is not full 3d as he doesn't need full 3d solution. It will look like but it is not. Creation of shadow casting must be considered as intersection between volume. What he needs is less complicated. Remember (if you have kown) dooms and dooms like rendering engine. All processing were done on two dimensions.

-

Fundamentally, what you're looking for can't be done. You want to take a bunch of 2D objects and have them cast shadows as if they were 3D objects. If you want to have full 3D shadows, you need to have 3D objects.

-
But that isn't 3d shadow casting like in the question. Any wall completely blocks line of sight in that direction, you can't have objects of variable height. – yuriks Apr 28 '11 at 0:53