Lighting Effect - How to Make a 2D Game in Java #44

In this video, we implement darkness and lighting effects that are provided by torch or lantern so we can create nighttime situations or dark interiors such as dungeons, caves, ruins, etc.
There are many methods to add lighting effects and this time we apply a so-called smooth lighting/soft lighting effect by utilizing Area, Rectangle2D, Ellipse2D, and RadialGradientPainclass.
Guidelines for using Blue Boy Adventure's code and assets:
docs.google.com/document/d/1q...
Thanks for watching.
Timestamps:
0:00 Introduction
0:45 Creating EnvironmentManager and Lighting class
3:30 What we're gonna do (Summary)
4:57 Creating a rectangle with a hall in the middle
14:12 First result (Spotlight effect)
14:56 Creating a gradation effect
21:20 Second result
22:24 Changing the size of the lighting area
23:37 Conclusion
#javagamedev #javagametutorial

Пікірлер: 87

  • @RyiSnow
    @RyiSnow Жыл бұрын

    *CODE UPDATE* I was tweaking the lighting system today and realized that I made a very dumb mistake in this video 😳 Well, technically it's not really a mistake but I included a totally unnecessary process. It turned out that you don't really need to do the clipping with those two areas. In fact, after creating the darknessFilter and setting centerX and centerY, you can just skip all the processes with the Area classes (Creating screenArea, lightArea, and subtract). You just need to create the RadialGradientPaint and apply it to the g2 and *draw a screen size rectangle*, then you get the same result. Here's the revised constructor: public Lighting(GamePanel gp, int circleSize) { // Create a buffered image darknessFilter = new BufferedImage(gp.screenWidth, gp.screenHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = (Graphics2D)darknessFilter.getGraphics(); // Get the center x and y of the light circle int centerX = gp.player.screenX + (gp.tileSize)/2; int centerY = gp.player.screenY + (gp.tileSize)/2; // Create a gradation effect Color color[] = new Color[12]; float fraction[] = new float[12]; color[0] = new Color(0,0,0,0.1f); color[1] = new Color(0,0,0,0.42f); color[2] = new Color(0,0,0,0.52f); color[3] = new Color(0,0,0,0.61f); color[4] = new Color(0,0,0,0.69f); color[5] = new Color(0,0,0,0.76f); color[6] = new Color(0,0,0,0.82f); color[7] = new Color(0,0,0,0.87f); color[8] = new Color(0,0,0,0.91f); color[9] = new Color(0,0,0,0.94f); color[10] = new Color(0,0,0,0.96f); color[11] = new Color(0,0,0,0.98f); fraction[0] = 0f; fraction[1] = 0.4f; fraction[2] = 0.5f; fraction[3] = 0.6f; fraction[4] = 0.65f; fraction[5] = 0.7f; fraction[6] = 0.75f; fraction[7] = 0.8f; fraction[8] = 0.85f; fraction[9] = 0.9f; fraction[10] = 0.95f; fraction[11] = 1f; // Create a gradation paint settings RadialGradientPaint gPaint = new RadialGradientPaint(centerX, centerY, (circleSize/2), fraction, color); // Set the gradient data on g2 g2.setPaint(gPaint); g2.fillRect(0, 0, gp.screenWidth, gp.screenHeight); g2.dispose(); } I guess I was overthinking when I constructed my theory in my head... Sorry about this. Hope you can utilize that shape-clipping knowledge for some other purposes 😅

  • @ChyluXIII

    @ChyluXIII

    Жыл бұрын

    Hello. Can u upload at github?

  • @laurenzfitzner
    @laurenzfitzner Жыл бұрын

    Another video with an amazing topic! Thank you, now it can be actually dark in the cave part of my game XD

  • @RyiSnow

    @RyiSnow

    Жыл бұрын

    Glad to hear that. I wish I could see how your cave looks!

  • @kpm25
    @kpm25 Жыл бұрын

    Hi RyiSnow! I found your channel after searching for help for my computer graphics paper using java swing. Thanks for your hard work! I started this tutorial series recently and am quickly catching up to the latest video. Much appreciated.

  • @davyswanson7412
    @davyswanson7412 Жыл бұрын

    thank you so much for this game tutorial,its amazing and helps me learn so much easier,hope you get a chance to do more tutorials in the future,great work

  • @accumulator4825
    @accumulator4825 Жыл бұрын

    This project seems awesome! Hope to see more when you have more time to work on it! God bless

  • @Nick-ns3vj
    @Nick-ns3vj Жыл бұрын

    Thanks, exactly what I needed!

  • @TheGreat8220
    @TheGreat8220 Жыл бұрын

    I love your videos. Thank you so much for each and every one!

  • @darkpaladin.
    @darkpaladin. Жыл бұрын

    You are truly amazing my friend ❤

  • @LeoOno
    @LeoOno Жыл бұрын

    @RyiSnow san, you did it! the final result is really excellent :)! i loved the tile based lighting too, besides rpgs, i think it would look great in rts games to hide areas not yet explored :)

  • @RyiSnow

    @RyiSnow

    Жыл бұрын

    Thanks Leo! Yeah, I liked the tile-based lighting a lot. It's pretty handy to place multiple light sources. And you're right, I think it can also be used to create a fog of war :D I'd love to develop a strategy game sometime (I'm a Fire Emblem guy so more into turn-based than RTS)

  • @Lazullien
    @Lazullien Жыл бұрын

    this is starting to look sick!

  • @xioned
    @xioned Жыл бұрын

    Just started learning Java And found out this. Im so Pleased and Grateful that you are making this ^_^. Thank you

  • @RyiSnow

    @RyiSnow

    Жыл бұрын

    You're welcome and good luck with learning Java!

  • @hawkale140
    @hawkale140 Жыл бұрын

    To do the torches at the end of the video, do you think it would make sense to use a similar method as the one in the video, but adding circles for every source of lights? Or did you put a "lighting" integer to every tile to add shadow over them? It looked super cool! I'll re-organize my code while waiting for new videos cause I had never cleared it and try to add something... Do you plan to do more episodes?

  • @uxvor3986
    @uxvor3986 Жыл бұрын

    Hey @ryisnow, I was wondering if I can add multiple soundtracks to my game which can be played when a music ends, can you tell me how is that done?

  • @TORMENTUMM
    @TORMENTUMM9 ай бұрын

    Thanks for this videos, are amazing

  • @hugodoquoc1262
    @hugodoquoc1262 Жыл бұрын

    hi I have a question. How could you make the light glow only on one fixed place for example around porch that is next to house?

  • @sketchbyk.v.s
    @sketchbyk.v.s Жыл бұрын

    Damn good tutorial

  • @mcgames9517
    @mcgames9517 Жыл бұрын

    are you going to make a tutorial on randomly generated maps in the future? really enjoying the series btw. good stuff

  • @RyiSnow

    @RyiSnow

    Жыл бұрын

    Yes, random map generation is definitely one of the themes that I'm planning to do in my future tutorial!

  • @kevincolgan6651
    @kevincolgan6651 Жыл бұрын

    This is amazing thanks

  • @64_bit_coding
    @64_bit_coding Жыл бұрын

    Hey I stumbled across your tutorial last month and I thought about implementing quests and missions, but I can't find a way to do that. Could you do a video on that?

  • @wrenwolf3753
    @wrenwolf3753 Жыл бұрын

    I've just started the playlist for these tutorials, and firstly I just wanted to say thank you! I really appreciate how much effort you put into your videos, and it's clear so many of your viewers feel the same way. You've already taught me so much, and I'm excited to learn more! I don't know if this has been asked already, and if you've gone over this in a video I haven't seen yet I apologize in advance haha but, say I wanted to make a very large map, maybe 20,000 tiles in height and the same amount in width, or maybe even larger; at what point (in terms of how many tiles) would rendering and overall hardware performance start to suffer? Would it be more realistic to make a single map that's 20,000 x 20,000 tiles, or would it be better to create multiple, smaller maps that would be loaded once the player's sprite reaches a boundary? Looking forward to catching up with the rest of these videos! Thanks again for all you do

  • @RyiSnow

    @RyiSnow

    Жыл бұрын

    > at what point (in terms of how many tiles) would rendering and overall hardware performance start to suffer? That depends on how fast your computer is so there's no universal threshold for it. In any case, we're only drawing tiles that are in our camera so the map size doesn't really affect the performance. I just tested a 1000x1000 map which is 20 times larger than the current one and the drawTime didn't change at all.

  • @wrenwolf3753

    @wrenwolf3753

    Жыл бұрын

    @@RyiSnow ahhh, that's true, I didn't even think about that. Thank you though, this is very good to know!

  • @Frombehindyo

    @Frombehindyo

    Жыл бұрын

    Just make sure you don't have too many objects (creatures and stuff) in the world that get updated, you don't want to call the AI for 1000 creatures when you only need a few. So either you spawn them in when you get close enough or you find a way to ignore them in the update method when they are too far away. It may get a bit complicated to code it, so maybe it's best to avoid huge maps and instead transition from one map to another.

  • @misterdneh
    @misterdneh Жыл бұрын

    This was so difficult for but awesome when it came to light 😁 get what I did there 😆 thank you my greatful teacher, I've creditted you if that is alright!

  • @MattListor
    @MattListor Жыл бұрын

    Is it possible to have glow around multiple items at once? Great tutorial! One day soon I will upload a video showcasing my game and the adjustments I made to it.

  • @lotakusupreme6009
    @lotakusupreme6009 Жыл бұрын

    Keep going, king!

  • @Nyctous
    @Nyctous Жыл бұрын

    do you think u might have a link to source code? i was having a issue on early episodes and dont know what i did wrong

  • @rianoliveira5378
    @rianoliveira5378 Жыл бұрын

    this is amazing

  • @MrLoser-ks2xn
    @MrLoser-ks2xn Жыл бұрын

    Thanks!

  • @malikarslan2672
    @malikarslan2672 Жыл бұрын

    will we see in another video what we will do with the load game from the title state

  • @parunev
    @parunev Жыл бұрын

    I stumbled across a problem. Can you give me advice how to set up a steady torch? Not moving one?

  • @Luke-vk2bx
    @Luke-vk2bx Жыл бұрын

    AMAZING!! I'm new Subcribers👍

  • @RyiSnow

    @RyiSnow

    Жыл бұрын

    Awesome! Thank you!

  • @Lazullien
    @Lazullien Жыл бұрын

    the player could be able to move diagonally, it's a small thing, but makes the experience so much better

  • @No-fc9jo
    @No-fc9jo Жыл бұрын

    I exported my game into a runnable jar with eclipse. But, when I open it and click the enter key to get into the game it just starts playing the music and it just stays in the title screen (Also the arrow keys do not move around the cursor for the options). It works fine running it in the IDE and goes into the game after pressing enter. Any ideas on how to fix this?

  • @RyiSnow

    @RyiSnow

    Жыл бұрын

    Jar is case-sensitive while IDE is not so that's one thing you want to check.

  • @berkayw5781
    @berkayw5781 Жыл бұрын

    Hi Ryi, I have an issue. When i swing sword or interact something(like taking potion, axe, key etc.) first time after the game starts, my game freezes for 0.5 - 1 seconds. Then everything goes normal. There is a bug or something i missed. I couldn't find out why it happens. Could you help me please? (my comment deleted idk why, video #28)

  • @berkayw5781

    @berkayw5781

    Жыл бұрын

    @Gnelion Ryi explained the solve at #28 video’s comments. It was about reading sound file. Check here : There is a little delay when you read a sound file for the first time. Play the field music when you press Start then there's no delay after that. You can also play a blank audio file if you don't want to play music.

  • @bluethemcguy4430
    @bluethemcguy4430 Жыл бұрын

    Hello RyiSnow, just wondering, do you have any idea why my FPS drops down to 42 or less when I implemented the lighting effect?

  • @RyiSnow

    @RyiSnow

    Жыл бұрын

    Hmm I'm not sure. What is your PC's spec (CPU & Memory)?

  • @bluethemcguy4430

    @bluethemcguy4430

    Жыл бұрын

    @@RyiSnow Here are my PC's specifications: Processor AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx 2.10 GHz Installed RAM 12.0 GB (9.92 GB usable)

  • @RyiSnow

    @RyiSnow

    Жыл бұрын

    That looks pretty good. You have more memory than mine. The CPU clock is a bit low but I'm not sure if that's the reason. It still looks good enough to me. That said lighting is pretty heavy stuff so I think we'd better utilize OpenGL if we go further and add more visual effects like this.

  • @shil2207
    @shil2207 Жыл бұрын

    Hi, thank you so much for the instructive videos. Please tell me how you can add falling raindrops?

  • @RyiSnow

    @RyiSnow

    Жыл бұрын

    I think rain will be featured in the next or next to next video :)

  • @Loggog

    @Loggog

    Жыл бұрын

    maybe you can use particles?

  • @shil2207

    @shil2207

    Жыл бұрын

    @@Loggog My self-study of the java language lasts only half a year). All I had enough knowledge to do was to create a rain from many elements (where each drop is a separate object). With this method of creation, the load on the computer is too great. After 10 objects, friezes and lags begin. I will study the issue with particles. Thank you)

  • @nedstarkx5637
    @nedstarkx5637Ай бұрын

    How would I allow multiple light sources? Could you make a tutorial for that?

  • @billseaman3103
    @billseaman3103 Жыл бұрын

    Great tutorial! By the way are you japanese?

  • @ezhelmoruq4307
    @ezhelmoruq4307 Жыл бұрын

    What if my character is not fixed on center, how can i change shape location

  • @PalaceLad
    @PalaceLad Жыл бұрын

    Hey man I love your videos and just recently caught up with your tutorial! I was browsing through the other kinds of videos you had on your channel and I saw that you made a tile editor, is there a link to download it or a tutorial I can find to remake it? It seems like it would be perfect for making maps for this game.

  • @RyiSnow

    @RyiSnow

    Жыл бұрын

    Sorry but there's no link (that editor needs a little edit to be used for this Blue Boy Adventure) or tutorial at this point. I think I will create a tutorial or just release the editor when I have a bit more time!

  • @hongquanpham3730
    @hongquanpham3730 Жыл бұрын

    Hi RyiSnow! Can you show me how to change from orientation attack with only 4 direction to target selection attack. I want my character to select the monster and item nearby automatically or by mouse click then press Enter to attack or pickup. Thanks !

  • @RyiSnow

    @RyiSnow

    Жыл бұрын

    Sorry but I don't think I will do that in this series since this game is programmed solely based on keyboard control. You'll need to re-program it from the ground up to implement mouse control otherwise you can't get its coordinates correctly. Keyboard control and mouse control require quite different control schemes.

  • @quentinfouilleul2651
    @quentinfouilleul2651 Жыл бұрын

    Nice :)

  • @Mr-Menace
    @Mr-Menace Жыл бұрын

    hey i have a idea the walking is a bit slow here what about implementing a stamina system when holding down shift you go a bit faster. just a suggestion

  • @haikamu7178

    @haikamu7178

    Жыл бұрын

    The implementation is, make a variable that contains health, if the health is upper than 5, set the player speed/velocity high, make an object that increase the health, if player collide with that object, make health += 5, and subtract the health when key is pressed

  • @orangomango
    @orangomango Жыл бұрын

    nice video

  • @misterdneh
    @misterdneh Жыл бұрын

    Oh man I already was trying this from your Farewell video but drew the image ovetlaying my main view instead. I am only 5 min in can't wait to see the rest

  • @rookie8960
    @rookie8960 Жыл бұрын

    Hi, cool video, what did you use to make the game? JavaFX? i'd like to create a card game like Monopoly/UNO or Blackjack, which GUI library would you suggest me?

  • @RyiSnow

    @RyiSnow

    Жыл бұрын

    This is not JavaFX. Just standard Java. As for GUI to create Monopoly/Uno/Blackjack, either the standard Java classes or JavaFX can handle it nicely. It depends on what you want to learn. I've made a Blackjack with the standard Java classes before. If you are interested, check: kzread.info/dash/bejne/c6l-lZuTfNSofqw.html

  • @rookie8960

    @rookie8960

    Жыл бұрын

    @@RyiSnow Thanks a lot, for the standard java classes you mean Java Swing, right?

  • @RyiSnow

    @RyiSnow

    Жыл бұрын

    Swing, AWT, Util, IO... those are the standard Java class library. Swing is only a small part of this program.

  • @rookie8960

    @rookie8960

    Жыл бұрын

    @@RyiSnow Its Swing enough for a card game right?

  • @MRCN49
    @MRCN49 Жыл бұрын

    Hello! I have a question. How can I be more efficient when creating a map? Because typing numbers in a .txt file is such a pain.

  • @kpm25

    @kpm25

    Жыл бұрын

    You could make a small program that converts a color image (say made in paint or whatever) into a map.txt file of same format with the different colors representing different tiles.

  • @MRCN49

    @MRCN49

    Жыл бұрын

    @@kpm25 Thanks you! I've already figured out a much more efficient way than writing in .txt, changed the reading from the file to read the .csv file instead of a .txt file, and with the "Tiled" tool I can build maps faster.

  • @kpm25

    @kpm25

    Жыл бұрын

    @@MRCN49 Cool, I'm only just starting this series today, looks like fun!

  • @MRCN49

    @MRCN49

    Жыл бұрын

    @@kpm25 Good luck!

  • @user-uo7nd6ds1v
    @user-uo7nd6ds1v Жыл бұрын

    Hello. You're very cool at it. Can you drop the download link for this game? Just not the old version. Thanks for understanding.

  • @RyiSnow

    @RyiSnow

    Жыл бұрын

    I will do that once the game is complete so pls wait.

  • @bluethemcguy4430
    @bluethemcguy4430 Жыл бұрын

    Hi RyiSnow, if possible could you give me a thought on how to implement infinite world generation? I really want to implement this in my game. Also, THANK YOU SO Much for this video. I really needed it.

  • @misterdneh

    @misterdneh

    Жыл бұрын

    If you mean you want to load chunks randomly let's say the further the player goes, then you can simply (not so simply) do so by generating the size of the world to be larger everytime the player reaches a specific point. Minecrafr for example, renders the world when the player moves in specific directions. TECHNICALLY the world isn't infinite but I won't travel 1Trillion blocks ever 😆. Anyways I hope this sort of helps

  • @RyiSnow

    @RyiSnow

    Жыл бұрын

    @BlueTheMCGUY You need to construct an algorithm so it won't place tiles just randomly but with a certain rule otherwise, the world wouldn't look natural. A very simple example would be like, "if tile 1 is water, only water or sand tile can be placed next to it". The more you set these rules, the more the world looks natural.

  • @bluethemcguy4430

    @bluethemcguy4430

    Жыл бұрын

    @@RyiSnow Thanks so much for the response!

  • @bluethemcguy4430

    @bluethemcguy4430

    Жыл бұрын

    @@misterdneh Thanks for the response and explanation!

  • @misterdneh

    @misterdneh

    Жыл бұрын

    @@bluethemcguy4430 you're welcome!

  • @MrLoser-ks2xn
    @MrLoser-ks2xn7 ай бұрын

    🥰🥰🥰

  • @iarleyyyxx
    @iarleyyyxx Жыл бұрын

    Upload it at the github.

  • @hamiltongoerge499
    @hamiltongoerge499 Жыл бұрын

    Hello RyiSnow, just wondering, do you have any idea why I get this error when I run the lighting effect: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Environment.EnvironmentManager.draw(EnvironmentManager.java:21) at main.GamePanel.paintComponent(GamePanel.java:98) at javax.swing.JComponent.paint(JComponent.java:1056) at javax.swing.JComponent.paintChildren(JComponent.java:889) ... The issue I found that causes this is, when lighting.draw() is called in EnvironmentManager, it is null. I checked, and the lighting constructor is being called, but for some reason when g2.fill or g2.fillrect is called, it exits early and runs the lighting.draw() command, even though it didn't finished the contructor. Any idea?

  • @nque8435

    @nque8435

    Жыл бұрын

    I fixed it using try and catch public void draw(Graphics2D g2) { try { lighting.draw(g2); } catch (Exception e) { } }