How to use a for loop and the array function to create a tail for a bouncing ball
in
Programming Questions
•
2 years ago
Large subject title huh? It pretty much captures my current predicament.
This is my current code, with what is needed following:
int balls = 100;
float[] xPos = new float[balls];
float[] yPos = new float[balls];
float xSpeed, ySpeed, posMorpher;
float top, right, left, bottom;
float radius;
float yBumperWidth, yBumperHeight, xBumperWidth, xBumperHeight;
float fMorpher, sMorpher;
color rC, lC, bC, tC, nC, ballColor;
float[] xPosBalls = new float[20];
float[] yPosBalls = new float[20];
float lerpNeutral, lerpSpeed;
int rCounter = 00;
int lCounter = 00;
int tCounter = 00;
int bCounter = 00;
void setup()
{
size(800,600);
smooth();
xSpeed = 5.0f;
ySpeed = 5.0f;
lerpNeutral = 0;
lerpSpeed = .1;
for (int i = 0; i < balls; i ++)
{
xPos[i] = width/2;
yPos[i] = height/2;
println (xPos[i]);
}
frameRate(40);
rC = color(242, 68, 68);
lC = color(35,65,240);
bC = color(90,0,180);
tC = color(35,255,18);
nC = color(250,250,250);
ballColor = nC;
}
void draw()
{
background(0);
top = 0;
bottom = height;
left = 0;
right = width;
radius = (width + height)/25;
yBumperWidth = (width + height)/40;
yBumperHeight = (width + height)/15;
xBumperWidth = (width + height)/15;
xBumperHeight = (width + height)/40;
//-------------------------------------------------------------------------theBall
fill(ballColor);
ellipse(xPos[1], yPos[1], (width + height)/25, (width + height)/25);
xPos[1] += xSpeed;
yPos[1] += ySpeed;
//----------------------------------------------------------------------theBumpers
rectMode(CENTER);
fill(lC);
rect(yBumperWidth/2, yPos[1], yBumperWidth, yBumperHeight);
fill(rC);
rect(right-((yBumperWidth)/2), yPos[1], yBumperWidth, yBumperHeight);
fill(tC);
rect(xPos[1], xBumperHeight/2, xBumperWidth, xBumperHeight);
fill(bC);
rect(xPos[1], bottom - (xBumperHeight/2), xBumperWidth, xBumperHeight);
//wallBounce
xPos[1] = constrain(xPos[1], left + radius/2 + yBumperWidth, right - radius/2 - yBumperWidth);
if(xPos[1] >= right - radius/2 - yBumperWidth || xPos[1] <= left + radius/2 + yBumperWidth)
{
xSpeed = -xSpeed;
}
yPos[1] = constrain(yPos[1], top + radius/2 + xBumperHeight, bottom - radius/2 - xBumperHeight);
if(yPos[1] >= bottom - radius/2 - xBumperHeight || yPos[1] <= top + radius/2 + xBumperHeight)
{
ySpeed = -ySpeed;
}
lerpNeutral =+ lerpSpeed;
//colorChanger
//---------------------------------------------------rightChange
if (xPos[1] >= right - radius/2 - yBumperWidth)
{
rCounter += 1;
println("Right Bumper hit = " + rCounter + " times");
}
if ((xPos[1] <= right - radius/2 - yBumperWidth) && (xPos[1] > right - (radius/2) * 6 - yBumperWidth))
{
ballColor = lerpColor(rC, nC, norm(xPos[1], right - radius/2 - yBumperWidth, right - (radius/2) * 6 - yBumperWidth));
}
//----------------------------------------------------leftChange
if (xPos[1] <= left + radius/2 + yBumperWidth)
{
lCounter += 1;
println("Left Bumper hit = " + lCounter + " times");
ballColor = lC;
}
if ((xPos[1] >= left + radius/2 + yBumperWidth) && (xPos[1] <= (left + radius/2) * 6 + yBumperWidth))
{
ballColor = lerpColor(lC, nC, norm(xPos[1], left + radius/2 + yBumperWidth, (left + radius/2) * 6 + yBumperWidth));
}
//---------------------------------------------------bottomChange
if (yPos[1] >= bottom - radius/2 - xBumperHeight)
{
bCounter += 1;
println("Bottom Bumper hit = " + bCounter + " times");
ballColor = bC;
}
if ((yPos[1] <= bottom - radius/2 - xBumperHeight) && (yPos[1] >= bottom - (radius/2) * 6 - xBumperHeight))
{
ballColor = lerpColor(bC, nC, norm(yPos[1], bottom - radius/2 - xBumperHeight, bottom - (radius/2) * 6 - xBumperHeight));
}
//------------------------------------------------------topChange
if (yPos[1] <= top + radius/2 + xBumperHeight)
{
tCounter += 1;
println("Top Bumper hit = " + tCounter + " times");
ballColor = tC;
}
//topFadeEffect
if ((yPos[1] >= top + radius/2 + xBumperHeight) && (yPos[1] <= top + (radius/2) * 6 + xBumperHeight))
{
ballColor = lerpColor(tC, nC, norm(yPos[1], top + radius/2 + xBumperHeight, top + (radius/2) * 6 + xBumperHeight));
}
}
My problem is, I need to somehow get 19 further ellipses behind the initial ellipse (total:20 on screen) which are a frame behind each other. After doing that, I need to make each ball slowly alpha out till the last one is gone.
I know this is a pretty intense question, but any help in the right direction would be greatly appreciated.
1