VimHax
September 4, 2018, 7:05pm
19
jb4x:
To get back to your initial project, the way to go is to draw your track on a PGraphic.
The white part is then draw on top of the PGraphic but never on it.
Here a code illustrating it:
int posX, posY;
int prevX, prevY;
int velX, velY;
int size;
PGraphics back;
void setup() {
fullScreen();
background(20);
noStroke();
fill(255);
back = createGraphics(width, height);
posX = 20;
posY = 20;
prevX = posX - 1;
prevY = posY - 1;
velX = 1;
velY = 1;
size = 30;
}
void draw() {
// Draw on the graphic
back.beginDraw();
back.fill(100);
back.noStroke();
back.ellipse(prevX, prevY, size, size);
back.endDraw();
// Draw the leading shape on top of the PGraphics
background(20);
image(back, 0, 0);
ellipse(posX, posY, size, size);
//Change position
prevX = posX;
prevY = posY;
posX += velX;
posY += velY;
}
void mousePressed() {
exit();
}
I found Processing probably about 4 days ago and its easier to start with than Java and I’m definitely going to learn how to do some advanced stuff. This seems interesting, I’ll try to check this method out aswell!
jb4x:
For the speeding up, you just need to run what’s in your draw()
function several time per frame.
So you just need to add a nbOfCycles
variable and add a for loop in draw()
boolean XPlus = true, YPlus = true;
boolean RTrue = true, GTrue = false, BTrue = false;
int once;
float SquareX, SquareY;
float Speed = 1;
float Size = 20;
float BackGround = 0;
float TraceX, TraceY;
int R = 0, G = 0, B = 0;
int nbOfCycles = 50;
void setup() {
background(BackGround);
noSmooth();
fullScreen();
}
void draw() {
for (int i = 0; i < nbOfCycles; i++) {
// Random Start Location //
if (once == 0) {
SquareX = random(0, width);
SquareY = random(0, height);
once++;
}
// Collision and Boundary Restriction Logic //
if (SquareX>=width-(Size/2)) {
XPlus=false;
SquareX=width-(Size/2);
}
if (SquareX<=0) {
XPlus=true;
SquareX=0;
}
if (SquareY>=height-(Size/2)) {
YPlus=false;
SquareY=height-(Size/2);
}
if (SquareY<=0) {
YPlus=true;
SquareY=0;
}
// Collision Reaction Logic //
if (XPlus==false) {
SquareX=SquareX-Speed;
}
if (XPlus==true) {
SquareX=SquareX+Speed;
}
if (YPlus==false) {
SquareY=SquareY-Speed;
}
if (YPlus==true) {
SquareY=SquareY+Speed;
}
// Trace Logic //
if (XPlus==false) {
TraceX=SquareX+Speed;
}
if (XPlus==true) {
TraceX=SquareX-Speed;
}
if (YPlus==false) {
TraceY=SquareY+Speed;
}
if (YPlus==true) {
TraceY=SquareY-Speed;
}
// Color Logic //
if (RTrue == true) {
R++;
}
if (GTrue == true) {
G++;
}
if (BTrue == true) {
B++;
}
if (R >= 255) {
RTrue = false;
GTrue = true;
}
if (G >= 255) {
GTrue = false;
BTrue = true;
}
if (B >= 255) {
BTrue = false;
RTrue = true;
}
if (RTrue == false && R > 0) {
R--;
}
if (GTrue == false && G > 0) {
G--;
}
if (BTrue == false && B > 0) {
B--;
}
// Visuals //
noStroke();
fill(R, G, B);
rect(TraceX, TraceY, Size, Size);
noStroke();
fill(255);
rect(SquareX, SquareY, Size, Size);
}
}
void mousePressed() {
exit();
}
Oh, I didn’t even think about that lol…
jb4x:
I love the result btw
I feel very inspired to continue learning Processing!
3 Likes