So this is processing project I have here https://www.openprocessing.org/sketch/672559 does work OK but the ball and the paddles don't really go very smooth when it goes faster. (you have to press the button to show the code on this page)
After a couple of bounces the ball becomes a bit jittery and I would like to know how to avoid that or make the whole movement smoother.
This is the code for the ball without the colors.
//ball variables
PVector ballVelocity = new PVector(0, 0); //velocity of ball
PVector ballPosition = new PVector(initial.x/2, initial.y/2); //position of ball
int maxPositions = 10; //the x positions
float radius = 10; //radius of ball
colour currentCol = new colour(255, 255, 255);
void placeBall() {
ballBeforePosition = new PVector(ballPosition.x, ballPosition.y);
ballPosition = new PVector(initial.x/2, initial.y/2);
ballPosition.x *= scalar.x;
ballPosition.y *= scalar.y;
ballVelocity.y = (float)(Math.random()*(10*scalar.y))-(4*scalar.y);
while (abs(ballVelocity.y)<1) ballVelocity.y = (float)(Math.random()*(10*scalar.y))-5;
if (!leftScored && rightScored) {
ballVelocity.x = 5 * scalar.x;
} else if (leftScored && !rightScored) {
ballVelocity.x = -5 * scalar.x;
} else {
if ((int)(Math.random()*2)==1) {
ballVelocity.x = 5 * scalar.x;
} else {
ballVelocity.x = -5 * scalar.x;
}
}
}
void accelerateBall(float acceleration) {
PVector newVelo = new PVector(ballVelocity.x * acceleration, ballVelocity.y * acceleration); //velocity of ball
ballVelocity = new PVector(newVelo.x, newVelo.y);
if (abs(ballVelocity.x)>(player1.dim().x+maxEndSpeed)) {
ballVelocity.x = sign(ballVelocity.x)*(player1.dim().x+maxEndSpeed);
return;
}
}
void decelerateBall(float deceleration){
PVector newVelo = new PVector(ballVelocity.x / deceleration, ballVelocity.y / deceleration); //velocity of ball
ballVelocity = new PVector(newVelo.x, newVelo.y);
if (abs(ballVelocity.x)< 5 * scalar.x) {
ballVelocity.x = sign(ballVelocity.x)*(5 * scalar.x);
return;
}
}
void runBallCollision(PVector pot, paddle p) {
if (collides(pot, p)) {
hitcount++;
firstcollision++;
if (firstcollision ==3) {
accelerateBall(1.5);
}
if ((p==player1&&pot.x > p.pos().x)||(p==player2&&pot.x < p.pos().x)) {
PVector tempVel = new PVector(ballPosition.x - p.pos().x, (ballPosition.y - p.pos().y)/5);
tempVel.normalize();
tempVel.mult(ballVelocity.mag());
ballVelocity = tempVel;
} else if (!sameSign(p.moveAmount(), ballVelocity.y)&&p.moveAmount()!=0) {
//p.speedBurst();
ballVelocity.x *= -1;
ballVelocity.y = abs(ballVelocity.y)*sign(p.moveAmount())+(p.moveAmount());
if (p==player1) {
ballPosition.x = p.pos().x + (p.dim().x) + radius;
}
if (p==player2) {
ballPosition.x = p.pos().x - (p.dim().x) - radius;
}
}
if (firstcollision<3 || firstcollision > 3 ) {
accelerateBall(1.05);
}
projected = false;
}
}
void moveBall() {
PVector potPos = PVector.add(ballPosition, ballVelocity);
runBallCollision(potPos, player1);
runBallCollision(potPos, player2);
if (potPos.y - radius <= 0+cornerwidth) {
ballVelocity.y *= -1;
} else if (potPos.y + radius >= height-cornerwidth) {
ballVelocity.y *= -1;
}
ballPosition.add(ballVelocity);
//GOAL
if (ballPosition.x<0+cornerwidth) {
rightScored = true;
leftScored = false;
reset(player2, true);
} else if (ballPosition.x>width-cornerwidth) {
rightScored = false;
leftScored = true;
reset(player1, true);
}
}
void displayBall() {
pushMatrix();
PVector newPosi = ballBeforePosition;
translate(ballPosition.x, ballPosition.y);
fill(currentCol.R(), currentCol.G(), currentCol.B());
noStroke();
rect(0, 0, radius*2, radius*2);
popMatrix();
}