Box2d lights - lights over sprite - java

I wanted to ask if you can use box2d lights so you can see only objects that are in the lights area. For example i have a flashlight and only want to see game objects in the light. I managed to do something like this but the problem is that the sprites of the game objects lose their color intensity because I render lights on top of sprites and the game itself doesn't look good because of this (even though it is the effect that i want).I used box2d bodies with user data containing sprites. I can't figure any way out. Is there any proper way to use box2d lights library to make these objects visible and with their real color? (I am setting lights to X-rays to do this; also I am using it with libgdx in java).

It may be because the default setting is to not use diffuseLight. You have to set rayHandler.useDiffuseLight(true).
Libgdx and Box2DLights - too bright + colors grayed out

Related

Make body borders invisible/different color

Java LibGdx Box2d
I would like to make body borders invisible or at least different color. One body should work as sensor and thus not be visible on the screen. I could not find a valid answer for that in hours and I cannot believe there is no such option in this library so I assume I must have missed one liner somewhere in Docs.
I would like to make this square transparent/invisible or at least different color but still keep it to discover movement of these circles.
Looking over stackoverflow and googling it in docs.
Closest things I found:
Make invisible body line box2d libgdx
so maybe the only solution to this is setting some flags in render method or unique render methods for each body?
What I can see on your screenshot is Box2d's shapes for debugging.
They were not designed to have adjustable visuals and are here only to show you the precise shapes and positions of your Box2d's entities.
According to the wiki (https://libgdx.com/wiki/extensions/physics/box2d#debug-renderer):
Debug Renderer
The next thing we are going to do is setup our debug renderer. You generally will not use this in a released version of
your game, but for testing purposes we will set it up now like so: ...
You will need some other kind of renderer if you want to adjust the visuals. For example:
ShapeRenderer, for simple shapes (https://libgdx.com/wiki/graphics/opengl-utils/rendering-shapes)
SpriteBatch, for textured visuals of any kind (https://libgdx.com/wiki/graphics/2d/spritebatch-textureregions-and-sprites)
PolygonSpriteBatch / CpuSpriteBatch / any custom renderer you may find or create yourself
Retrieve the positions / shapes / sizes of your Box2d entities and use one of the more advanced renderers to draw shapes or textures in positions of your Box2d entities. Box2d is not designed for drawing stuff, it just simmulates the physics and gives you position / rotation of bodies and some other info about its world.
Just make the box the same color as the background. Adding color to lines Box2d (EdgeShape)
This post shows how to change the color

Creating a "shadow" layer for the canvas in JavaFX?

I'm currently remaking a game I made several years ago in Swing/AWT, this time using JavaFX. My current dilemma is that the original game had a "flashlight", in which I first created a blank black layer which I would then create a polygon on and subtract it from that layer using a blend mode. From there, the layer was drawn with transparency to give the appearance that everything was dark and the player had a flashlight.
I'm having trouble figuring out how to implement this in JavaFX. I figured I could create a blank black image and that there would be someway to create a GraphicsContext out of that and I could set a blend mode to subtract from the image, but images provide no support for this type of rendering in JavaFX, and in fact, WritableImage is a separate class which only allows the use of a PixelWriter, where I have to set each pixel manually.
I know there's probably plenty I still don't understand about JavaFX, because I've only made a few applications with it before. Does anyone have any recommendations about how to go about implementing this feature? It would be nice if I could make it look better than the original.
Here's a picture from the old game for reference.
I was able to achieve something similar to what I'm looking for by drawing the black layer in between when all game entities are drawn and when the HUD is drawn. I draw it with 80% opacity, and then I draw my flashlight polygon with 5% opacity and blend mode SRC_OVER. It took a lot of playing around with the different blend modes, but MULTIPLY was definitely not the way to go.
I created two Canvas, the "bright-world"-canvas and a second "night-overlay"-canvas with just a night-grey rectangle and light-images.
If I want night in my game I add the night canvas with Blendmode.MULTIPLY.
Here I create the night-overlay: (simplified)
private void renderLightEffect()
{
ShadowMaskGc.setFill(shadowColor);
ShadowMaskGc.fillRect(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
for (Sprite sprite : passiveCollisionRelevantSpritesLayer)
{
Image lightImage = lightsImageMap.get(lightSpriteName);
ShadowMaskGc.drawImage(lightImage, sprite.getPositionX() + sprite.getHitBoxOffsetX() + sprite.getHitBoxWidth() / 2 - lightImage.getWidth() / 2 - camX, sprite.getPositionY() + sprite.getHitBoxOffsetY() + sprite.getHitBoxHeight() / 2 - lightImage.getHeight() / 2 - camY);
}
}
After creating the night-layer a add it to the Pane every render cycle:
root.getChildren().clear();//root-Pane
root.getChildren().add(worldCanvas);//The world with all sprites
//LightMap
if (shadowColor != null) {//To switch on/off night
renderLightEffect(currentNanoTime);//update of the positions of the light points
root.getChildren().add(shadowMask);//grey Canvas with some light points
shadowMask.setBlendMode(BlendMode.MULTIPLY);//Here is the magic merge the colors
}
EDIT: Later I realized, that the two-canvas approach is bad for performance. I recommend to draw the shadow layer directly on the world canvas.
Result:

Java 2D- Actual Player Collision Detection | Ignore Transparency

So I'm creating a 2D game platform that works with Tiles. Currently it won't let the player go through the any tiles and works fine. Though rather then stopping the player at a solid tile. I would like to stop the player at an actual object. Pretend the triangle is in a tile.
Whats Happening:
What I want:
I would like the player to be able to walk through the tile until their is no more transparents. Basically walk on the triangle.
Player Class http://pastebin.com/SJrzSvVV
Tile Class http://pastebin.com/V3nqxh61]
TileMap Class http://pastebin.com/fuj8dR5K
You should check out the intersects() method provided by the Java2D API.
You can define the two sprites as shapes and adjust the coordinates when the two shapes intersect. I'm guessing you are splitting up a BufferedImage to create the animation frames.
You can draw a Rectangle or Ellipse frame around your sprites and check those for collisions. The frames do not have to be visible but making them visible helps when debugging.
Figured it out, essentially what I did was I was creating this game using tiles. I created something to scan a tile and find out where it's transparent, and allow players to go through it or not depending on whether the transparency is already inside something in the tile or not.

box2d - How do I render lights in a proper way?

Can you modify the darkness you get on screen when using a ray handler in libgdx with box2d world? Also when I put lights over sprites they look different because the light is on top of them. Can you render lights so that the color of the sprite is the same, even though it is lighted up by a source?
Yes. You can modify the darkness by setting ambient light. For instance:
rayHandler.setAmbientLight(0.5f);
makes everything 50% brighter.
Also, if you find that the lights over your sprites cause too much of a difference in colour, you might want to set the light's colour to white and set the alpha lower (just tweak around with it until you think it looks good).

Drawing shapes with transparent colors much slower than opaque colors in Java

I am currently working on making a Java videogame with a few friends. It is a top-down RPG that is drawn with tiles of different types. I just added a day/night cycle to the game, and found that using transparent colors to draw to the screen is very slow. I recently learned how to make colors transparent by adding an alpha-value after the RGB values, and it works very well, but as I said, it is slow. I tried switching between transparent colors and opaque colors and discovered that the lag is because of the transparency. Basically, after a tile is drawn, it calls the method for getting the desired transparency factor, makes a new color, and calls g.fillRect() over the tile. This is done for every tile on screen.
I would like to know if there is another way to have a transparent overlay without just drawing the overlay over the whole window, because we want to be able to add light sources that replace the transparency for specific tiles. The tiles are drawn to the screen from a spritesheet. Any suggestions are welcomed.

Categories