LibGDX Lightning Effect Code

To an Android Game I am currently developing slowly, I thought a Lightning effect would look good to link and show something is in effect.

I first tried to create my own Lightning, but my first go didn’t look exactly as I wanted it.

I remembered seeing a post about someone sharing some source code on a game, and decided to look for it.

After talking to Cero about his lightning code here, I went through with it and implemented it into my game.

Since implementing the code into my game, I decided to modify Cero’s original code, to make it more friendly to use, by setting some methods to private and also adding a few duplicate methods to allow more control or less code required to use.

Source Code
Source Code can be found here

Implementation
The GFX class is completely static, you do not need to create a new instance of it, just supply the texture once, and use it when you need to.

During your creation of the game, in the create() method add the line


GFX.setTexture(new Texture("image.png")); //set a texture for it to use, 1x1 white image is all that is required

Preview of the effect:

Default Chain Lightning

Red and Yellow Chain Lightning

Multi Coloured Chain Lightning

Chain Lightning in a random position

Default Sphere Lightning

Red and Yellow Sphere Lightning

Multi Coloured Sphere Lightning

Using a 1x1 white image will produce the lightning a little differently to what is seen in the previews, if you would like the image I used you can get it here

If you have any suggestions for improvements, let me know, and I will see what I can do.

Thanks to Cero for letting me use his code, the original can be found here.

Nice… Thanks for sharing. Might probably have a use for this in my project.

When I wrote this code a year ago I made this video:

j7DX4Eyrh-Y

the texture, the filtering, shaders, adding particle effects and some clever coding can make this stuff way more beautiful of course.
Also to make it simpler @Phased took out a performance enhancement where I use the Sqrt class with precalculated values to speed up the get distance calculation

Furthermore note that this code was not intended for third party use and was just part of my code base and I posted it, meaning it might still be clunky and there are some hardcoded values to some extend.

Actually, I just changed from my getDistance() to your original implementation.

Looking through the code again, in your original implementation, you decided to use the getDistanceAccurate() method, which is the code implemented in the one on this thread.

Your Trigonemtrics class had getDistanceAccurate() and getDistanceFast(), and your GFX class used getDistanceAccurate().

About the hard coded variables, there is a few, but I did mess around with them, and then numbers you chose in my opinion are the actual “magic number” they made it look the best in my opinion.

I was going to add a method to set them, so the user could customize it, but I didn’t worry as it didn’t really improve it.

Really huh ? Well the code is a year old so there goes my memory. Should be fine with the fast variation… I might change it in my code base