Live data from Hacker News

How to create 2D visibility/shadow effects for your game

ncase.github.io

41–47 of 47 posts

Re: How to create 2D visibility/shadow effects for your game

#41
this is interesting (although this sort of effect has never mystified me) - i can't help but think how this can be optimised. the steps gone through here are things i never would have done and the final solution is about where i would start...

just blindly tracing to all of the verts 'feels wrong' its the sort of thing that is intuitively unreasonable with large data sets and is quite obviously going to be constrained by available horsepower at some size.

precomputed vis data is the obvious optimisation and removes the O(n) behaviour. not only do you not have to test everypoint but your raycast itself need not test every edge. i wrote something ages ago about generating PVS in 2d as an example of how to approach the problem in 3D...

(http://jheriko-rtw.blogspot.co.uk/2011/05/visibility-determi...)

the other thing that bugs me here is the jitter. it is the classic 'lazy' way to soften shadows... a better effect can be gotten for less - if you offset the light position perpendicular to the direction toward the test vert, in both directions by the same amount then you can generate two ray casts. triangulating the resulting mesh might be a little painful (unless you cut the world into convex chunks - then it is nearly trivial) but you will get the outline for both edges of the shadow and can then do something like vertex interpolated colour to give you the same visual result as infinite raycasts for the price of two...

Re: How to create 2D visibility/shadow effects for your game

#42
post #31

Nice article. However I think there's a better algorithm: the one actually used in Doom from Id Software. First the scene must be put into a 2d bsp (that requires slicing some edge). After that, the bsp must be traversed front-to-back and that gives the edges in the right order. Just keep track of which angles are occluded then while traversing. During the process, when an edge is proccessed, cast/create the shadow a…

that might work for a frustum like directional light source (maybe) but what about an omnidirectional point light like in the example?

you can certainly walk 'outwards' from a node but then you are missing the real benefit to the bsp for this problem imo... which is that you just carved the world into convex volumes.

convex polygons are easier to work with. they allow applying many more algorithms and make others trivial enough to re-invent on the fly as needed... instead of walking a structure at all you could keep a list of which edges are visible at each leaf node - if you have 'loads' of memory (e.g. > 1MB) then you might be able to store the edges themselves rather than indexes and mitigate memory lookup expenses as well (which are so far away from anything in a broswer... but yeah).

Re: How to create 2D visibility/shadow effects for your game

#45
post #5

He uses a nice trick to approximate the real thing, however this algorithm wouldn't be able to deal with curved objects.

Couldn't you do something like this for a hack? http://i.imgur.com/G5U43ZU.png Calculate the edge points of the circle, draw a line between them, and then your shadow is simply a rectangle. Overlay the circle over the shadow to hide the line. This will work with circles and ovals, but not more complex curves.

That's not a hack, that's actually the best way to do it. And it can work with more complex shapes (even non-convex ones) as long as you can find the tangent points - which isn't always easy.

Re: How to create 2D visibility/shadow effects for your game

#46
post #36

Earlier quoted context omitted.

A bit OT but what are some of the most successful open source game titles out there? I'm writing a game server backend right now and didn't find very many projects where I could read good quality game code. (I come from the JS web app world where everything is out in the open so I'm super biased)

I found Battle for Wesnoth to be entertaining as a game. It's open source, but I don't know about code quality. If you are willing to read code from when dinosaurs roamed the land and sea, try Nethack. If you only care about reading code, open source or not, I think the first Deus Ex's in game scripting code was visible in the game's editor. (And the rest was the first Unreal engine, whose might also be available for…

Cool, thanks for the pointers!

Re: How to create 2D visibility/shadow effects for your game

#47

I'm kinda tired of the "cone-of-vision" mechanic and associated visual effect common in modern 2D games. In traditional 2D games, being able to see things that are not visible to the player's avatar is, IMO, a significant part of the charm. Not being restricted to a realistic viewpoint lets you become the larger-than-life hero of cartoons and cheesy action movies that does impossible things like leaping into a room f…

You seem to have an unnecessarily narrow view of games and how they're constructed. Try out Mark of the Ninja and tell me they were 'just trying to make an FPS'. Picking an appropriate perspective for a game involves a lot of tradeoffs. While certain mechanics are most appropriate for certain perspectives (top-down 2d, isometric, first-person 3d...) a general approach to 'visibility' is not one of them. Visibility (o…

I hang out in some indie/amateur game dev communities and it's like everyone and their dog is making a top-down stealth shooter with the exact same effect, the exact same mechanics, and the exact same "FPS in 2D" feeling. That's what I'm tired of, not 2D raycasted lighting/shadows in general (yes, I thought of Nethack while writing the grandparent comment).
Post reply on HN