# Collision detection

**URL:** <https://discourse.processing.org/t/collision-detection/17059>\
**Category:** Libraries\
**Created:** [January 13, 2020, 3:06am UTC](https://discourse.processing.org/t/collision-detection/17059 "2020-01-13T03:06:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Morgy\_B](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@Morgy\_B](https://discourse.processing.org/u/Morgy_B)\
**Post date:** [January 13, 2020, 3:06am UTC](https://discourse.processing.org/t/collision-detection/17059/1 "2020-01-13T03:06:06Z")

</div>

hi

I am trying to get the ball to bounce off my paddle like in the coding challenge ‘pong’ video. I have followed step by step and the ball is bouncing off random parts of the page and not off my paddle.

can somebody help me plz

```auto

import org.gamecontrolplus.gui.*;
import org.gamecontrolplus.*;
import net.java.games.input.*;

ControlIO control;
ControlDevice stick;

Ball ball;
Paddle center;

int score;
String s;
boolean intersect;
float px;
boolean trailOn;
PImage img;
ArrayList<PVector> shadows = new ArrayList<PVector>();
ArrayList<PVector> trail = new ArrayList<PVector>();
 
public void setup() {
    size(1000, 700);
  ball = new Ball();
  center = new Paddle(true);
  img = loadImage("Gun.png");
  
  
  
   // Initialise the ControlIO
  control = ControlIO.getInstance(this);
  // Find a device that matches the configuration file
  stick = control.getMatchedDevice("joystick");
  if (stick == null) {
    println("No suitable device configured");
    System.exit(-1); // End the program NOW!
  }
  // Setup a function to trap events for this button
  stick.getButton("SHADOW").plug(this, "dropShadow", ControlIO.ON_RELEASE);
}

// Poll for user input called from the draw() method.
public void getUserInput() {
  px = map(stick.getSlider("X").getValue(), -1, 1, 0, width);
  //py = map(stick.getSlider("Y").getValue(), -1, 1, 0, height);
  trailOn = stick.getButton("TRAIL").pressed();
}

/// Event handler for the SHADOW button
public void dropShadow() {
  // Make sure we have the latest position
  getUserInput();
  shadows.add(new PVector(px, 40));
}
public void draw() {
  getUserInput(); // Polling
  background(255, 255, 240);
  
 
  
  // Draw shadows
     for (PVector shadow : shadows)
    ellipse(shadow.x, shadow.y, shadow.z, shadow.z);
    if (trailOn) 
   trail.add(new PVector());
  else
    trail.clear();
  if (trail.size() > 1) {
    stroke(132, 0, 0);
    for (int n = 1; n < trail.size(); n++) {
      PVector v0 = trail.get(n-1);
      PVector v1 = trail.get(n);
      line(v0.x, v0.y, v1.x, v1.y);
      v0 = v1;
    
    
    
  }
 }

  
  ball.checkPaddle(center);
  center.update();
  center.show();
  ball.show();
  ball.update();
  ball.edges();
  
}
class Ball {
  float x = width/5;
  float y = height/15;
  float xspeed = 3;
  float yspeed = 3;
  float r = 12;
  Ball(){
   
  }
  void checkPaddle(Paddle px){
    if (y < px.y + px.h/2 && y > px.y - px.h/2 && x - r < px.x + px.w/2){
     // if (x < px.x + px.w/2 && x > px.x - px.x/2 && y - r < px.y + px.h/2){
     
         xspeed *=-1;
      }
      
      
 
      
  
  }
  
  void update(){
    x = x + xspeed;
    y = y + yspeed;
  }
  
  void reset(){
    x = random(width);
    y = height/15;
  }
  
  void edges(){
    if (x < 0 ||x > width){
      xspeed *=-1;
    }
    
    if(y > height){
      reset();
    }
    if (y < 0){
      reset();
  }
  }
  void show(){
    fill(150);
    ellipse (x,y,r*2,r*2);
  }
}class Paddle{
  float x;
  float y = height/2;
 float w = 1000;
 float h = 1;
  
  float ychange = 0;
  
  void update(){
    y+= ychange;
    y = constrain(y,h/2, height-h/2);
  }
  
  void move(float steps){
    ychange = steps;
  }
  
  Paddle(boolean center){
    if(center){
    x = w/2;
    } else {
      x = width - w;
    }
 
   
    
  }
  void show(){
    
noStroke();
 
 imageMode(CENTER);
image(img,px,700, 300,300);
//fill(255);
//rectMode(CENTER);
//rect(px, 700, 100, 200);

}

}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 13, 2020, 5:56am UTC](https://discourse.processing.org/t/collision-detection/17059/2 "2020-01-13T05:56:08Z")

</div>

you are aware that for  
[https://thecodingtrain.com/CodingChallenges/067-pong.html](https://thecodingtrain.com/CodingChallenges/067-pong.html)  
the code is available  
[https://github.com/CodingTrain/website/tree/master/CodingChallenges/CC\_067\_Pong/Processing/CC\_067\_Pong](https://github.com/CodingTrain/website/tree/master/CodingChallenges/CC_067_Pong/Processing/CC_067_Pong) ,

* * *

also i think you need to check on  
the disabled line / your modifications? / inside

```auto
 void checkPaddle(Paddle px)

```

---

<div class="post-metadata">

**Author:** ![Morgy\_B](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@Morgy\_B](https://discourse.processing.org/u/Morgy_B)\
**Post date:** [January 13, 2020, 4:46pm UTC](https://discourse.processing.org/t/collision-detection/17059/3 "2020-01-13T16:46:21Z")

</div>

thankyou i will give this a try

---

<div class="post-metadata">

**Author:** ![Nathan](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nathan](https://discourse.processing.org/u/Nathan)\
**Post date:** [January 13, 2020, 5:07pm UTC](https://discourse.processing.org/t/collision-detection/17059/4 "2020-01-13T17:07:01Z")

</div>

I realize you’re looking for a specific fix to your problem, but I would recommend checking this out:

> **[Collision Detection](http://www.jeffreythompson.org/collision-detection/table_of_contents.php)**
>
> An online book about collision detection using Processing.

This is an excellent resource for both getting your head around how collision detection works. It explains the problem  
in simple terms, describes how to solve it, and provides a solution in the form of example code.

Since you already have partially working code, there is probably either a _bug_ in the basic logic, a missing edge case, or some underlying assumption that is false.

I’m guessing if you have no paddle collision then the ball (or rather the point (x-r, y) is never actually inside the test boundaries) when you check. Also in the code you provided I think the ball will only ever bounce off the left side of the paddle AND when it’s center’s y-coord is between:  
px.y - px.h / 2 and px.y + px.h / 2  
When and if it does bounce you are only modifying **xspeed** , and _not_ yspeed.

You might want circle-rectangle collision instead of point-rectangle.
