# Translate camera moving

**URL:** https://discourse.processing.org/t/translate-camera-moving/42089
**Category:** Coding Questions
**Created:** [May 28, 2023, 10:24pm UTC](https://discourse.processing.org/t/translate-camera-moving/42089 "2023-05-28T22:24:30Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ctremblay](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@ctremblay](https://discourse.processing.org/u/ctremblay)
#### Post date: [May 28, 2023, 10:24pm UTC](https://discourse.processing.org/t/translate-camera-moving/42089/1 "2023-05-28T22:24:30Z")

</div>

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

```auto
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

```auto
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);
  }
}

```

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [May 29, 2023, 12:06am UTC](https://discourse.processing.org/t/translate-camera-moving/42089/2 "2023-05-29T00:06:14Z")

</div>

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:

```auto
if(camera.x < -20)camera.x = 0;

```

---

<div class="post-metadata">

### Author: ![ctremblay](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@ctremblay](https://discourse.processing.org/u/ctremblay)
#### Post date: [June 7, 2023, 9:55am UTC](https://discourse.processing.org/t/translate-camera-moving/42089/3 "2023-06-07T09:55:16Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [June 7, 2023, 10:24am UTC](https://discourse.processing.org/t/translate-camera-moving/42089/4 "2023-06-07T10:24:49Z")

</div>

If I understand the problem correctly this will solve it.

```auto
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);
  }
}

```
