# I need help to make it so that when the square touches a green rectangle it resets to position (20,650)

**URL:** <https://discourse.processing.org/t/i-need-help-to-make-it-so-that-when-the-square-touches-a-green-rectangle-it-resets-to-position-20-650/16940>\
**Category:** Coding Questions\
**Created:** [January 7, 2020, 6:56pm UTC](https://discourse.processing.org/t/i-need-help-to-make-it-so-that-when-the-square-touches-a-green-rectangle-it-resets-to-position-20-650/16940 "2020-01-07T18:56:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rango](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@Rango](https://discourse.processing.org/u/Rango)\
**Post date:** [January 7, 2020, 6:56pm UTC](https://discourse.processing.org/t/i-need-help-to-make-it-so-that-when-the-square-touches-a-green-rectangle-it-resets-to-position-20-650/16940/1 "2020-01-07T18:56:30Z")

</div>

```auto
int x=20;
int y=650;
int w=20;
int h=20;

int c=1;
int d=20;

String a= "";
String b= "Warning: Wormhole detected ahead. Approach with caution.";

float rectX;
float rectY;

float rectA;
float rectB;

float rectC;
float rectD;

float rectE;
float rectF;

float rectG;
float rectH;

float rectWidth;
float rectHeight;

//A Mover object
Mover mover;

void setup() {
  size(800,900);
  mover = new Mover(); 
rectX=100;
rectY=200;

rectWidth=30;
rectHeight=600;

rectA=250;
rectB=350;

rectC=350;
rectD=200;

rectE=420;
rectF=350;

rectG=510;
rectH=200;
}

void draw() {
 
  background(0);
  smooth();
  background(0);
  noStroke();
  fill(255,0,0);
  rect(10,10,785,190);
  filter(BLUR, 6);
  stroke(0);
  fill(255,255,0);
  rect(10,10,780,180);
  
  // Update the location
  mover.update();
  // Display the Mover
  mover.display(); 

 textSize(20);
fill(0);
text(a, 100,50);

fill(25,200,35);
rect(rectX,rectY,rectWidth,rectHeight);
rect(rectA,rectB,rectWidth,rectHeight);
rect(rectC,rectD,rectWidth,rectHeight);
rect(rectE,rectF,rectWidth,rectHeight);
rect(rectG,rectH,rectWidth,rectHeight);

if (x> rectX && x < rectX + rectWidth && y > rectY && y < rectY + rectHeight) {
 x=20;
  y=650;
  } else if (x > rectA && x < rectA + rectWidth && y > rectB && y < rectB + rectHeight) {
    x=20;
  y=650;
  } else if (x > rectC && x < rectC + rectWidth && y > rectD && y < rectD + rectHeight) {
    x=20;
  y=650;
  } else if (x > rectE && x < rectE + rectWidth && y > rectF && y < rectF + rectHeight) {
    x=20;
  y=650;
  } else if (x > rectG && x < rectG + rectWidth && y > rectH && y < rectH + rectHeight) {
    x=20;
  y=650;
  } 

}

class Mover {

  // The Mover tracks location, velocity, and acceleration 
  PVector location;
  PVector velocity;
  PVector acceleration;
  // The Mover's maximum speed
  float topspeed;

  Mover() {
    // Start at (20,650)
    location = new PVector(x,y);
    velocity = new PVector(0,0);
    topspeed = 10;
  }

  void update() {
    
    // Compute a vector that points from location to mouse
    PVector mouse = new PVector(mouseX,mouseY);
    PVector acceleration = PVector.sub(mouse,location);
    // Set magnitude of acceleration
    acceleration.setMag(5);
    
    // Velocity changes according to acceleration
    velocity.add(acceleration);
    // Limit the velocity by topspeed
    velocity.limit(topspeed);
    // Location changes by velocity
    location.add(velocity);
  }

  void display() {
    stroke(255);
    strokeWeight(2);
    fill(127);
    rect(location.x,location.y,20,20);
  }

}

```

---

<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 8, 2020, 1:32am UTC](https://discourse.processing.org/t/i-need-help-to-make-it-so-that-when-the-square-touches-a-green-rectangle-it-resets-to-position-20-650/16940/2 "2020-01-08T01:32:16Z")

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

* * *

what are these

```auto
if (x> rectX && x < rectX + rectWidth && y > rectY && y < rectY + rectHeight) {

```

lines supposed to do?  
what is x y used for?

---

<div class="post-metadata">

**Author:** ![Rango](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@Rango](https://discourse.processing.org/u/Rango)\
**Post date:** [January 8, 2020, 4:17pm UTC](https://discourse.processing.org/t/i-need-help-to-make-it-so-that-when-the-square-touches-a-green-rectangle-it-resets-to-position-20-650/16940/3 "2020-01-08T16:17:08Z")

</div>

Thank you for taking a look. The above code you have a question about is a proximity detection, but I’m struggling with making proximity for the below object.

```auto
fill(127);
    rect(location.x,location.y,20,20);

```

The x and y are just up there temporarily (I was trying to represent locaton.x with x and location.y with y). It didn’t work, but I want it so that if the rectangle’s position is within the green rectangles it will reset to position (20,650). It’s like a maze where if the object touches a wall it resets.
