因此,这是我正在处理的项目https://www.openprocessing.org/sketch/672559确实可以正常工作,但是当速度加快时,球和桨实际上并不会非常平滑。 (您必须按下按钮才能在此页面上显示代码)
几次弹跳后,球变得有点抖动,我想知道如何避免这种情况或使整个运动更加顺畅。
这是没有颜色的球的代码。
//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();
}