# Natural "first person like" turning/moving in P3D

**URL:** <https://discourse.processing.org/t/natural-first-person-like-turning-moving-in-p3d/21854>\
**Category:** Coding Questions\
**Created:** [June 14, 2020, 4:06am UTC](https://discourse.processing.org/t/natural-first-person-like-turning-moving-in-p3d/21854 "2020-06-14T04:06:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jetjaguar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jetjaguar/32/1370_2.png) [@jetjaguar](https://discourse.processing.org/u/jetjaguar)\
**Post date:** [June 14, 2020, 4:06am UTC](https://discourse.processing.org/t/natural-first-person-like-turning-moving-in-p3d/21854/1 "2020-06-14T04:06:48Z")

</div>

Hey everyone,

I think I’m making something more difficult than it needs to be. To begin with I am simply trying to be able to turn the camera in the P3D environment so that when I then move forward, the camera moves in the direction it is facing - just like in a typical first person game.

I know there are libraries that handle this well, but for educational purposes, I’d like to achieve this effect as simply as possible. Here is a very simple version of the code. I’m not really sure how to describe what is happening. Turning (changing the angle of the camera) and moving forward seem to work, but if you watch the box as you turn, it does not behave as you would expect in real life.

What am I missing or not taking into consideration?

Thank you very much.

```auto
float rotX = 0;
float forward = height/2;

void setup() {
  size(640, 360, P3D);
}

void draw() {
  background(100);
  beginCamera();
  camera();
  translate(0,0,forward);
  rotateY(rotX);
  endCamera();
  translate(width/2, height/2, -100);
  stroke(0);
  box(200);

  //37=left,right=39, up=38, down=40

  if (keyCode == 37) {
    rotX = rotX - .1;
  }
  if (keyCode == 39) {
    rotX = rotX + .1;
  }
  if (keyCode == 38) {
    forward = forward + 36;
  }
  if (keyCode == 40) {
    forward = forward - 36;
  }    

}//draw

void keyReleased() { 
  keyCode = 0;
}

```

---

<div class="post-metadata">

**Author:** ![jetjaguar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jetjaguar/32/1370_2.png) [@jetjaguar](https://discourse.processing.org/u/jetjaguar)\
**Post date:** [June 15, 2020, 9:08pm UTC](https://discourse.processing.org/t/natural-first-person-like-turning-moving-in-p3d/21854/2 "2020-06-15T21:08:27Z")

</div>

Allright, so I see that the camera rotates around the center of the scene.

I’d like to rotate the camera around it’s own center, no matter where I have moved it to. Then, when I go forward, it actually moves forward in the direction it is facing.

Still trying to figure out how to do that.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 17, 2020, 10:39am UTC](https://discourse.processing.org/t/natural-first-person-like-turning-moving-in-p3d/21854/3 "2020-06-17T10:39:17Z")

</div>

[https://discourse.processing.org/t/extend-mousex-position-beyond-screen/](https://discourse.processing.org/t/extend-mousex-position-beyond-screen/)
