Translate camera moving

I’m trying to make a settlement building camera scroll game, after I scroll to the side a few it won’t stop going sideways.

here my code:

sketch

int s = 25;
boolean buildBase = false;
ArrayList<Ground> ground = new ArrayList<Ground>();
PVector camera = new PVector(0,0);// this my camera position


void setup(){
  size(600,600);
  for (int i = 0; i < 2400; i++){
    ground.add(new Ground(i));
  }
}

void draw(){
  background(0);
  translate(camera.x,camera.y);
  for (int i = ground.size()-1; i >= 0; i--){
    Ground g = ground.get(i);
    g.show();
  }
  mouseCheck();
}

void mouseCheck(){
  if (mouseX < camera.x + 10 && camera.x > 0){camera.x += 1;}// here check to see if within 10 of the visable window
  if (mouseX > camera.x + (width - 10)){camera.x -= 1;}// here checks to see if mouse is within 10 of translate width
}

ground

class Ground{
  PVector pos;
  
  Ground(int n){
    int y = 0;
    while(n > 99){
      n -= 100;
      y += 1;
    }
    pos = new PVector(n * s, y * s);
  }
  
  void show(){
    fill(139, 69, 19);
    rect(pos.x,pos.y,s,s);
  }
}

If you add the following to draw() just after translate() it will only scroll when the mouse is along the right side of the window:

if(camera.x < -20)camera.x = 0;
2 Likes

If i do it that way then i can’t scroll to the left & returning it to 0 dont that just bring me back to the beginning?

If I understand the problem correctly this will solve it.

boolean buildBase = false;
ArrayList<Ground> ground = new ArrayList<Ground>();
PVector camera = new PVector(0, 0);// this my camera position


void setup() {
  size(600, 600);
  for (int i = 0; i < 2400; i++) {
    ground.add(new Ground(i));
  }
}

void draw() {
  background(0);
  translate(camera.x, camera.y);
  for (int i = ground.size()-1; i >= 0; i--) {
    Ground g = ground.get(i);
    g.show();
  }
  mouseCheck();
}

void mouseCheck() {
  if (mouseX < 10 && camera.x < 0) {
    camera.x += 1;
  }// here check to see if within 10 of the visable window
  if (mouseX >  (width - 10) && camera.x > -1900) {
    camera.x -= 1;
  }// here checks to see if mouse is within 10 of translate width
}

class Ground {
  PVector pos;
  int s = 25;

  Ground(int n) {
    int y = 0;
    while (n > 99) {
      n -= 100;
      y += 1;
    }
    pos = new PVector(n * s, y * s);
  }

  void show() {
    fill(139, 69, 19);
    rect(pos.x, pos.y, s, s);
  }
}
2 Likes