Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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; http://drilian.com/2009/02/25/lightning-bolts/

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
add comment

2 Answers

up vote 0 down vote accepted

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

Normalize(endPoint-startPoint)

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

Perpendicular(Normalize(endPoint-startPoint))

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
add comment

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
add comment

Your Answer

 
discard

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.