# Collision detection issues

**URL:** <https://discourse.processing.org/t/collision-detection-issues/17009>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [January 10, 2020, 5:24pm UTC](https://discourse.processing.org/t/collision-detection-issues/17009 "2020-01-10T17:24:43Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![gibbo7997](https://avatars.discourse-cdn.com/v4/letter/g/e56c9b/32.png) [@gibbo7997](https://discourse.processing.org/u/gibbo7997)\
**Post date:** [January 10, 2020, 5:24pm UTC](https://discourse.processing.org/t/collision-detection-issues/17009/1 "2020-01-10T17:24:43Z")

</div>

Hi guys, currently working on a uni project where we have to incorporate a piece of hardware into our code. I have tried to create a code based on the flappy bird game and found someone who had created a ‘flappy code’ game, i have changed it so my slider controls the Cave Raider (what my game is called) but i am having trouble with the collision of my raider and my pillars, i was hoping someone might be able to help, and could potentially change the way i have used the collision in my code because i currently don’t understand it. Any help would be much appreciated!

Harry - code below

```auto
import cc.arduino.*;
import com.tinkerkit.*;

pillar[] p = new pillar[3];
boolean end=false;
boolean intro=true;
int score=0;

Arduino arduino;

//declare the potentiometer
TKPotentiometer pot;

float minVal = 1;
float maxVal = 700;
float val;

PImage raider;
PImage background;

raider r;
String gameScene;

void setup() {  

  size(500, 700);

  arduino = new Arduino(this, Arduino.list()[1], 57600);    

  //for every tinkerkit component we have to pass
  //the arduino and the port
  pot = new TKPotentiometer(arduino, TK.I0);

  noStroke();
  raider = loadImage("caveraiderimg.png");
  background = loadImage("background.png");

  for (int i = 0; i<3; i++) {
    p[i]=new pillar(i);
    
    r =new raider();
    gameScene = "Intro";
  }
}

void draw() {

  background(0);
  
  if(gameScene =="Intro"){
    introScene();
  }else if(gameScene=="Play"){
    playScene();
  }else if(gameScene=="Lose"){
    loseScene();
  }

       if(gameScene=="Play"){
   fill(0);
      stroke(255);
      textSize(32);
      rect(20,20,100,50);
      fill(255);
      text(score,30,58);
       }
      
  //get the potentiometer values
  float val = pot.read();

  //calibration
  if (val < minVal) minVal = val;
  if (val > maxVal) maxVal = val;  

  //draw the raider 
  imageMode(CENTER);
  image(raider, 100, val, 50, 50);
  
  r.displayRaider();
}
// a void which will enable the slider value
void getSliderValue(){
  val = map(val, minVal, maxVal, 600, height);
  
  r.setRaiderx(val);
}

//creating classes - separate to the rest of the code so that they can reference them
class raider { 
  float Raiderx;
  float xPos,yPos,ySpeed;
  raider(){
    xPos = 250;
    yPos = 400;
  }
  void displayRaider(){
}
void setRaiderx(float val){
  Raiderx = val;
}
void move(){
     yPos+=ySpeed; 
     for(int i = 0;i<3;i++){
      p[i].xPos-=3;
     }
    }

  void checkCollisions(){
     if(yPos>800){
      end=false;
     }
    for(int i = 0;i<3;i++){
    if((xPos<p[i].xPos+50&&xPos>p[i].xPos-50)&&((float)val<p[i].opening-100||(float)val>p[i].opening+100)){
     end=false; 
    }
    }
  }
}

class pillar {
  float xPos, opening;
  boolean cashed = false;
  
  pillar(int i) {
    xPos = 100+(i*200);
    opening = random(600)+100;
  }
   void drawPillar(){
       line(xPos,0,xPos,opening-100);  
       line(xPos,opening+100,xPos,800);
     }
     void checkPosition(){
      if(xPos<0){
       xPos+=(200*3);
       opening = random(600)+100;
       cashed=false;
      } 
      if(xPos<250&&cashed==false){
       cashed=true;
       score++; 
      }
     }
}

    void reset(){
     end=true;
     score=0;
     r.yPos=400;
     for(int i = 0;i<3;i++){
      p[i].xPos+=550;
      p[i].cashed = false;
     }
    }
  
  void introScene(){
    background(0);
    image(background, 250, 350);
    if(mousePressed==true)
    if(mouseX > 0 && mouseX < 500 && mouseY>0 && mouseY<700){
  gameScene = "Play";
     }
      
}

void playScene(){
  background(0);
    r.move();
      r.displayRaider();
      r.checkCollisions();
      for(int i = 0;i<3;i++){
      p[i].drawPillar();
      p[i].checkPosition();
      }
  
  
}

void loseScene(){
  background(0);
} '''
```

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [January 10, 2020, 6:15pm UTC](https://discourse.processing.org/t/collision-detection-issues/17009/2 "2020-01-10T18:15:30Z")

</div>

welcome to the community, for future reference please remember to format your code using ctrl shift and c.

---

<div class="post-metadata">

**Author:** ![gibbo7997](https://avatars.discourse-cdn.com/v4/letter/g/e56c9b/32.png) [@gibbo7997](https://discourse.processing.org/u/gibbo7997)\
**Post date:** [January 11, 2020, 1:06pm UTC](https://discourse.processing.org/t/collision-detection-issues/17009/3 "2020-01-11T13:06:33Z")

</div>

hi okay i will do. any help with this mate?

---

<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 12, 2020, 3:14am UTC](https://discourse.processing.org/t/collision-detection-issues/17009/4 "2020-01-12T03:14:00Z")

</div>

please format your code posting by pasting it into the

```auto
</> code button

```

of the editor header menu ( context name: Preformatted text )  
it looks like  
```  
type or paste code here  
```

also can use the ``` manually above and below your code.

thank you.

* * *

now we need you to REPAIR your above CODE POSTING,  
not make a new / better post.  
( just think about the hundred people opening your topic in the future )

* * *

for your project,  
please link to where the code / project / hardware come from

* * *

update FOUND IT  
[TinkerKit\_Processing/examples/linear\_potentiometer/linear\_potentiometer.pde at master · TinkerKit/TinkerKit\_Processing · GitHub](https://github.com/TinkerKit/TinkerKit_Processing/blob/master/examples/linear_potentiometer/linear_potentiometer.pde) ,

and the first thing you did is: you deleted the first 10 lines

```auto
/*
Linear Potentiometer Example
by Matteo Loglio (http://matlo.me)
display a rectangle whose horizontal position
depends from the potentiometer values. 
remember to upload standardFirmata on the
Arduino board.
*/

```

-a- so that guy not deserves any respect?  
-b- anyone try using your code does not need to know how to start  
about the arduino first? ( because you already got it running? )

* * *

if you think the problem is about the arduino communication / slider…  
( like the topic header / and the forum category suggests )  
then you cut that part out of your ?game? code  
so we can concentrate on that.

if you think like your intro text suggests that you have a processing collision code problem  
please note that all the

> arduino / slider hardware / firmata code /

in your question  
just will result that probably no one of us can run your code  
( besides we also not have your pictures… )

in that case you better cut out the game part

- and temporary replace the slider by keyboard operation
- skip the picture part ( or provide a full .zip )

so with copy / paste into PDE **all** can play and verify your problem  
so the chance you get help increase hundred times…

---

<div class="post-metadata">

**Author:** ![gibbo7997](https://avatars.discourse-cdn.com/v4/letter/g/e56c9b/32.png) [@gibbo7997](https://discourse.processing.org/u/gibbo7997)\
**Post date:** [January 14, 2020, 3:07pm UTC](https://discourse.processing.org/t/collision-detection-issues/17009/5 "2020-01-14T15:07:12Z")

</div>

Hi, apologies for the poorly written first post but i wasn’t aware of the various rules, I’m not the best with coding, is there any thing you can see that is wrong with my code?

---

<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 14, 2020, 4:08pm UTC](https://discourse.processing.org/t/collision-detection-issues/17009/6 "2020-01-14T16:08:31Z")

</div>

thank you for formatting the code,  
but now we see that you did not format it in the PDE ( processing IDE )  
sorry i am nagging but the point is WE and YOU can not see the program flow  
without a formatting

PDE / Edit / Auto Format / or [ctrl][t]

* * *

you ignored the rest of my comments,

so i get your code, formatted it,  
disabled all arduino things  
and replaced

```auto
  float val = mouseY; //kll pot.read();

```

also i disabled all picture things ( as you not provided them )  
and replaced the player with a rectangle.

```auto
// image(raider, 100, val, 50, 50);
  rect(100, val, 50, 50); // kll temp test

```

so the game can be started with a mouse click ( and see the ?pipes? )

ALL that work just to see that your collision

> a comment by my new cat `CAFFEE` on my keyboard

```auto
tgtgtgtgtgtgtgtgtgtgtgtgtgtgtgtgtgtgtgtgtgtg

```

player pipe not work.

* * *

i see a problem with your variable naming

class Big  
variables small

```auto
Big small;

```

you make that exactly opposite / change that please.

* * *

the variable

```auto
PImage raider;

class raider { }

```

is a double usage ? no idea why the code start anyhow…

* * *

your draw() loop???  
contains

```auto
else if (gameScene=="Play") {
    playScene();
}

// also
  if (gameScene=="Play") {
    fill(0);
    stroke(255);
    textSize(32);
    rect(20, 20, 100, 50);
    fill(255);
    text(score, 30, 58);
  }

// and
  //draw the raider 
  imageMode(CENTER);
  image(raider, 100, val, 50, 50);
  r.displayRaider();

```

and  
playScene() calls `r.displayRaider();` again…  
but that is empty:

```auto
  void displayRaider() {
  }

```

* * *

your excuse is possibly that you NOT make that code,

> [@gibbo7997](#):
>
> found someone who had created a ‘flappy code’ game

well you copy from the wrong source and expect us to repair that?  
why not try to find a working `flappy bird` processing code  
and start from that?  
and add on your hardware project.  
anyhow, you need to have in the code the link to the code source you started from,  
( same as i told you with the linear\_potentiometer.pde code),  
as you not understand classes until now, do not try to sell that as your own code.  
take credit ONLY for your own work.

* * *

---

<div class="post-metadata">

**Author:** ![gibbo7997](https://avatars.discourse-cdn.com/v4/letter/g/e56c9b/32.png) [@gibbo7997](https://discourse.processing.org/u/gibbo7997)\
**Post date:** [January 14, 2020, 4:15pm UTC](https://discourse.processing.org/t/collision-detection-issues/17009/7 "2020-01-14T16:15:22Z")

</div>

I’ve not tried to take credit for anyones work, and as its an assignment with a report i have referenced everyone who has aided me, or any sources i have took code from. The source for that flappy code is the one you entered in the previous comment, to be honest my knowledge of coding is minimal and im just looking for a quick fix before my deadline on thursday, I will make the adjustments you have suggested. And if you are able to help further and actually get my collision code working i would pay you and of course reference you!

---

<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 14, 2020, 4:24pm UTC](https://discourse.processing.org/t/collision-detection-issues/17009/8 "2020-01-14T16:24:43Z")

</div>

> [@gibbo7997](#):
>
> The source for that flappy code is the one you entered in the previous comment

?? that i not understand, i found and linked only to  
linear\_potentiometer.pde  
source.  
the source for your not working flappy bird i can not know ,  
possibly here from the forum, but you might have copy from a QUESTION instead from a answer,  
i made that mistake already…

---

<div class="post-metadata">

**Author:** ![gibbo7997](https://avatars.discourse-cdn.com/v4/letter/g/e56c9b/32.png) [@gibbo7997](https://discourse.processing.org/u/gibbo7997)\
**Post date:** [January 14, 2020, 4:26pm UTC](https://discourse.processing.org/t/collision-detection-issues/17009/9 "2020-01-14T16:26:12Z")

</div>

Okay, no its a fully functioning code, here’s the link, i just added the classes and gameScenes as well as the hardware example we were supplied in an attempt to get my sprites working in the same way his game works.

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/3580/flappy-code)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

---

<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 14, 2020, 4:31pm UTC](https://discourse.processing.org/t/collision-detection-issues/17009/10 "2020-01-14T16:31:08Z")

</div>

right, that code works,  
your code not detect collision,  
compare and find what you changed

---

<div class="post-metadata">

**Author:** ![gibbo7997](https://avatars.discourse-cdn.com/v4/letter/g/e56c9b/32.png) [@gibbo7997](https://discourse.processing.org/u/gibbo7997)\
**Post date:** [January 14, 2020, 4:32pm UTC](https://discourse.processing.org/t/collision-detection-issues/17009/11 "2020-01-14T16:32:50Z")

</div>

believe me that’s the first thing i did, i would not have asked on here if I’d not have done that.

The introduction of hardware made it difficult to use the same code that he did for the collision, i have copied all the relevant fields but i must be missing something. Hence why i asked on here.

---

<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 14, 2020, 4:45pm UTC](https://discourse.processing.org/t/collision-detection-issues/17009/12 "2020-01-14T16:45:08Z")

</div>

i think you forget to copy OR deleted the code where the  
`end` is used, after it is set by the collision code.

---

<div class="post-metadata">

**Author:** ![gibbo7997](https://avatars.discourse-cdn.com/v4/letter/g/e56c9b/32.png) [@gibbo7997](https://discourse.processing.org/u/gibbo7997)\
**Post date:** [January 14, 2020, 4:48pm UTC](https://discourse.processing.org/t/collision-detection-issues/17009/13 "2020-01-14T16:48:03Z")

</div>

hmm yes i see what you mean, as he uses end in the void keyPressed/mousPressed functions, would i just enter this into my classes then instead? thanks

---

<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 14, 2020, 4:51pm UTC](https://discourse.processing.org/t/collision-detection-issues/17009/14 "2020-01-14T16:51:43Z")

</div>

hm, no  
i think HIS code from main `draw()`  
( after the collision check )  
is missing in your project

```auto
if(end){
      rect(20,20,100,50);
      fill(255);
      text(score,30,58);
      }else{
      rect(150,100,200,50);
      rect(150,200,200,50);
      fill(255);
      if(intro){
        text("Flappy Code",155,140);
        text("Click to Play",155,240);
      }else{
      text("game over",170,140);
      text("score",180,240);
      text(score,280,240);
      }
      }

```

but i would not copy that structure…

---

<div class="post-metadata">

**Author:** ![gibbo7997](https://avatars.discourse-cdn.com/v4/letter/g/e56c9b/32.png) [@gibbo7997](https://discourse.processing.org/u/gibbo7997)\
**Post date:** [January 14, 2020, 4:56pm UTC](https://discourse.processing.org/t/collision-detection-issues/17009/15 "2020-01-14T16:56:32Z")

</div>

I have tried putting the end code into my raider class and it has actually worked, however i need to adjust where the collision happens, making progress! thank you my friend

```auto
 float Raiderx;
 float xPos,yPos,ySpeed;
 raider(){
   xPos = 250;
   yPos = 400;
 }
 void displayRaider(){
}
void setRaiderx(float val){
 Raiderx = val;
}
void move(){
    yPos+=ySpeed; 
    for(int i = 0;i<3;i++){
     p[i].xPos-=3;
    }
     intro=false;
    if(end==false){
      reset();
    }
   } '''
```
