# How do I detect collision with the background color in Processing 3 (java)?

**URL:** <https://discourse.processing.org/t/how-do-i-detect-collision-with-the-background-color-in-processing-3-java/16779>\
**Category:** Coding Questions\
**Created:** [January 1, 2020, 3:45pm UTC](https://discourse.processing.org/t/how-do-i-detect-collision-with-the-background-color-in-processing-3-java/16779 "2020-01-01T15:45:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![CharmanderProgrammer](https://avatars.discourse-cdn.com/v4/letter/c/dec6dc/32.png) [@CharmanderProgrammer](https://discourse.processing.org/u/CharmanderProgrammer)\
**Post date:** [January 1, 2020, 3:45pm UTC](https://discourse.processing.org/t/how-do-i-detect-collision-with-the-background-color-in-processing-3-java/16779/1 "2020-01-01T15:45:26Z")

</div>

I am creating a basic dungeon game where you spawn into a randomly generated dungeon and you need to fight monsters.

I just did a random generator where it will move a simulated character around the screen and draw a path of rectangles behind it. This is good and all but now that I am working on implimenting the character, I realise that I cannot detect collision with the background. I am attempting to use color collision. My current color detection is on line 94 and has a comment on 93. Here is my code so far (It is very long. Too lazy to trim it):

> ```
> int rectNum = 0;
> int gx = 400;
> int gy = 400;
> int gr;
> int rx1 = -100;
> int rx2 = -100;
> int rx3 = -100;
> int rx4 = -100;
> int rx5 = -100;
> int rx6 = -100;
> int rx7 = -100;
> int rx8 = -100;
> int rx9 = -100;
> int rx10 = -100;
> int rx11 = -100;
> int rx12 = -100;
> int rx13 = -100;
> int rx14 = -100;
> int rx15 = -100;
> int rx16 = -100;
> int rx17 = -100;
> int rx18 = -100;
> int rx19 = -100;
> int rx20 = -100;
> int ry1;
> int ry2;
> int ry3;
> int ry4;
> int ry5;
> int ry6;
> int ry7;
> int ry8;
> int ry9;
> int ry10;
> int ry11;
> int ry12;
> int ry13;
> int ry14;
> int ry15;
> int ry16;
> int ry17;
> int ry18;
> int ry19;
> int ry20;
> int i = 0;
> int gc = 0;
> int whileCount = 0;
> int x;
> int y;
> int sx = 25;
> int sy = 25;
> int draw = 0;
> int right = 0;
> int left = 0;
> int up = 0;
> int down = 0;
> int speed = 3;
> 
> void setup() {
> size(900, 900);
> }
> 
> void draw() {
> background(0, 0, 0);
> fill(#696969);
> noStroke();
> rect(rx1, ry1, 50, 50);
> rect(rx2, ry2, 50, 50);
> rect(rx3, ry3, 50, 50);
> rect(rx4, ry4, 50, 50);
> rect(rx5, ry5, 50, 50);
> rect(rx6, ry6, 50, 50);
> rect(rx7, ry7, 50, 50);
> rect(rx8, ry8, 50, 50);
> rect(rx9, ry9, 50, 50);
> rect(rx10, ry10, 50, 50);
> rect(rx11, ry11, 50, 50);
> rect(rx12, ry12, 50, 50);
> rect(rx13, ry13, 50, 50);
> rect(rx14, ry14, 50, 50);
> rect(rx15, ry15, 50, 50);
> rect(rx16, ry16, 50, 50);
> rect(rx17, ry17, 50, 50);
> rect(rx18, ry18, 50, 50);
> rect(rx19, ry19, 50, 50);
> rect(rx20, ry20, 50, 50);
> if (draw == 1) {
> fill(#FF00AA);
> ellipse(x, y, 20, 20);
> x += (right - left) * speed;
> y += (down - up) * speed;
> }
> //Current Color Collision
> if (get(x - 5, y) == color(0, 0, 0) || get(x + 5, y) == color(0, 0, 0) || get(x, y + 5) == color(0, 0, 0) || get(x, y - 5) == color(0, 0, 0)) {
> x += (left - right) * speed;
> y += (up - down) * speed;
> println("eeee");
> }
> stroke(0);
> strokeWeight(20);
> noFill();
> rect(0, 0, 900, 900);
> if (i < 21) {
> gc = 0;
> whileCount = 0;
> while (gc == 0) { 
> whileCount ++;
> if (whileCount > 20) {
> reset();
> }
> gr = int(random(1, 5));
> println("While Stats: " + i + " " + gr + " " + whileCount);
> if (gr == 1) {
> if (gy + 50 < 900) {
> if (get(gx + 25, gy - 25) == color(0)) {
> gc = 1;
> gy -= 50;
> i++;
> }
> }
> }
> if (gr == 2) {
> if (gy - 50 > 0) {
> if (get(gx + 25, gy + 75) == color(0)) {
> gc = 1;
> gy += 50;
> i++;
> }
> }
> }
> if (gr == 3) {
> if (gx + 50 < 900) {
> if (get(gx + 75, gy + 25) == color(0)) {
> gc = 1;
> gx += 50;
> i++;
> }
> }
> }
> if (gr == 4) {
> if (gx - 50 > 0) {
> if (get(gx - 25, gy + 25) == color(0)) {
> gc = 1;
> gx -= 50;
> i++;
> }
> }
> }
> if (i == 1) {
> rx1 = gx;
> ry1 = gy;
> }
> if (i == 2) {
> rx2 = gx;
> ry2 = gy;
> }
> if (i == 3) {
> rx3 = gx;
> ry3 = gy;
> }
> if (i == 4) {
> rx4 = gx;
> ry4 = gy;
> }
> if (i == 5) {
> rx5 = gx;
> ry5 = gy;
> }
> if (i == 6) {
> rx6 = gx;
> ry6 = gy;
> }
> if (i == 7) {
> rx7 = gx;
> ry7 = gy;
> }
> if (i == 8) {
> rx8 = gx;
> ry8 = gy;
> }
> if (i == 9) {
> rx9 = gx;
> ry9 = gy;
> }
> if (i == 10) {
> rx10 = gx;
> ry10 = gy;
> }
> if (i == 11) {
> rx11 = gx;
> ry11 = gy;
> }
> if (i == 12) {
> rx12 = gx;
> ry12 = gy;
> }
> if (i == 13) {
> rx13 = gx;
> ry13 = gy;
> }
> if (i == 14) {
> rx14 = gx;
> ry14 = gy;
> }
> if (i == 15) {
> rx15 = gx;
> ry15 = gy;
> }
> if (i == 16) {
> rx16 = gx;
> ry16 = gy;
> }
> if (i == 17) {
> rx17 = gx;
> ry17 = gy;
> }
> if (i == 18) {
> rx18 = gx;
> ry18 = gy;
> }
> if (i == 19) {
> rx19 = gx;
> ry19 = gy;
> }
> if (i == 20) {
> rx20 = gx;
> ry20 = gy;
> }
> }
> }
> if (i > 20 && draw == 0) {
> if (get(sx, sy) == color(105, 105, 105)) {
> x = sx;
> y = sy;
> draw = 1;
> }
> else {
> if (sx > 875) {
> sx = 25;
> sy += 50;
> }
> else {
> sx += 50;
> }
> }
> }
> }
> 
> void reset() {
> rectNum = 0;
> gx = 400;
> gy = 400;
> gr = 0;
> rx1 = -100;
> rx2 = -100;
> rx3 = -100;
> rx4 = -100;
> rx5 = -100;
> rx6 = -100;
> rx7 = -100;
> rx8 = -100;
> rx9 = -100;
> rx10 = -100;
> rx11 = -100;
> rx12 = -100;
> rx13 = -100;
> rx14 = -100;
> rx15 = -100;
> rx16 = -100;
> rx17 = -100;
> rx18 = -100;
> rx19 = -100;
> rx20 = -100;
> ry1 = 0;
> ry2 = 0;
> ry3 = 0;
> ry4 = 0;
> ry5 = 0;
> ry6 = 0;
> ry7 = 0;
> ry8 = 0;
> ry9 = 0;
> ry10 = 0;
> ry11 = 0;
> ry12 = 0;
> ry13 = 0;
> ry14 = 0;
> ry15 = 0;
> ry16 = 0;
> ry17 = 0;
> ry18 = 0;
> ry19 = 0;
> ry20 = 0;
> i = 0;
> gc = 0;
> whileCount = 0;
> x = -100;
> y = 0;
> sx = 25;
> sy = 25;
> draw = 0;
> }
> 
> void keyPressed() {
> if (key == 'w') {up = 1;}
> if (key == 's') {down = 1;}
> if (key == 'a') {left = 1;}
> if (key == 'd') {right = 1;}
> }
> 
> void keyReleased() {
> if (key == 'w') {up = 0;}
> if (key == 's') {down = 0;}
> if (key == 'a') {left = 0;}
> if (key == 'd') {right = 0;}
> }
> 
> ```

If anyone knows how to detect collision with a background color that would be absolutely amazing! Anything helps!

---

<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 2, 2020, 1:04am UTC](https://discourse.processing.org/t/how-do-i-detect-collision-with-the-background-color-in-processing-3-java/16779/2 "2020-01-02T01:04:50Z")

</div>

-a- your ball has 20 diameter == 10 radius  
but the color collision looks ahead 5? step size?

-b- your logic:

> IF ( left black OR right black OR up black OR down black ) move

has a flaw, it would mean you can move in any direction!  
as long one direction shows black.

but you might need a extra IF for each walking direction  
but only need to do ONE check, depending on the direction.

-c-  
not sure the `== color(0,0,0)` will work,  
better would be like " darker as "…

-d- collision detection is one of the most interesting coding ( math ) things,  
color check just the first start.

* * *

but also questionable?  
come on, we are the CODERs,  
so we KNOW where we put the “obstacles” ( as long they are not moving …)

a better design would be a kind of LIST where we say where a obstacle has to be ( drawn ) and directly from that know if we can go there…

please when have time [play](http://kll.engineering-news.org/kllfusion01/articles.php?article_id=154#here33)

* * *
