2
\$\begingroup\$

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 :).

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Thank you for the explanation, really helped clear things up for me :) \$\endgroup\$ – dan369 Apr 15 '12 at 2:12
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ what time you feel was best for your offsetAmount? \$\endgroup\$ – dan369 Apr 15 '12 at 2:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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