
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?


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.


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.


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.


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.


Ahh I get it now. Thanks man. :)


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?


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.yp.y,b.xp.x) , then you have its length as l=Math.sqrt((b.yp.y)*(b.ypy)+(b.xp.x)*(b.xp.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.xp.x)*px/l; e.y=p.y+(b.yp.y)*px/l;
Geometry, 7th class.


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. :)


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…


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


Originally posted by DrYoshiyahu:
Haha, probably not when I become a minister. :P
Don’t forget us little guys. ;D


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


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


Originally posted by vesperbot:
You might have to optimize these calculations a little
And tweak them a bit. This doesn’t look right.
