xxxxxxxxxx
58
class Rock {
constructor(line_min = 10, line_max = 100) {
let p1 = createVector(0, 0, 0);
let p2 = createVector(random(line_min,line_max), 0, 0)
let p3 = createVector(random(p2.x), random(line_min, line_max), 0)
let p4 = createVector(p3.x, random(p3.y), random(line_min, line_max))
let centroid = createVector((p1.x+p2.x+p3.x+p4.x)/4,
(p1.y+p2.y+p3.y+p4.y)/4,
(p1.z+p2.z+p3.z+p4.z)/4)
p1 = p1.sub(centroid)
p2 = p2.sub(centroid)
p3 = p3.sub(centroid)
p4 = p4.sub(centroid)
this.p1 = p1
this.p2 = p2
this.p3 = p3
this.p4 = p4
this.face1 = [ p2, p3, p4, this.fnorm(p2, p3, p4) ]
this.face2 = [ p1, p4, p3, this.fnorm(p1, p4, p3) ]
this.face3 = [ p1, p2, p4, this.fnorm(p1, p2, p4) ]
this.face4 = [ p1, p3, p2, this.fnorm(p1, p3, p2) ]
this.spin_x = random(-0.008, 0.008)
this.spin_y = random(-0.008, 0.008)
}
fnorm(p1, p2, p3) {
let a = p5.Vector.sub(p2,p1)
let b = p5.Vector.sub(p3,p2)
let n = p5.Vector.cross(a, b)
let s = (n.dot(p1) > 0 ? 1 : -1)
return n.mult(s)
}
drawFace(f) {
beginShape()
normal(f[3].x, f[3].y, f[3].z)
vertex(f[0].x, f[0].y, f[0].z)
vertex(f[1].x, f[1].y, f[1].z)
vertex(f[2].x, f[2].y, f[2].z)
endShape(CLOSE)
}
draw() {
push()
rotateX(frameCount * this.spin_x)
rotateY(frameCount * this.spin_y)
this.drawFace(this.face1)
this.drawFace(this.face2)
this.drawFace(this.face3)
this.drawFace(this.face4)
pop()
}
}