# Problema de colisiones

**URL:** <https://discourse.processing.org/t/problema-de-colisiones/39835>\
**Category:** Español\
**Created:** [November 23, 2022, 9:59am UTC](https://discourse.processing.org/t/problema-de-colisiones/39835 "2022-11-23T09:59:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SYN1810](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@SYN1810](https://discourse.processing.org/u/SYN1810)\
**Post date:** [November 23, 2022, 9:59am UTC](https://discourse.processing.org/t/problema-de-colisiones/39835/1 "2022-11-23T09:59:21Z")

</div>

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

```auto
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;
  }

}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 23, 2022, 10:06am UTC](https://discourse.processing.org/t/problema-de-colisiones/39835/2 "2022-11-23T10:06:34Z")

</div>

> [@SYN1810](#):
>
> font = loadFont(“Arial-Black-48.vlw”); // Leer la fuente  
> textFont(font);

Please move this to setup ()

Takes too much processor time

---

<div class="post-metadata">

**Author:** ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)\
**Post date:** [November 24, 2022, 5:50pm UTC](https://discourse.processing.org/t/problema-de-colisiones/39835/3 "2022-11-24T17:50:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SYN1810](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@SYN1810](https://discourse.processing.org/u/SYN1810)\
**Post date:** [November 25, 2022, 8:41am UTC](https://discourse.processing.org/t/problema-de-colisiones/39835/4 "2022-11-25T08:41:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)\
**Post date:** [November 25, 2022, 5:18pm UTC](https://discourse.processing.org/t/problema-de-colisiones/39835/5 "2022-11-25T17:18:42Z")

</div>

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.

```auto
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:

> **[Collision Detection](https://jeffreythompson.org/collision-detection/circle-rect.php)**
>
> An online book about collision detection using Processing.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 25, 2022, 7:11pm UTC](https://discourse.processing.org/t/problema-de-colisiones/39835/6 "2022-11-25T19:11:41Z")

</div>

> [@SYN1810](#):
>
> posiy\>posy+180){  
> dirx=dirx\*-1;

Maybe we need a \< here ??

---

<div class="post-metadata">

**Author:** ![SYN1810](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@SYN1810](https://discourse.processing.org/u/SYN1810)\
**Post date:** [November 25, 2022, 8:53pm UTC](https://discourse.processing.org/t/problema-de-colisiones/39835/7 "2022-11-25T20:53:31Z")

</div>

thx you so much Divitiacus
