xxxxxxxxxx
70
class Star
{
constructor()
{
// setting random vector position
this.position = createVector(random(width), random(height));
// setting random size for the stars
this.size = random(3, 7);
// setting random vector velocity between the given range
this.velocity = createVector(random(-0.3, 0.3), random(-0.3, 0.3));
// setting random vector acceleration between the given range
this.acceleration = createVector(random(-0.01, 0.01), random(-0.01, 0.01));
}
update()
{
// Update the velocity and position based on acceleration
this.position.add(this.velocity);
// Limit acceleration
if (this.velocity < 5)
{
this.velocity.add(this.acceleration);
}
}
display()
{
// initializing variables that make up the star
let starPoints = random(3, 7);
let externalRadius = this.size;
let internalRadius = this.size * 0.4;
// Angles distributed evenly around circle based on number of star points
let angle = TWO_PI / starPoints;
let halfAngle = angle / 2.0;
fill(255);
// Drawing a star: https://p5js.org/examples/form-star.html
beginShape();
for (let a = 0; a < TWO_PI; a += angle)
{
// adjusting the coordinates based on sin and cos waves
// this is for the outer radius of the star
let starX = this.position.x + cos(a) * externalRadius;
let starY = this.position.y + sin(a) * externalRadius;
vertex(starX, starY);
// adjusting the coordinates based on sin and cos waves
// this is for the inner radius of the star
starX = this.position.x + cos(a + halfAngle) * internalRadius;
starY = this.position.y + sin(a + halfAngle) * internalRadius;
vertex(starX, starY);
}
endShape(CLOSE);
}
checkEdges()
{
// Checks the position of the star is at the borders, flips the direction of velocity accordingly
if((this.position.x>width) || (this.position.x<0))
{
this.velocity.x = this.velocity.x * -1;
}
if((this.position.y>width) || (this.position.y<0))
{
this.velocity.y = this.velocity.y * -1;
}
}
}