2

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

dan369
  • 983
  • 9
  • 19

2 Answers2

1

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.

George Duckett
  • 2,875
  • 24
  • 30
1

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.

Vodáček
  • 852
  • 1
  • 7
  • 24