I'm developing a 2D stealth game but I cannot figure out how to create something like a torch (flashlight) that the enemies hold and look around with.Any help? Also, if relevant, I'm using MonoGame.
-
2Can you post a picture of what you want it to look like? Are you looking for information on how to illuminate objects around the torch? Or do you want information on how to draw the fire coming out of the torch? (Also, do you mean "torch" in the British sense - like a flashlight - or American sense - like a stick on fire?) – user1118321 Jan 09 '18 at 06:40
-
Quick google search (I guess what you are asking for is just a simple lighting, everything is explained well enough for a beginner): 2D lighting – Nick Jan 09 '18 at 06:57
-
I've never used it myself but maybe this awesome lighting library would be of interest: https://github.com/discosultan/penumbra – craftworkgames Jan 09 '18 at 23:48
1 Answers
If I have understood your question correctly, what you want to have is e.g. a "dark map", where you can have a circular area whoch is brighter from the torch.
Now, since I don't really know Monogame, you could fill your screen with a half transparent, dark color over your normal map. Then, you probably could draw a sprite over it with a gradient from the aforementioned color to a fully transparent color. You'd have to use subtractive blending to clear the area from the dark color.
I mean something like that:
This was created using an image editor, I took the image, filled it with a 65% yellow-brownish color, then made a selection around the torch source, deleted it and added a gradient from fully transparent to the previous color. For your game you could also create this image in a pixel editor and then just draw it - either clearing the area below, or using subtractive blending if thats possible in monogame.
For example in JavaFX, you can clear a rectangle of a drawing canvas with the method clearRect(), for reference.
Cheers

- 235
- 2
- 8
-
This is, of course, a very basic way of getting that effect. It won't stop at obstacles etc. For more complicated effects (not only circular, but e.g. ray casted, you would need a bit more math and proper shaders. Have a look at Stealth Bastard (official webpage is down) for example. – Ignatiamus Jan 09 '18 at 15:51