# Move camera in front of picked face

**URL:** <https://forum.babylonjs.com/t/move-camera-in-front-of-picked-face/28334>\
**Category:** Questions\
**Created:** [March 11, 2022, 9:14am UTC](https://forum.babylonjs.com/t/move-camera-in-front-of-picked-face/28334 "2022-03-11T09:14:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Myrmod](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Myrmod](https://forum.babylonjs.com/u/Myrmod)\
**Post date:** [March 11, 2022, 9:14am UTC](https://forum.babylonjs.com/t/move-camera-in-front-of-picked-face/28334/1 "2022-03-11T09:14:28Z")

</div>

Hello guys,

I am building a small framework on top of BabylonJS. Now I want to add an example or add the possibility for facing a selected face of any object.

My first step is doing so using the `ArcRotationCamera`.

An example of the behavior I am looking for can be seen here: [https://showroom.littleworkshop.fr/](https://showroom.littleworkshop.fr/)  
As soon as you click on an object. The camera is facing the selected object.

I want to be able to face the objects’ picked face.

```nohighlight
function facePickedSite(e: BABYLON.ActionEvent) {
    const mesh = e.meshUnderPointer

    if (mesh.isFacetDataEnabled) {
      const vector = mesh.getFacetLocalNormals()[e.additionalData.faceId]
    }
  }

```

This code seems to give me the normal vector of the selected face. What are the next steps?

Kind regards

EDIT:  
I now have this function to animate the movement, but I need to calculate the `alpha` and `beta` required to face the clicked face correctly.

```nohighlight
function moveCamera() {
        const alphaAnim = new BABYLON.Animation(
          'alphaAnim',
          'alpha',
          60,
          BABYLON.Animation.ANIMATIONTYPE_FLOAT,
          BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT,
        )
        const betaAnim = new BABYLON.Animation(
          'betaAnim',
          'beta',
          60,
          BABYLON.Animation.ANIMATIONTYPE_FLOAT,
          BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT,
        )

        const alphaKeys = [
          { frame: 0, value: camera.alpha },
          { frame: 100, value: 0 },
        ]
        const betaKeys = [
          { frame: 0, value: camera.beta },
          { frame: 100, value: 0 },
        ]

        alphaAnim.setKeys(alphaKeys)
        betaAnim.setKeys(betaKeys)

        camera.animations.push(alphaAnim)
        camera.animations.push(betaAnim)
        camera._scene.beginAnimation(camera, 0, 100, false)
      }

```

---

<div class="post-metadata">

**Author:** ![Regit](https://avatars.discourse-cdn.com/v4/letter/r/6a8cbe/32.png) [@Regit](https://forum.babylonjs.com/u/Regit)\
**Post date:** [March 11, 2022, 11:10am UTC](https://forum.babylonjs.com/t/move-camera-in-front-of-picked-face/28334/2 "2022-03-11T11:10:40Z")

</div>

Hey, you don’t actually have to calculate the alpha and beta of the ArcRotateCamera, you can use vectors as well! There is `camera.position` and `camera.target`, which can also be animated. I’d suggest you set the target to the origin of your normal and the position to `normalOrigin.add(normal.multiplyByFloats(wantedRadius, wantedRadius, wantedRadius));`

---

<div class="post-metadata">

**Author:** ![Myrmod](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Myrmod](https://forum.babylonjs.com/u/Myrmod)\
**Post date:** [March 11, 2022, 11:38am UTC](https://forum.babylonjs.com/t/move-camera-in-front-of-picked-face/28334/3 "2022-03-11T11:38:13Z")

</div>

Thank you for the feedback. The way I understood what you wrote I came up with this snipped

```nohighlight
const vector = mesh.getFacetLocalNormals()[e.additionalData.faceId]
const newPosition = vector.add(vector.multiplyByFloats(5, 5, 5))
// newPosition.y = camera.target.y // with this it works for the sides
camera.setPosition(newPosition)

```

In the case of a simple Box this works for the top and bottom face. For the others I am slightly below where I want to be.

EDIT:  
This works so far the best, however clicking on the top or bottom rotates the camera slightly,

```nohighlight
const radius = 5
const vector = mesh.getFacetLocalNormals()[e.additionalData.faceId]
const newPosition = e.additionalData.pickedPoint.add(
  vector.multiplyByFloats(radius, radius, radius),
)

```

---

<div class="post-metadata">

**Author:** ![Myrmod](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Myrmod](https://forum.babylonjs.com/u/Myrmod)\
**Post date:** [March 11, 2022, 12:10pm UTC](https://forum.babylonjs.com/t/move-camera-in-front-of-picked-face/28334/4 "2022-03-11T12:10:55Z")

</div>

Here the whole function code so far

````nohighlight
function facePickedSite(e: BABYLON.ActionEvent) {
    try {
      const radius = 5
      const vector = e.meshUnderPointer.getFacetLocalNormals()[e.additionalData.faceId]
      const newPosition = e.additionalData.pickedPoint.add(
        vector.multiplyByFloats(radius, radius, radius),
      )

      const fps = 60
      const totalFrames = 60

      const positionAnim = new BABYLON.Animation(
        'positionAnim',
        'position',
        fps,
        BABYLON.Animation.ANIMATIONTYPE_VECTOR3,
        BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT,
      )

      const positionKeys = [
        { frame: 0, value: camera.position },
        { frame: totalFrames, value: newPosition },
      ]

      positionAnim.setKeys(positionKeys)
      camera.animations.push(positionAnim)
      camera._scene.beginAnimation(camera, 0, totalFrames, false)
    } catch (error) {
      console.error(error)
    }
  }
```
Problems:
- clicking the top or bottom face of a cube results in wrong camera rotation (the visible edges of the cube are not aligned to the screen, the cube itself doesn't have rotation)
- the animation zooms in and out, instead of moving directly or via curve to the new position
````

---

<div class="post-metadata">

**Author:** ![Regit](https://avatars.discourse-cdn.com/v4/letter/r/6a8cbe/32.png) [@Regit](https://forum.babylonjs.com/u/Regit)\
**Post date:** [March 11, 2022, 12:45pm UTC](https://forum.babylonjs.com/t/move-camera-in-front-of-picked-face/28334/5 "2022-03-11T12:45:36Z")

</div>

Maybe you have some limits on your camera set up that prevent correct movement?

We could much better help if you provided a playground with this code. Seems simple enough.

---

<div class="post-metadata">

**Author:** ![Myrmod](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Myrmod](https://forum.babylonjs.com/u/Myrmod)\
**Post date:** [March 11, 2022, 12:55pm UTC](https://forum.babylonjs.com/t/move-camera-in-front-of-picked-face/28334/6 "2022-03-11T12:55:47Z")

</div>

Haven’t thought about doing one since I am working with a different syntax. But here it is: [Babylon.js Playground](https://playground.babylonjs.com/#ZSA2P6)

---

<div class="post-metadata">

**Author:** ![Regit](https://avatars.discourse-cdn.com/v4/letter/r/6a8cbe/32.png) [@Regit](https://forum.babylonjs.com/u/Regit)\
**Post date:** [March 11, 2022, 1:41pm UTC](https://forum.babylonjs.com/t/move-camera-in-front-of-picked-face/28334/7 "2022-03-11T13:41:14Z")

</div>

Seems like your animation was kind of wrong. I use the helper function and it isn’t behaving off anymore: [https://playground.babylonjs.com/#ZSA2P6#1](https://playground.babylonjs.com/#ZSA2P6#1)  
There is still some issue when the camera wants to look straight up or down, but that seems like a gimbal lock problem to me… someone else should comment on that. Try uncommenting line 10 though, then you don’t have that problem anymore.

---

<div class="post-metadata">

**Author:** ![Myrmod](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Myrmod](https://forum.babylonjs.com/u/Myrmod)\
**Post date:** [March 11, 2022, 2:24pm UTC](https://forum.babylonjs.com/t/move-camera-in-front-of-picked-face/28334/8 "2022-03-11T14:24:04Z")

</div>

This almost works as intended, and so much simpler than I thought.  
The only issue that remains is the rotation of the camera as you mentioned.

Thanks a lot so far 😃

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [April 8, 2022, 11:39am UTC](https://forum.babylonjs.com/t/move-camera-in-front-of-picked-face/28334/9 "2022-04-08T11:39:34Z")

</div>

Hello @Myrmod just checking in, was your question answered? 🙂

---

<div class="post-metadata">

**Author:** ![Myrmod](https://avatars.discourse-cdn.com/v4/letter/m/f4b2a3/32.png) [@Myrmod](https://forum.babylonjs.com/u/Myrmod)\
**Post date:** [April 8, 2022, 12:22pm UTC](https://forum.babylonjs.com/t/move-camera-in-front-of-picked-face/28334/10 "2022-04-08T12:22:59Z")

</div>

Oh yes, everything is fine thanks 🙂
