# Camera following a certain point

**URL:** <https://discourse.processing.org/t/camera-following-a-certain-point/3629>\
**Category:** Processing.py\
**Created:** [September 18, 2018, 7:48am UTC](https://discourse.processing.org/t/camera-following-a-certain-point/3629 "2018-09-18T07:48:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![danablend](https://avatars.discourse-cdn.com/v4/letter/d/a8b319/32.png) [@danablend](https://discourse.processing.org/u/danablend)\
**Post date:** [September 18, 2018, 7:48am UTC](https://discourse.processing.org/t/camera-following-a-certain-point/3629/1 "2018-09-18T07:48:45Z")

</div>

I have begun making a game, and I need the camera centered on the player. Currently I have tried transforming the coordinate system without luck, and I am not sure about what method I should use to make the camera move with the player. I am using Python

---

<div class="post-metadata">

**Author:** ![Sarah](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sarah/32/1675_2.png) [@Sarah](https://discourse.processing.org/u/Sarah)\
**Post date:** [September 18, 2018, 10:11am UTC](https://discourse.processing.org/t/camera-following-a-certain-point/3629/2 "2018-09-18T10:11:30Z")

</div>

If you want your player to be the center of your camera, the easiest way to do that would to translate your terrain to be relative to your player’s position. Which could be something like this:

 ![Untitled%20drawing%20(6)](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/dcfd832b4b992c81501b3d6e833a5a97d22df8d6.png)

In the example, as the player moves “up”, the terrain or anything else will move down. If the player would be to move “left”, the terrain would move to the right, etc. So basically, the terrain moves in the opposite direction the players moving in. Here’s a small example:

```auto
float playerX = 0;
float playerY = 0;

float object1X;
float object1Y;
float object2X;
float object2Y;
float object3X;
float object3Y;

void setup() {
  size(400, 400);
  object1X = random(width);
  object1Y = random(height);
  object2X = random(width);
  object2Y = random(height);
  object3X = random(width);
  object3Y = random(height);
}

void draw() {
  noStroke();
  background(255);
  fill(255, 0, 0); // Fill red
  ellipse(width/2, height/2, 15, 15); // The player
  
  pushMatrix();
  
  translate(-playerX,-playerY); // Negative, as it moves in the opposite direction
  
  fill(0); // Fill black
  ellipse(object1X, object1Y, 15, 15); // Object 1
  ellipse(object2X, object2Y, 15, 15); // Object 2
  ellipse(object3X, object3Y, 15, 15); // Object 3
  popMatrix();
  
  // Use array keys to control player's position
  if(keyCode == UP && keyPressed) {
    playerY--; 
  }
  if(keyCode == DOWN && keyPressed) {
    playerY++;
  }
  if(keyCode == LEFT && keyPressed) {
    playerX--; 
  }
  if(keyCode == RIGHT && keyPressed) {
    playerX++;
  }
}

```

---

<div class="post-metadata">

**Author:** ![WakeMeAtThree](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wakemeatthree/32/38_2.png) [@WakeMeAtThree](https://discourse.processing.org/u/WakeMeAtThree)\
**Post date:** [September 18, 2018, 10:34am UTC](https://discourse.processing.org/t/camera-following-a-certain-point/3629/3 "2018-09-18T10:34:04Z")

</div>

Hello @danablend!  
You can use the `camera()` function to set the position of your camera (need to be in P3D mode). Check out the [reference for it here](https://processing.org/reference/camera_.html). By default the camera is set to:

```python
camera(width/2.0, height/2.0, (height/2.0) / tan(PI*30.0 / 180.0), 
       width/2.0, height/2.0, 0, 
       0, 1, 0)

```

You can replace it with your player position like so:

```python
camera(player.x, player.y, height/2 / tan(PI*30.0 / 180.0), 
       player.x, player.y, 0, 
       0, 1, 0)

```
