Not sure how to check for location

Im not entirely sure how to check for the location, what i did was i used an array and then some basic x,y variables to draw some rectangles to for a grid on the screen. Im just not sure how i can check for which box the cursor is in so i can, turn the box red or something (just to see it working).

better start easy with just one rectangle and give it a color
and show other color when mouse is over

int[] myrect = {10,10,20,20,255};  // X,Y,W,H,RED

//_______________________________
void setup() {
 size(200,200); 
}

//_______________________________
void draw() {
 background(200,200,0);
 if (overRect(myrect[0],myrect[1],myrect[2],myrect[3])) fill(myrect[4],0,0);
 else                                                   fill(0,0,200);
 rect(myrect[0],myrect[1],myrect[2],myrect[3]);
}

//_______________________________ mouse position over rectangle yes/no
boolean overRect(int rx, int ry, int rwidth, int rheight) {
  if (mouseX >= rx && mouseX <= rx+rwidth && 
    mouseY >= ry && mouseY <= ry+rheight) {
    return true;
  } else {
    return false;
  }
}

the basic is a boolean function what compares mouse position
with the rectangle spec X,Y,W,H

that spec is in a int array incl. a color memory for that rect.

so as a next step you can make the big arrary of that single rect array spec
and the loop over all rectangles.

when you have questions pls. post your full code here using formatter

</>

1 Like

Thank You! you seem very knowledgeable however my code works differently, is there any way you could give me your discord so we can continue this conversation, there are a few other bugs that i want to figure out, since this post like 20-ish minutes ago i have added a character but the grid broke and i’d love to hear your insight, im not sure how to constantly draw all of the rectangles, as well as another thing im curious about that i would like to ask about in lenght, thanks!

float x, y, charachterx, charachtery;
void setup() {
  size(600, 600);
  background(0);
  stroke(255);
  strokeWeight(3);
  noFill();
  rectMode(CENTER);
  x = 0;
  y = 1;
  charachterx = 3;
  charachtery = 3;
}

void draw() {
  background(0);
  for (int i = 0; i &lt; 25; i ++) {
    loc[i] = new PVector(x, y);
  }
  x++;
  if (x &gt; 
  5) {
    y++;
    x = 1;
  }
  rect((x * width/5) - width/10, (y * height/5) - height/10, width/5, height/5);
  ellipse((charachterx * width/5) - width/10, (charachtery * height/5) - height/10, width/20, height/20);
}

void keyPressed() {
  if (keyCode == RIGHT) {
    charachterx++;
  }
  if (keyCode == LEFT) {
    charachterx–;
  }
  if (keyCode == UP) {
    charachtery–;
  }
  if (keyCode == DOWN) {
    charachtery++;
  }
}
PVector[] loc = new PVector[25];
float x, y, charachterx, charachtery;
void setup() {
  size(600, 600);
  background(0);
  stroke(255);
  strokeWeight(3);
  noFill();
  rectMode(CENTER);
  x = 0;
  y = 1;
  charachterx = 3;
  charachtery = 3;
}

void draw() {
  background(0);
  for (int i = 0; i < 25; i ++) {
    loc[i] = new PVector(x, y);
  }
  x++;
  if (x > 5) {
    y++;
    x = 1;
  }
  rect((x * width/5) - width/10, (y * height/5) - height/10, width/5, height/5);
  ellipse((charachterx * width/5) - width/10, (charachtery * height/5) - height/10, width/20, height/20);
}

void keyPressed() {
  if (keyCode == RIGHT) {
    charachterx++;
  }
  if (keyCode == LEFT) {
    charachterx--;
  }
  if (keyCode == UP) {
    charachtery--;
  }
  if (keyCode == DOWN) {
    charachtery++;
  }
}
1 Like

that was your first question and thinking
that cursor means mouse
i give you a example.
if you still want to do that you run my code.

for your code i first see a general problem,

should NOT be in draw, something like the init of the complete grid array
better do only once in setup.

and in draw you use the array data to do the
rect(loc[i].x,loc[i].y,w,h);

But another problem thats in my code, when i constantly draw the background so the ball can move without leaving behind itself so it looks like its moving, but i cant do that without the rectangles i draw to get covered over and the grid disappears whether its in draw or setup.

sorry if my copy of your code confused you.

-a- in setup you should init that array
-b- in setup you should fill the array with the X Y position of the rectangles
? or was it not what the array was for ?

-c- in draw

  • background
  • draw_rectangles();
  • draw_circle();

// in above code you create a PVector array but not use it,
and i not see from your “spec” that it is actually needed??
but if, it might be required also to store like color…
and in that case best would be a class,
so here a example of a class and array of it for a grid

int mywinx=350, mywiny=350;
int myrectw=50;
int offset = 1;  //1 or 0

//Myrect onerect;
int grid=5, many = 25;
Myrect[] myrects = new Myrect[many];

public class Myrect {
  int posx, posy;
  int w, h;
  color cr;
  public Myrect(int posx, int posy, int w, int h,color cr) {  
    this.posx = posx;
    this.posy = posy;
    this.w = w;
    this.h = h;
    this.cr = cr;
  }
  public void draw() {
    stroke(0,200,0); //noStroke();
    fill(cr);
    rect(posx, posy, w, h);
  }
}

// circle spec
int cx=1, cy=1, cw = myrectw, cposx = myrectw/2+cx*cw, cposy = myrectw/2+cy*cw, cr = myrectw;

void settings() {
  size(mywinx, mywiny);
}

void setup() {
//  onerect = new Myrect(myrectw*offset,myrectw*offset,myrectw,myrectw,color(0,0,200));
  for ( int i=0; i < many; i++)  myrects[i] = new Myrect( myrectw*(offset+i%grid),myrectw*(offset+floor(i/grid)),myrectw,myrectw,color(0,0,200));
}

void draw() {
  background(200, 200, 0);
//  onerect.draw();
  for ( int i=0; i < many; i++) myrects[i].draw();
  fill(0,200,200);
  noStroke();
  ellipse(cposx,cposy,cr,cr);
}


void keyPressed() {
if (keyCode == RIGHT) cx++;
if (keyCode == LEFT)  cx--;
if (keyCode == UP)    cy--; 
if (keyCode == DOWN)  cy++;
cx = constrain(cx,1,5);
cy = constrain(cy,1,5);
cposx = myrectw/2+cx*cw;
cposy = myrectw/2+cy*cw;
}

1 Like