Collision detection issues

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

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);
} '''

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

1 Like

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

please format your code posting by pasting it into the

</> 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 ,

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

/*
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…

1 Like

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?

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

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

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

//  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

tgtgtgtgtgtgtgtgtgtgtgtgtgtgtgtgtgtgtgtgtgtg

player pipe not work.


i see a problem with your variable naming

class Big
variables small

Big small;

you make that exactly opposite / change that please.


the variable

PImage raider;

class raider { }

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


your draw() loop???
contains

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:

  void displayRaider() {
  }

your excuse is possibly that you NOT make that code,

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.


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!

?? 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…

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.

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

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.

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

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

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

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…

1 Like

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

 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();
    }
   } '''
1 Like