I'm new to this area and have less knowledge. I want to create some graphics like sparks and lightning by writing some code, using my own physics. I don't want to use some engine, However I can use external libraries.

Can anyone tell me how to set started, where should I proceed? Like should I go with OpenGL for writing my own code or something else.

  • 2
    $\begingroup$ For sparks, try a Google for "particle systems" $\endgroup$
    – Alan Wolfe
    Sep 11, 2016 at 15:55
  • $\begingroup$ As for the second part of your question, I would start with Processing, or maybe OpenFrameworks if you know some C++ already. These both use OpenGL under the hood but don't force you to write 100 lines of boilerplate just to get a window open... $\endgroup$
    – russ
    Sep 18, 2016 at 5:37

1 Answer 1


For the lightning, I recommend using a midpoint displacement algorithm. You start with a line segment between any 2 points A and B (this works in either 2D or 3D). Calculate the midpoint of the segment AB. Now move that point a random amount in the direction perpendicular to the line segment AB and call it point C. Replace the original segment AB with 2 line segments AC and CB. Repeat with the new line segments until you are satisfied with how it looks. In my experience you'll need somewhere between 5 and 10 iterations depending on the resolution you're working at. You can add a glow to the resulting geometry to make it look more realistic.

For sparks you need to make a particle system. These are pretty fun. You generally have a point (or other shape) that generates particles every few milliseconds. That is, particles are spontaneously formed on the point or boundary of the shape periodically. Each particle has various properties such as location, velocity, color, size, lifetime, etc. When animating, you can update the velocity and position based on various forces such as gravity, wind, or some sort of convection current or flow field. After a particle has reached its "lifetime" it can disappear, or fade out, and a new particle will be generated in its place at the point or on the boundary of the shape. The particles themselves can be drawn as single pixels, small shapes, or images. There's an interesting wikipedia article on particle systems.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.