Yesterday i asked this question on how to create a 2D line branch; Creating a 2D Line Branch

And thanks to the answered provided, i now have this nice looking main branch; *coloured to show the different segments in the final item. The 5th Generation Line (32 segments)

Now is the time now to branch things off as discussed in the article; http://drilian.com/2009/02/25/lightning-bolts/

Again however i am confused as to the meaning of the following pseudo code;

splitEnd = Rotate(direction, randomSmallAngle)*lengthScale + midPoint;

I'm unsure how to actually rotate this correctly. In all honesty i'm abit unsure what to-do completely at this part, "splitEnd" will be a Vector3, so whatever happens in the rotate function must then return some form of directional rotation which is then * by a scale to create length and then added to the midPoint.

I'm not sure. If someone could explain what i'm meant to be doing in this part that would be really grateful.


1 Answer 1


Instead of asking a series of questions on specifics (normally the right thing to do on StackExchange, yes), you would be far better served by simply learning the basics of the linear algebra that you're running up against.

To give a slightly more directed answer, the Rotate function must perform a basic 2D rotation of the direction vector by the angle randomSmallAngle. This can be done with some very simple math, explained in most of the links above, or you can just use the XNA Matrix.CreateRotationZ() method to create a rotation matrix to transform the direction vector by.

The rotation does not return "some form of directional rotation" but in fact just returns a regular vector. It's taking the original direction, rotating it slightly left or right, and returning that. The lengthScale multiplication just scales that vector, changing its lengths. Finally it's added to the midPoint, giving a vector that's a position relative to the midPoint.

  • \$\begingroup\$ Its defiantly something i need to improve upon, so thank you for the links :). So i've been reading the links, and i've come up with this; Vector3 splitEnd = Vector3.Transform(midPoint, Matrix.CreateRotationZ(MathHelper.ToRadians(rand.Next(-45, 45)))) * lengthScale + midPoint;. It seems to be working, just wanted to make sure however. \$\endgroup\$
    – dan369
    Commented Apr 15, 2012 at 11:19
  • \$\begingroup\$ Looks reasonable. \$\endgroup\$ Commented Apr 16, 2012 at 3:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .