Shadow Casting Engine (SOLVED)

15 posts

Flag Post

I’ve been looking all over the internet for a tutorial on building an engine that casts shadows bsed on a light source. (The sort of thing you’d see in a survival horror game with a torch or a lantern)

I’m not even sure where to begin, or how to make shadows hide objects, can someone point me in the right direction?

 
Flag Post

Well, you can try, for example, to have two bitmaps, one is main, the other is your light cone, and black background. You clear the light cone, draw your current shape of a light cone with center on character, draw your level anyhow on the main bitmap, then you do a copyChannel() for alpha from the light cone bitmap to main. Voila, blackness out of light cone.

 
Flag Post

sure, I assume you are looking for this:

http://dl.dropbox.com/u/17828135/index.html

If you PM me with an email or something I’ll send you my software version when have the chance; however the software version can only run 3~4 dynamic lights before slowing down greatly.

The above runs on hardware but is very untidy and unoptimized and engine specific.

 
Flag Post

That’s actually perfect qwerb. It’s exactly what I’m trying to achieve. :P

Originally posted by vesperbot:

Well, you can try, for example, to have two bitmaps, one is main, the other is your light cone, and black background. You clear the light cone, draw your current shape of a light cone with center on character, draw your level anyhow on the main bitmap, then you do a copyChannel() for alpha from the light cone bitmap to main. Voila, blackness out of light cone.

Vesper, I want the light cone to be obstructed by walls on the map.

 
Flag Post

Then you have to first calculate the cone, then draw it, then mask the main image. You were also asking of “how to make shadows hide objects”, I responded to that part.

 
Flag Post

Ahh I get it now. Thanks man. :)

 
Flag Post

I’ve got the light working, it looks great. I’ve got a box but I’m not sure what’s next.

I need to draw a line from the corners of the box to off the screen to make a polygon I can fill black to create a shadow, but I’m not sure where to get the angle from.

So if p is the player, b is the box, and x is the end of the line:
I need a line from b to x based on the location of p.

p———b———x

How do I do that?

 
Flag Post

Well, you have the center of our light source as “p”, you have “b” as a point, and you have a light source distance as “px”. You first get direction of “pb” via (b.y-p.y,b.x-p.x), then you have its length as l=Math.sqrt((b.y-p.y)*(b.y-py)+(b.x-p.x)*(b.x-p.x)) so you can get the coordinates of “x” (I’ll name it “e” for “end”, as “x” is a coordinate) like e.x=p.x+(b.x-p.x)*px/l; e.y=p.y+(b.y-p.y)*px/l;

Geometry, 7th class.

 
Flag Post

Haha, once I stopped doing maths at the end of last year, everything went right out the window.
Thanks a lot for your help. You’re actually a genius. :)

 
Flag Post

Maths are more useful in real life than many other scientific courses you might take in either normal school or university. Even integrals help occasionally :) By the way…

 
Flag Post

Haha, probably not when I become a minister. :P

 
Flag Post
Originally posted by DrYoshiyahu:

Haha, probably not when I become a minister. :P

Don’t forget us little guys. ;D

 
Flag Post

Here’s the engine now. Thanks for the help. :) I wrote down that formula because I’ll need it for the bullets :D

 
Flag Post

Good job :) You might have to optimize these calculations a little, if there would be a lot of calculations of this.

 
Flag Post
Originally posted by vesperbot:

You might have to optimize these calculations a little

And tweak them a bit. This doesn’t look right.