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

preview_player
Показать описание
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:

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
Рекомендации по теме
Комментарии
Автор

*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,
Graphics2D g2 =

// 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 😅

RyiSnow
Автор

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

laurenzfitzner
Автор

@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 :)

LeoOno
Автор

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.

kpm
Автор

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

God bless

accumulator
Автор

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

xioned
Автор

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

davyswanson
Автор

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

TheGreat
Автор

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

wrenwolf
Автор

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!

misterdneh
Автор

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

Lazullien
Автор

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?

hawkale
Автор

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.

MattListor
Автор

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?

_bit_coding
Автор

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

malikarslan
Автор

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

mcgames
Автор

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

shil
Автор

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

parunev
Автор

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?

rookie
Автор

Great tutorial! By the way are you japanese?

billseaman