I've been attempting to create a pyramid shape using multiple circles, similar to this: balls in pyramid shape
To achieve this, I've developed a 'Balls' class:
class Balls {
constructor(x, y, radius, color, ballCount) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
this.balls = [];
this.ballCount = ballCount;
this.option = { restitution: 1, friction: 0.01, label: "ball" };
}
setupBalls() {
for (var i = 0; i < this.ballCount; i++) {
var ball = Bodies.circle(this.x, this.y , this.radius, this.option);
this.balls.push(ball);
World.add(engine.world, [this.balls[i]]);
}
}
drawBalls() {
fill(this.color);
noStroke();
for(var i = 0; i<this.balls.length; i++){
drawVertices(this.balls[i].vertices);
}
}
}
The 'setupBalls()' method needs to be called within the 'function setup()', while 'drawBalls()' should be invoked inside 'function draw()' of p5.js as shown below:
function setup() {
createCanvas(1200, 600);
//create engine
engine = Engine.create();
//set world gravity to 0
engine.world.gravity.y = 0;
//balls
balls = new Balls(width / 2 + 100, 250, 8, color(200, 0, 0), 15);
balls.setupBalls();
}
function draw() {
background(125);
Engine.update(engine);
//balls
balls.drawBalls();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.19.0/matter.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.min.js">
</script>
I experimented with adjusting the x and y positions using nested loops but couldn't achieve the desired pyramid shape.