Circular shadowcaster

Post your own tutorials, guides and demos.

Post » Sun Feb 15, 2009 3:16 pm

Okay here's a tidier version. I've put the drawing into a function so all you need to do is place your own circles in the layout editor, loop through each circle and call the function as in the example. I've left the create function in there (but disabled) in case you want to create circles at runtime. Just pass the 3 params - x,y,radius.

Still haven't fixed the things I said I would because my brain is in meltdown today.. I think I overdosed on math last night! Anyway, it's getting there I think and it's workable for now.

http://willhostforfood.com/users/P/procrastinator/circleshadowcaster_cleanedup3-functions.cap
B
15
S
5
G
7
Posts: 877
Reputation: 5,650

Post » Sun Feb 15, 2009 11:34 pm

Looking good! Containers and functions make that really easy to use.

I'll definitely be switching to your distort map method - with 5 circles, the box ring method drops to ~45 FPS, while yours is running 260.

I wonder if this is how Ashley implemented the shadowcaster behaviour, using something similar to distort maps? I have a rough idea of how we could make box shadows (like in the behaviour) this way. Not that that there would be much point, as behaviours are inherently more efficient, but it's an interesting thought. :)
B
5
S
2
G
4
Posts: 113
Reputation: 2,101

Post » Mon Feb 16, 2009 12:25 am

Yeah I tried your demo earlier and noticed it was a lower FPS. I think the shadowcaster behaviour sends scanlines out in all directions and works from there? That's the only thing I can think of..

The good thing about distort maps is you could assign a texture to the shadow itself. You could have gradient shadows where the opacity falls off as it gets further away from the circle for example. I'm sure there are other uses for a textured shadow... hmmm
B
15
S
5
G
7
Posts: 877
Reputation: 5,650

Post » Mon Feb 16, 2009 2:46 pm

Here's a fixed version. In the older versions one side was inaccurate. I had my triangles wrong when working it out and used atan instead of asin ;) It works now! Still need to fix the clipping..

http://willhostforfood.com/users/P/procrastinator/circleshadowcaster_v0.4.cap
B
15
S
5
G
7
Posts: 877
Reputation: 5,650

Post » Mon Feb 16, 2009 10:29 pm

At this rate, you're going to be one of the forum math people! ;)

(I'm still working out how you did this - haven't had much time lately, but I think I'm getting the idea.)

By the way, I'm trying to work out if objects are shadowed in the game I'm making (for a situation where a swarm of small objects have to hide from light). I was using instant-hit bullets or LOS objects to detect whether they're shadowed, but there should be a way to adapt this method to work it out, right? I might try it sometime this week.

(Unfortunately, 'Object is overlapping shadow' doesn't work - I guess distorted objects don't count for collision purposes!)
B
5
S
2
G
4
Posts: 113
Reputation: 2,101

Post » Mon Feb 16, 2009 10:47 pm

[quote:262t7gxu]At this rate, you're going to be one of the forum math people! ;)[/quote:262t7gxu]

hahah I hope not. I suck badly at maths. I was just lucky I figured this out!

[quote:262t7gxu](I'm still working out how you did this - haven't had much time lately, but I think I'm getting the idea.)[/quote:262t7gxu]

I'll try and comment how I did it for the next version. I rarely comment my stuff when I'm hacking away at it but I really need to with this so I can remember in a years time what the hell those formulas were ;)

[quote:262t7gxu]By the way, I'm trying to work out if objects are shadowed in the game I'm making (for a situation where a swarm of small objects have to hide from light). I was using instant-hit bullets or LOS objects to detect whether they're shadowed, but there should be a way to adapt this method to work it out, right? I might try it sometime this week.

(Unfortunately, 'Object is overlapping shadow' doesn't work - I guess distorted objects don't count for collision purposes!)
At this rate, you're going to be one of the forum math people! ;)

(I'm still working out how you did this - haven't had much time lately, but I think I'm getting the idea.)

By the way, I'm trying to work out if objects are shadowed in the game I'm making (for a situation where a swarm of small objects have to hide from light). I was using instant-hit bullets or LOS objects to detect whether they're shadowed, but there should be a way to adapt this method to work it out, right? I might try it sometime this week.

(Unfortunately, 'Object is overlapping shadow' doesn't work - I guess distorted objects don't count for collision purposes!)[/quote:262t7gxu]

As I was reading that I was immediately thinking "try collision detection with the shadows" but you say they don't work eh... damn. Right... just tried LOS... here you go. Although it depends what you class as "being in shadow"... ;)

http://willhostforfood.com/users/P/procrastinator/circleshadowcaster_v0.41-LOS.cap
B
15
S
5
G
7
Posts: 877
Reputation: 5,650

Post » Tue Feb 17, 2009 7:47 am

Yeah, it makes sense... "If I can't see the Sun, then I am in a shadow."
B
62
S
21
G
12
Posts: 1,910
Reputation: 13,155

Post » Tue Feb 17, 2009 9:12 am

Here we go!

http://www.mediafire.com/?mj1zuunyx1d

I had in mind something where if the object isn't completely shadowed, it's considered to be in sun. And after two clever-seeming ideas that turned out to be completely unworkable, I swallowed my pride and just used instant-hit bullets. It turned out to be pretty efficient, since I only fire them along the shadow's edge.

Still not totally accurate (doesn't notice if there's only a few pixels of object showing), but I'll straighten that out - I just need to event the bullets not to notice the circle they originate from, and then I can cut much closer without unwanted collisions.
B
5
S
2
G
4
Posts: 113
Reputation: 2,101

Post » Tue Feb 17, 2009 1:00 pm

That's a nice method but it's flawed unfortunately. It's too slow (halves the framerate here) and if you move the lightsource to behind the very bottom left circle the object isn't hidden anymore. Almost there... probably needs a little tweaking to get it right.

I'll get this damn clipping done today and then I'll put a bit more time into this problem.. hmm..
B
15
S
5
G
7
Posts: 877
Reputation: 5,650

Post » Tue Feb 17, 2009 10:07 pm

It's obviously not too slow.. I meant it's slower. Anyway, a few further tests of my own and I couldn't get it much faster than yours with that accuracy. I've just tried a "point in poly" routine but it didn't work. Too tired.... but I think that'll be the fastest method if I can get it working...

This shadowcaster will be very handy for many things when it's fully working. I have a few plans for it but those are a long way off ;)

What's the game you're working on btw? Sounds like a 2D Thief game or something.
B
15
S
5
G
7
Posts: 877
Reputation: 5,650

PreviousNext

Return to Your tutorials & example files

Who is online

Users browsing this forum: No registered users and 0 guests