# Problem with my collisions need help

**URL:** https://discourse.processing.org/t/problem-with-my-collisions-need-help/6852
**Category:** Coding Questions
**Created:** [December 23, 2018, 3:35pm UTC](https://discourse.processing.org/t/problem-with-my-collisions-need-help/6852 "2018-12-23T15:35:58Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Robin73](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@Robin73](https://discourse.processing.org/u/Robin73)
#### Post date: [December 23, 2018, 3:35pm UTC](https://discourse.processing.org/t/problem-with-my-collisions-need-help/6852/1 "2018-12-23T15:35:59Z")

</div>

My friend and I are working on this labyrinth and it was working but Yoshi were able to cross the wall so i added the function get() and i wanted my yoshi to walk in a direction if the key was pressed and if the color of the pixel in the same direction my yoshi is going is white but now my yoshi isn’t moving.  
Thanks for replying.

ps: I’m french

PImage labyFond;  
int x;  
int y;  
PImage[] Yoshi = new PImage[7];

color b = color (255,255,255); //White

color q;  
color r;  
color s;  
color t;

void setup()  
{  
size(1024,1024);  
frameRate(30);  
labyFond = loadImage(“laby.png”);

Yoshi[0] = loadImage( “Yoshi0.gif” );  
Yoshi[1] = loadImage( “Yoshi1.gif” );  
Yoshi[2] = loadImage( “Yoshi2.gif” );  
Yoshi[3] = loadImage( “Yoshi3.gif” );  
Yoshi[4] = loadImage( “Yoshi4.gif” );  
Yoshi[5] = loadImage( “Yoshi5.gif” );  
Yoshi[6] = loadImage( “Yoshi6.gif” );

x=-5;  
y=117;  
// get the color of the pixel next to Yoshi :  
q =get(x,y-5); //Color above Yoshi  
r= get(x,y+5); //Color under Yoshi  
s= get(x-5,y); //Color on the left of Yoshi  
t= get(x+5,y); //Color on the right of Yoshi  
}

void draw()  
{  
background(labyFond);

image( labyFond, 0, 0, width, height );  
image( Yoshi[frameCount%1], x, y );  
}

void keyPressed(){  
if (key==CODED){  
if ((keyCode==UP) &&( q == b)) {  
y = y-2; // notre Yoshi avance vers le haut  
image( labyFond, 0, 0, width, height );  
image( Yoshi[frameCount%7], x, y );  
}

```
if ((keyCode==DOWN)&&( r == b)) {
  y = y+2; // notre Yoshi avance vers le bas
  image( labyFond, 0, 0, width, height );
  image( Yoshi[frameCount%7], x, y );
}
if ((keyCode==LEFT)&&( s == b)) {
  x = x-2; // notre Yoshi avance vers la gauche
  image( labyFond, 0, 0, width, height );
  image( Yoshi[frameCount%7], x, y );
}
if ((keyCode==RIGHT)&&( t == b)) {
  x = x+2; // notre Yoshi avance vers la droite
    image( labyFond, 0, 0, width, height );
    image( Yoshi[frameCount%7], x, y );
}

```

}

}

---

<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: [December 24, 2018, 12:12am UTC](https://discourse.processing.org/t/problem-with-my-collisions-need-help/6852/2 "2018-12-24T00:12:30Z")

</div>

-1- if you want check on the color of pixel  
i think it should be not done in setup /only at beginning

-a- copy that 4 lines out of setup  
-b- put it in a extra function

-c- and call that when moving.

-2- other thing:  
the image calls in keypress better delete  
as they are overwritten by draw.

```auto
void keyPressed() {
  if (key==CODED) {
    if ((keyCode==UP) &&( q == b)) y = y-2; // notre Yoshi avance vers le haut
    if ((keyCode==DOWN) &&( r == b)) y = y+2; // notre Yoshi avance vers le bas
    if ((keyCode==LEFT) &&( s == b)) x = x-2; // notre Yoshi avance vers la gauche
    if ((keyCode==RIGHT)&&( t == b)) x = x+2; // notre Yoshi avance vers la droite
  }
  color_check(); // for next move
}

void color_check() { // called at setup and after every move
  q =get(x, y-5); //Color above Yoshi
  r= get(x, y+5); //Color under Yoshi
  s= get(x-5, y); //Color on the left of Yoshi
  t= get(x+5, y); //Color on the right of Yoshi  
}

```

---

<div class="post-metadata">

### Author: ![Robin73](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@Robin73](https://discourse.processing.org/u/Robin73)
#### Post date: [December 24, 2018, 12:52am UTC](https://discourse.processing.org/t/problem-with-my-collisions-need-help/6852/3 "2018-12-24T00:52:21Z")

</div>

Thanks i’ll try that  
And about the overwritting i was thinking about it but i wasn’t sure like it was my friend part

---

<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: [December 24, 2018, 12:56am UTC](https://discourse.processing.org/t/problem-with-my-collisions-need-help/6852/4 "2018-12-24T00:56:48Z")

</div>

you move by 2 pix, and check by 5pix? what is the idea?

---

<div class="post-metadata">

### Author: ![Robin73](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@Robin73](https://discourse.processing.org/u/Robin73)
#### Post date: [December 24, 2018, 1:07am UTC](https://discourse.processing.org/t/problem-with-my-collisions-need-help/6852/5 "2018-12-24T01:07:39Z")

</div>

Was juste trying some stuff but i’ll change that

---

<div class="post-metadata">

### Author: ![Robin73](https://avatars.discourse-cdn.com/v4/letter/r/3ec8ea/32.png) [@Robin73](https://discourse.processing.org/u/Robin73)
#### Post date: [December 24, 2018, 2:46pm UTC](https://discourse.processing.org/t/problem-with-my-collisions-need-help/6852/6 "2018-12-24T14:46:14Z")

</div>

still not working i don’t understand

PImage labyFond;

int x;

int y;

PImage[] Yoshi = new PImage[7];

color b = color (255,255,255); //White

color q;

color r;

color s;

color t;

void setup()

{

size(1024,1024);

frameRate(30);

labyFond = loadImage(“laby.png”);

Yoshi[0] = loadImage( “Yoshi0.gif” );

Yoshi[1] = loadImage( “Yoshi1.gif” );

Yoshi[2] = loadImage( “Yoshi2.gif” );

Yoshi[3] = loadImage( “Yoshi3.gif” );

Yoshi[4] = loadImage( “Yoshi4.gif” );

Yoshi[5] = loadImage( “Yoshi5.gif” );

Yoshi[6] = loadImage( “Yoshi6.gif” );

x=-5;

y=117;

}

void check\_color(){

// get the color of the pixel next to Yoshi :

q =labyFond.get(x,y-2); //Color above Yoshi

r= labyFond.get(x,y+2); //Color under Yoshi

s= labyFond.get(x-2,y); //Color on the left of Yoshi

t= labyFond.get(x+2,y); //Color on the right of Yoshi

}

void draw()

{

background(labyFond);

image( labyFond, 0, 0, width, height );

image( Yoshi[frameCount%1], x, y );

}

void keyPressed(){

check\_color();

if (key==CODED){

if ((keyCode==UP) &&( q == b)) {

y = y-2; // notre Yoshi avance vers le haut

}

if ((keyCode==DOWN)&&( r == b)) {

y = y+2; // notre Yoshi avance vers le bas

}

if ((keyCode==LEFT)&&( s == b)) {

x = x-2; // notre Yoshi avance vers la gauche

}

if ((keyCode==RIGHT)&&( t == b)) {

x = x+2; // notre Yoshi avance vers la droite

}

}

}

---

<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: [December 24, 2018, 3:05pm UTC](https://discourse.processing.org/t/problem-with-my-collisions-need-help/6852/7 "2018-12-24T15:05:06Z")

</div>

pls.  
-a- do not show your email here!!  
-b- pls post your code using the code formatter

> \</\>

you changed get(x,y) to labyFond.get(x,y)  
why? that i can not test here

pls, as i can not follow better describe what is not working?  
its not moving at all OR it walks through other colors as WHITE?

also take a look at that example i just made:

> [@Making a labyrinth](https://discourse.processing.org/t/neep-help-for-a-labyrinth/6869/2):
>
> there is some ?rules? about the naming of variables, and you just hit it! better: class Balle {.... Balle b1; if you want go with the color check ( instead object collision ) need the color check inside your class ( instead the window size limit check) and should do just before you walk. there was a small mistake, you can not use width and height in the variable declaration / prior to setup size() so b1 position was wrong. for example: Balle b1; color labcol = color(200, 0, 0); …
