PDA

View Full Version : Gnarly Tree



freeskier89
February 1st, 2006, 08:36 PM
Hello there!

Here is a little recursive branching thing I whipped up today, because I had nothing better to do. It is made entirely with actionscript. I was intending it to go in the 25 line AS contest, but I guess Kirupa shut down the submissions today :( I thought today was the last day to submit. No big deal though. Please feel free to comment :)

function Tree(x, y, direction, splitD, splitN, lw, z) {
this.Mc = _root.createEmptyMovieClip("wanderer"+(depth++), depth);
this.Mc.Location = {x:x, y:y};
this.Mc.Direction = direction;
this.Mc.splitD = splitD;
this.Mc.splitN = splitN;
this.Mc.lw = lw;
this.Mc.lineStyle(lw, 0, z);
this.Mc.z = z;
this.Mc.moveTo(x, y);
this.Mc.onEnterFrame = function() {
this.Direction += Math.random()*50-25;
this.distTraveled += 2;
if (this.distTraveled>this.splitD) {
for (var n = 0; n<splitN; n++) Tree(this.Location.x, this.Location.y, this.Direction+Math.random()*40-20, this.splitD*.9, 2, this.lw*.8, this.z*(.6+.4*Math.random()));
this.onEnterFrame = null;
} else {
this.Location.x = this.Location.x+2*Math.cos(this.Direction*Math.PI/180);
this.Location.y = this.Location.y+2*Math.sin(this.Direction*Math.PI/180);
this.lineTo(this.Location.x, this.Location.y);
}
};
if (lw<2) this.Mc.removeMovieClip();
}
onMouseDown = function () {
Tree(_xmouse, _ymouse, -80-Math.random()*20, 30+_ymouse/20, 2, 12, _ymouse/4);
};
Oh btw I reccommend that you not let more than two trees generate at a time. It will get very very laggy, and it might crash your system.

rmo518
February 2nd, 2006, 01:40 AM
freeskier89, glad that you made it in under the wire too . . .

Cool script too, you can make a creepy forest like something out of an Edgar Alan Poe story with this.

freeskier89
February 2nd, 2006, 02:15 AM
Thanks fellow charter member of the Oops -I-Misunderstood-the-Deadline Support Group :) I just realized that the enterFrame event is not being killed correctly, so it continues to lag after the tree is being generated.

calvintage
February 2nd, 2006, 02:23 AM
This is amazing. At first I thought the tree's branches were digging into the ground and thought hmm... not very realistic... and then BOOM! The viewer is actually looking below at the trees. So cool. Nice use of fractals.

hybrid101
February 2nd, 2006, 06:23 AM
it's cpu intensive near the end, but it looks awesome, i havent seen curves used in tree fractals before!