<p>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.</p>

<p>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 <a href="https://en.wikipedia.org/wiki/Particle_system" rel="nofollow">interesting wikipedia article on particle systems</a>.</p>

CC BY-SA 3.0
2016-09-15T03:25:15.923Z
2016-09-15T03:25:15.923Z
user1118321
<p>I'm new to this area and have less knowledge. I want to create some graphics like  <strong>sparks and lightning</strong> by writing some code, using my own physics. I don't want to use some engine, However I can use external libraries. </p>

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

CC BY-SA 3.0
2016-09-11T15:55:13.157Z
For sparks, try a Google for "particle systems"
Alan Wolfe
CC BY-SA 3.0
2016-09-18T05:37:32.630Z
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...
russ
<3d><physics><projections>
Creating Sparks using Code
sdfsdf
# Creating Sparks using Code

(Asked by: username_0 on 2016-09-11)

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.

**Tags:** 3d, physics, projections

> **Comments:**
> **username_1**: For sparks, try a Google for "particle systems"  
> **username_2**: 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...  

---

## 1 answers

### Answer by username_3 on 2016-09-15. Score: 3

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](https://en.wikipedia.org/wiki/Particle_system).


---

