Hi,
it really is no problem to draw things in correct order, but it would be a bit more comfortable if it was possible... anyway, so it just helps to improve my skills and discipline
I just started playing with p5 today, this is the script:
Code:
color circleColor;
color circleBGColor;
int test = 0;
float test2 = 0;
int numOfCircles = 50;
int stageWidth = 300;
int stageHeight = 300;
int circleWidth = 40;
int circleBorderWidth = 50;
int newPosRadius = 40;
int minXPos = circleWidth + 10;
int maxXPos = stageWidth - minXPos;
int minYPos = circleWidth + 10;
int maxYPos = stageHeight - minYPos;
int frame = 0;
Circle[] circles = new Circle[numOfCircles];
void setup() {
size(stageWidth, stageHeight);
colorMode(RGB, 100);
circleColor = color(100, 50, 0);
circleBGColor = color(100, 100, 100);
smooth();
background(20);
framerate(30);
for (int i = 0; i < numOfCircles; i++) {
circles[i] = new Circle(int(random(minXPos, maxXPos)), int(random(minYPos, maxYPos)));
}
}
void draw() {
background(20);
for (int i = 0; i < numOfCircles; i++) {
circles[i].moveTowardsTarget();
circles[i].drawMyBorder();
}
for (int i = 0; i < numOfCircles; i++) {
circles[i].drawMe();
}
}
class Circle {
int xPos, yPos;
int targetX, targetY;
Circle(int x, int y) {
xPos = x;
yPos = y;
setNewTarget();
}
void drawMe() {
fill(circleColor);
noStroke();
ellipse(xPos, yPos, circleWidth, circleWidth);
}
void drawMyBorder() {
fill(circleBGColor);
noStroke();
ellipse(xPos, yPos, circleBorderWidth, circleBorderWidth);
}
void setNewTarget() {
float alpha = random(TWO_PI);
if (aroundMouse(xPos, yPos, circleWidth)) {
test2 = abs(atan2(mouseX - xPos, mouseY - yPos));
if (test2 > TWO_PI) println("WHOAAAAAA");
println("yea" + test2);
}
targetX = xPos + int(newPosRadius * cos(alpha));
targetY = yPos + int(newPosRadius * sin(alpha));
if (!inBounds(targetX, targetY))
setNewTarget();
//if (mousePressed && aroundMouse(targetX, targetY, circleWidth))
// setNewTarget();
}
void moveTowardsTarget() {
if (diff(xPos, targetX) <= 7 && diff(yPos, targetY) <= 7)
setNewTarget();
xPos = xPos - int((xPos - targetX) / 8);
yPos = yPos - int((yPos - targetY) / 8);
}
}
int diff(int a, int b) {
return(abs(a - b));
}
boolean inBounds(int x, int y) {
if (x >= minXPos && x <= maxXPos && y >= minYPos && y <= maxXPos) {
return true;
} else {
return false;
}
}
boolean aroundMouse(int x, int y, int radius) {
if (q(diff(x, mouseX)) + q(diff(y, mouseY)) < q(radius)) {
return true;
} else {
return false;
}
}
int q(int a) {
return a*a;
}