This is actually my first question on stackoverflow :)
Firstly, I wanted to say that I am new to coding and I am wanting to get an understanding of some syntax. Basically, I am following a tutorial by the codetrain - creating an object orientated fractal tree, I am wondering based on the code below, how I would make it more '3D'. One way I thought of this, is through rotating the vectors. I have been looking for potential solutions to rotating the following vector in 3D space. The example codetrain uses is to make a direction vector: direction = p5.Vector.Sub(this.begin, this.end). He then uses the code direction.rotate(45). I realise that you can't write direction.rotateY(45). I saw from the p5.js syntax that .rotate() can only be used for 2D vectors. Is there, therefore, an syntax that I have somehow overlooked that rotates the vector in 3D space based on the following code?
Here is the code for the sketch. The Branch class controls tree construction, and the branchA
and branchB
function are where some rotation might be added.
var tree = [];
var leaves = [];
var count = 0;
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
angleMode(DEGREES)
var randomangle = random(0, 90)
var randomMinusAngle = random(0, -90)
var a = createVector(0, 0, 0);
var b = createVector(0, -100, 0);
var root = new Branch(a, b);
tree[0] = root;
}
//INPUT.
function mousePressed() {
for (var i = tree.length - 1; i >= 0; i--) {
if (!tree[i].finished) {
tree.push(tree[i].branchA())
tree.push(tree[i].branchB())
}
tree[i].finished = true;
}
count++
if (count === 8) {
for (var i = 0; i < tree.length; i++) {
if (!tree[i].finished) {
var leaf = tree[i].end.copy();
leaves.push(leaf);
}
}
}
}
function draw() {
background(51);
//orbitControl();
rotateY(frameCount);
translate(0, 200, 0);
for (var i = 0; i < tree.length; i++) {
tree[i].show();
tree[i].rotateBranches();
//tree[i].jitter();
}
//LEAVES
for (var i = 0; i < leaves.length; i++) { //should make the leaves an object in new script.
fill(255, 0, 100);
noStroke();
ellipse(leaves[i].x, leaves[i].y, 8, 8);
}
}
function Branch(begin, end) {
this.begin = begin;
this.end = end;
//bool to check whether it has finished spawning branch.
this.finishedGrowing = false;
this.show = function() {
stroke(255);
line(this.begin.x, this.begin.y, this.begin.z, this.end.x, this.end.y, this.end.z);
}
//var rotateValue = random(0, 45)
this.branchA = function() {
var direction = p5.Vector.sub(this.end, this.begin);
direction.rotate(45)
direction.mult(0.67);
var newEnd = p5.Vector.add(this.end, direction);
var randomss = p5.Vector.random3D(this.end, newEnd)
var b = new Branch(this.end, newEnd);
return b;
}
this.branchB = function() {
var direction = p5.Vector.sub(this.end, this.begin)
direction.rotate(-15);
direction.mult(0.67);
var newEnd = p5.Vector.add(this.end, direction);
var b = new Branch(this.end, newEnd);
return b;
}
this.rotateBranches = function() {
// TODO
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.1/p5.min.js"></script>