# Bug within setTarget-function for z-up

**URL:** <https://forum.babylonjs.com/t/bug-within-settarget-function-for-z-up/1094>\
**Category:** Bugs\
**Created:** [January 31, 2019, 4:48pm UTC](https://forum.babylonjs.com/t/bug-within-settarget-function-for-z-up/1094 "2019-01-31T16:48:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sudoku](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@Sudoku](https://forum.babylonjs.com/u/Sudoku)\
**Post date:** [January 31, 2019, 4:48pm UTC](https://forum.babylonjs.com/t/bug-within-settarget-function-for-z-up/1094/1 "2019-01-31T16:48:48Z")

</div>

Hi everyone,

this topic is already posted in the html5gamedev forum, it is not approved yet but i will add the link as soon as it is…

i am having a problem with the setTarget method when cameraUp is set to (0, 0, 1).

Can you checkout this pg?: [Playground](https://playground.babylonjs.com/#BHMD2A#3)

depending on how often this block is called

```auto
    camera.setTarget(new BABYLON.Vector3(0, 7, 10));
    camera.getViewMatrix();
    console.log(camera._computationVector);

```

the camera position changes and the objects in the scene are visible or not…  
but it should not make a difference how often i call setTarget i guess

please checkout the results of the console log statements as well.

Those are:

```auto
e {x: -9.184850993605146e-16, y: 4.999999999999999, z: 3}
VM91:18 e {x: -1.1102849226888157e-15, y: -2.9000000000000004, z: 5}
VM91:21 e {x: -8.201425513350847e-16, y: -5.1000000000000005, z: -2.8999999999999995}
VM91:24 e {x: -5.510910596163087e-16, y: 2.9999999999999982, z: -5.1}
VM91:27 e {x: -9.184850993605148e-16, y: 4.999999999999999, z: 2.9999999999999973}

```

y and z values are getting switched and the sign switched too.

i expect the issue to be caused somewhere in here?:

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/1X/e7292e4243eba42d46a93c9ee8a6d689d37318ba.png)

Every comment on that is appreciated!

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [January 31, 2019, 9:23pm UTC](https://forum.babylonjs.com/t/bug-within-settarget-function-for-z-up/1094/2 "2019-01-31T21:23:21Z")

</div>

> [@Sudoku](#):
>
> html5gamedev forum

That’s the old forum, so don’t worry about it. You should use this forum. 🙂

I can’t repro the behavior you see. On my machine using Chrome, I see the following in the output window:

```auto
Babylon.js v4.0.0-alpha.22 - WebGL2
VM72:15 eisNonUniform: (...)x: -9.184850993605148e-16y: 4.999999999999999z: 2.9999999999999973 __proto__ : Object
VM72:18 eisNonUniform: (...)x: -9.184850993605148e-16y: 4.999999999999999z: 2.9999999999999973 __proto__ : Object
VM72:21 eisNonUniform: (...)x: -9.184850993605148e-16y: 4.999999999999999z: 2.9999999999999973 __proto__ : Object
VM72:24 eisNonUniform: (...)x: -9.184850993605148e-16y: 4.999999999999999z: 2.9999999999999973 __proto__ : Object
VM72:27 eisNonUniform: (...)x: -9.184850993605148e-16y: 4.999999999999999z: 2.9999999999999973 __proto__ : Object

```

Is there something special about your set up?

---

<div class="post-metadata">

**Author:** ![Sudoku](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@Sudoku](https://forum.babylonjs.com/u/Sudoku)\
**Post date:** [February 1, 2019, 9:03am UTC](https://forum.babylonjs.com/t/bug-within-settarget-function-for-z-up/1094/3 "2019-02-01T09:03:07Z")

</div>

Thanks for your reply @bghgary ! oh ya, i just realized: only if the console is open before i press ‘run’ the output values match the ones i added to the post. if i hit run first (or just open the page) i get the same result as you do. but in both cases - if i delete the last setTarget() block -

```
camera.setTarget(new BABYLON.Vector3(0, 7, 10));
camera.getViewMatrix();
console.log(camera._computationVector);

```

the position of the camera changes and the objects are not visible anymore… 😕

---

<div class="post-metadata">

**Author:** ![aWeirdo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/aweirdo/32/52_2.png) [@aWeirdo](https://forum.babylonjs.com/u/aWeirdo)\
**Post date:** [February 1, 2019, 3:40pm UTC](https://forum.babylonjs.com/t/bug-within-settarget-function-for-z-up/1094/4 "2019-02-01T15:40:22Z")

</div>

You can setTarget as much as you like.

Bug seems related to

> camera.getViewMatrix();

or something deeper than that.  
As a _get_ function it shouldn’t change anything, but it clearly does.

> **[Babylon.js Playground](https://playground.babylonjs.com/#BHMD2A%234)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

uncomment any camera.getViewMatrix and it messes the camera up.

* * *

Update…  
Upon some further investigation, it seems multiple functions are affected, including setTarget as you wrote.  
Looks like the cache isn’t updated and old values being used perhaps?

> **[Babylon.js Playground](https://playground.babylonjs.com/#BHMD2A%236)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [February 1, 2019, 6:00pm UTC](https://forum.babylonjs.com/t/bug-within-settarget-function-for-z-up/1094/5 "2019-02-01T18:00:36Z")

</div>

Yup. Looks like a bug.

Here is a min repro:  
[https://playground.babylonjs.com/#1RGET5](https://playground.babylonjs.com/#1RGET5)

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [February 1, 2019, 6:31pm UTC](https://forum.babylonjs.com/t/bug-within-settarget-function-for-z-up/1094/6 "2019-02-01T18:31:11Z")

</div>

It is actually not a bug per se.

The getViewMatrix() call is also updating internal and external computed properties like position (as the position of an ArcRotateCamera is based on alpha, beta and radius).

For user convenience the camera.position is computed out of alpha, beta and radius as soon as the viewMatrix is built (it is easy and fast to extract that info from the matrix itself)

In the repro shared by @bghgary, the getViewMatrix call will update the position property which in turn is used by the setTarget call (as the setTarget call will compute alpha, beta and radius from position and target).

So if you call setTarget BEFORE getViewMatrix all is fine as camera postion was not yet moved.

Hopefully it makes sense

---

<div class="post-metadata">

**Author:** ![aWeirdo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/aweirdo/32/52_2.png) [@aWeirdo](https://forum.babylonjs.com/u/aWeirdo)\
**Post date:** [February 1, 2019, 6:49pm UTC](https://forum.babylonjs.com/t/bug-within-settarget-function-for-z-up/1094/7 "2019-02-01T18:49:51Z")

</div>

Hmm yeah that makes sense,  
Any explaination for this behavior? ( line #20 )  
[https://playground.babylonjs.com/#BHMD2A#7](https://playground.babylonjs.com/#BHMD2A#7)

Maybe it’s the same, I would just assume a setTarget would simply change the target property and update alpha/beta/radius to keep the same position or atleast the same angle & distance

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [February 1, 2019, 7:02pm UTC](https://forum.babylonjs.com/t/bug-within-settarget-function-for-z-up/1094/8 "2019-02-01T19:02:13Z")

</div>

yes this is the same issue.  
Calling setTarget uses camera.position which is ONLY updated during camera.getViewMatrix call:

[https://playground.babylonjs.com/#BHMD2A#8](https://playground.babylonjs.com/#BHMD2A#8)

I know this is not ideal so I will update the setTarget function to first call a getViewMatrix to make sure the camera.position is updated first
