Problema de colisiones

Hola, he estado haciendo un programa que simule el juego ping pon, pero la pelota algunas veces choca con la pala blanca y otras veces no, adjunto mi codigo

int posy=250;
int posx=250;
int posix=600;
int posiy=300;
int ballSize=100;
int dirx=5;
int diry=5;
int i=1;
int j=2;
PFont font; 

void setup(){
    fullScreen();
}

void draw()
{
    background(0);
    fill(255);
    noStroke();
    strokeWeight(5);
    stroke(255);
    line(width/2,height/2-1000,width/2,height/2+1000);
    strokeWeight(0);
    
    //score
  font = loadFont("Arial-Black-48.vlw"); // Leer la fuente
  textFont(font);
  fill(255);
  text(i, width/2-200, 50); // Escribvir en las coordenadas (0,40)
  text(j, width/2+150, 50);
    
    //cuerpo
  rect(25,posy,50,200);
  rect(1295,posx,50,200);
  
   //pelota
  fill(255,0,0);
  posix+=dirx*1;
  posiy+=diry*1;
  circle(posix,posiy,ballSize);
  if(posix<100 && posiy>posx && posiy<posx+180){
    dirx=dirx*-1; 
  }
  
  if(posix>width-100 && posiy>posy && posiy>posy+180){
    dirx=dirx*-1; 
  }
    
  if(posiy>=height-ballSize/2){
    diry=diry*-1; 
  }

  if(posiy<=0+ballSize/2){
    diry=diry*-1; 
  }    
  
  if(posix>1400){
    posix=width/2; posiy=height/2; i=i+1;
  }
  
  if(posix<-50){
    posix=width/2; posiy=height/2; j=j+1;
  }
}

void keyPressed()
{
    if(keyCode=='S')
    posy=posy+5;
    
    if(posy<=5) //limita la trayectoria hacia arriba
    posy=5;

    if(keyCode=='W')
    posy=posy-5;
    
    if(posy>=575) //limita la trayectoria hacia abajo
    posy= 575;

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    if(keyCode=='L')
    posx=posx+5;
    
    if(posx<=5) //limita la trayectoria hacia arriba
    posx=5;

    if(keyCode=='O')
    posx=posx-5;
    
    if(posx>=575) //limita la trayectoria hacia abajo
    posx= 575;

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  
  if(keyCode=='1'){
    i=i+1;
  }
    
  if(keyCode=='2'){
    j=j+1;
  }

}

Please move this to setup ()

Takes too much processor time

1 Like

The arguments you use in the if statements are not completely correct.

The position of your paddles x-value is fixed at 25 and 1295.
But be aware, that the coordinate for the rectangle you draw is the upper left corner.
So your x value for of the right side of the left paddle is 25+50.
Your ball’s left end is posix-ballsize/2.
The y-position for the paddle goes from posy to posy+200.
The y-position of the ball is the same as the posiy coordinate. So it has to be larger than posy but smaller than posy+200.

If the paddles x-value and dirx are equal and diry is between posy and posy+200 the ball should change direction.

For the left side your paddles left x-value is at 1295.
Your ball’s left end is posix+ballsize/2.
Y-position of the paddle is the same as on the left side.

If you apply these in the first two if statements, collision from the paddles should work.

1 Like

I’m afraid that either I didn’t understand you or I couldn’t do it well, sorry

1 Like

Okay, there is an additional issue: Sometimes the ball comes from the side on the paddle, and when the velocity is switched, the ball stays in the range of the paddle and then switches velocity again. You can fix that by adding a check, that the sign on the velocity only switches, when the velocity goes in the wrong direction.

Google Translate:
De acuerdo, hay un problema adicional: a veces, la pelota viene del costado de la paleta, y cuando se cambia la velocidad, la pelota permanece en el rango de la paleta y luego cambia de velocidad nuevamente. Puede arreglar eso agregando una verificación, que el signo de la velocidad solo cambia cuando la velocidad va en la dirección incorrecta.

if(posix+ballSize/2 >= 1295 && posiy>=posx && posiy<=posx+200){
if (dirx > 0){
dirx*=-1;
}
}

if(posix-ballSize/2 <= 75 && posiy>=posy && posiy<=posy+200){
if (dirx < 0){
dirx*=-1;
}
}

There will still be some slight overlaps between circle and paddle in cases the most left point of the circle is not the closest to the paddle. If you want that corrected, you would need something like that:

1 Like

Maybe we need a < here ??

thx you so much Divitiacus

1 Like