Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking into creating a 2D line branch, something for a "lightning effect". I did ask this question before on creating a "lightning effect" (mainly though referring to the process of the glow & after effects the lightning has & to whether it was a good method to use or not); Methods of Creating a "Lightning" effect in 2D

However i never did get around to getting it working. So i've been trying today to get a seconded attempt going but i'm getting now-were :/.

So to be clear on what i'm trying to-do, in this article posted;

I'm trying to create the line segments seen in the images on the site. I'm confused mainly by this line in the pseudo code;

// Offset the midpoint by a random amount along the normal.
midPoint += Perpendicular(Normalize(endPoint-startPoint))*RandomFloat(-offsetAmount,offsetAmount);

If someone could explain this to me it would be really grateful :).

share|improve this question
up vote 0 down vote accepted

That line gets a unit vector (vector of length 1) from startPoint to endPoint


then gets a vector perpendicular to that (i.e. at right angles to the line)


then multiplies it by some amount with a range (+ve or -ve).

* RandomFloat(-offsetAmount,offsetAmount)

It then adds this to midPoint thereby offsetting it.

share|improve this answer
Thank you for the explanation, really helped clear things up for me :) – dan369 Apr 15 '12 at 2:12

I also tryied to implement "lighting" effect into my engine and my part of code for your problem is follows:

Random rand=new Random();
Vector3 dir = Vector3.Normalize(segment.endPoint - segment.startPoint);
midPoint += new Vector3(dir.Y, -dir.X, 0) *(float)(rand.NextDouble()*(offsetAmount*2)-offsetAmount);

Trick is hidden here:

  new Vector3(dir.Y, -dir.X, 0)

This creates normal vector, witch is perpendicular.

share|improve this answer
what time you feel was best for your offsetAmount? – dan369 Apr 15 '12 at 2:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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