|
metadata
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?
|
|
|
metadata
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.
|
|
|
metadata
sure, I assume you are looking for this:
[http://dl.dropbox.com/u/17828135/index.html](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.
|
|
|
metadata
That’s actually perfect qwerb. It’s exactly what I’m trying to achieve. :P
> *Originally posted by **[vesperbot](/forums/4/topics/310485?page=1#posts-6560292):***
>
> 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.
|
|
|
metadata
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.
|
|
|
metadata
Ahh I get it now. Thanks man. :)
|
|
|
metadata
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?
|
|
|
metadata
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.
|
|
|
metadata
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. :)
|
|
|
metadata
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…

|
|
|
metadata
Haha, probably not when I become a minister. :P
|
|
|
metadata
> *Originally posted by **[DrYoshiyahu](/forums/4/topics/310485?page=1#posts-6560903):***
>
> Haha, probably not when I become a minister. :P
Don’t forget us little guys. ;D
|
|
|
metadata
[Here’s the engine now.](http://megaswf.com/serve/2520169/) Thanks for the help. :) I wrote down that formula because I’ll need it for the bullets :D
|
|
|
metadata
Good job :) You might have to optimize these calculations a little, if there would be a lot of calculations of this.
|
|
|
metadata
> *Originally posted by **[vesperbot](/forums/4/topics/310485?page=1#posts-6563461):***
>
> You might have to optimize these calculations a little
And tweak them a bit. This doesn’t look right.

|