Here is a simple 24-hour clock, driven by the millis() command.
Every 500 milliseconds (every half second) it counts up a game-hour towards 24. It loops back around from 23 (11pm) to 0 (12a). You can slow down the clock by making "500" larger -- a game-hour per second (1000) or a game-hour per minute (60000).
The sun is drawn in front of the scene and moves only to the edges of the screen to make it very easy to see what the hour (noon is up, midnight is down) and how the blueness of the sky and the lights in the windows react to the hour.
There is one tricky line here:
light = sin(TWO_PI * hour/24 - 8);
This takes the hour of day and feeds it into a sine wave such that:
size(500,500);
background(255,0,0);
noStroke();
fill(0,125);
rect(50,50,400,400);
fill(255,125);
rect(100,100,300,300);
fill(255);
text("Why this color is not same\n as default background ?",150,150);
One approach to having a pool of light that shows the background is to tint a version of the background image on a PGraphics, mask it with a circle (or a partially transparent set of nested circles) and then draw it under the Player.
George, do you realize how much work jeremy put into this? And now you say you want to have something totally different. Please describe longer what you mean in future.
Here is a demo of the compositing order to create a simple player flashlight / fog-of-war effect. Parts of the background are hidden (e.g. in shadow) and the player presence reveals them (using a mask).
/**
* PlayerLight
* Demonstration of a simple flashlight / fog-of-war type effect,
* in shadowed parts of the background are only visible near the player.
* 2017-10-18 Jeremy Douglass - Processing 3.3.6
* forum.processing.org/two/discussion/24534/how-to-create-2d-lights-for-game
*/
PImage bg;
PImage light;
PGraphics lightMask;
void setup() {
bg = loadImage("https://processing.org/reference/images/PImage.png");
bg.resize(width, height);
light = bg.copy();
lightMask = createGraphics(width, height);
}
void draw() {
background(0);
image(bg, 0, 0);
shadow();
drawLight(mouseX, mouseY, 30, lightMask);
light.mask(lightMask);
image(light, 0, 0);
drawPlayer();
}
void shadow() {
pushStyle();
noStroke();
int step=16;
for (int i=4; i<=step; i++) {
fill(0, 64);
rect(i*width/step, 0, width, height);
}
popStyle();
}
void drawLight(float x, float y, float radius, PGraphics pg) {
pg.beginDraw();
pg.noStroke();
pg.fill(255);
pg.background(0, 0);
pg.ellipse(x, y, radius, radius);
pg.endDraw();
}
void drawPlayer() {
pushStyle();
fill(0, 255, 0);
ellipse(mouseX, mouseY, 10, 10);
popStyle();
}
These effects can also be used to create a looking-glass effect, e.g. for hidden object games.
Answers
Here is a simple 24-hour clock, driven by the
millis()
command.Every 500 milliseconds (every half second) it counts up a game-hour towards 24. It loops back around from 23 (11pm) to 0 (12a). You can slow down the clock by making "500" larger -- a game-hour per second (1000) or a game-hour per minute (60000).
Now the game knows what game-hour of the game-day it is.
Let's use the hour (0-23) to set how light or dark the sky is.
The sun is drawn in front of the scene and moves only to the edges of the screen to make it very easy to see what the hour (noon is up, midnight is down) and how the blueness of the sky and the lights in the windows react to the hour.
There is one tricky line here:
This takes the hour of day and feeds it into a sine wave such that:
But i want to create shadow mask for player :/ and on this mask i want create light mask...
i want to this.... Test it:
Can you explain a little more about this please
What do you want to achieve?
I want to make light when is night :D
I don't understand what you are saying.
Use. More. Words.
I want to do the night in the game. And when the player is there, I want him to have a flashlight so light is around him
Ah. See the tint() and mask() examples.
One approach to having a pool of light that shows the background is to tint a version of the background image on a PGraphics, mask it with a circle (or a partially transparent set of nested circles) and then draw it under the Player.
George, do you realize how much work jeremy put into this? And now you say you want to have something totally different. Please describe longer what you mean in future.
You haven’t even said Thanks....
Can you create exemple ?
No.
THX !
@GeorgeJava -- glad you got your tint/mask working.
Would you please share your solution here?
That could help future learners on the forum.
Here is a demo of the compositing order to create a simple player flashlight / fog-of-war effect. Parts of the background are hidden (e.g. in shadow) and the player presence reveals them (using a mask).
These effects can also be used to create a looking-glass effect, e.g. for hidden object games.
WOOOW :O THX VEEEERYYY MUCH :) :O
And how create more lights ? :/
Not worked but background was restarting light... new code:
But how to make collision with other circle of lights ?
What do you mean? Use more words.
By collision, do you mean:
http://www.jeffreythompson.org/collision-detection/circle-circle.php
when two lights collide it looks strange ... Try it :D
I don't know what you mean by "when two lights collide." It looks fine to me.
Also, the code you posted is broken. Try it.
Use more words! By more words, I mean that you should write more than ten words. Try twenty words, or thirty words.
Explain.
Hah.
If you want additive light (I guess), at the top of
drawLight()
addpg.blendMode(ADD);
To understand why, read:
Good, but :D i take this code for create darkening circle of light and now not working :/
CODE FOR CIRCLE:
NICE !!! I HAVE IT :D can i make color of light ? plsss :) :D
To color tint the masked image before drawing it:
VERY NICE :O THX A LOT !
Looks good. Good luck!
Also, I see you corrected it from earlier, but remember:
If you care about FPS, DON'T do this:
The five-argument form of
image()
is extremely slow. Instead use.resize()
once in setup, then use it with:And how to create more fps ? :D but i have only 20... or more down.. :/
I dont no where is problem...
Why dont working image ?
For more speed, don't call resize every frame. Very slow.
For pasting code into and out of the forum, break urls into 2 strings:
How to make colored lights ? Every light have own colors... Can it works?
How create more colors of lights ?
WHERE IS THE PROBLEM ?