April 3, 2015

Basic Lightning Theory

  1. Introduction
  2. The Good Stuff
  3. Are You Done?

Introduction

Every good game needs good visual effects. Here is how to do basic lightning. This only covers 2D lightning, but you could easily extend this to 3D.

The Good Stuff

Define 2 points, A and B.

AB Sorry, your browser does not support inline SVG.

Draw a line to link the 2 points up.

AB Sorry, your browser does not support inline SVG.

Find the middle of the line AB, and mark it (M).

ABM Sorry, your browser does not support inline SVG.

Add a displacement to the coordinates of M (should be random), then link AM and MB together. In this case, M is displaced (-18, -13) to create M'.

ABMM' Sorry, your browser does not support inline SVG.

Are You Done?

Congratulations! You've got the basics! Now, just rinse and repeat with your new lines AM' and M'B! Recursion works wonders, doesn't it! Here is an implementation of this basic lightning thingy in Python and pygame!

This little thing has the lightning originate from the middle of the window and arc out to attack your cursor if you click on the screen. I made this to test the lightning concept for a tower defense game that I was inspired to create.

Tags: lightning visual theory pygame python