Jump to content

ArcRotateCamera setPosition behavior different in 2.3


satguru
 Share

Recommended Posts

Any reason why this check for equal position was added in 2.3

https://github.com/BabylonJS/Babylon.js/blob/master/src/Cameras/babylon.arcRotateCamera.ts#L492

This changes the behavior subtly

In 2.2 if I wanted to change the target but keep the same position I would save the current position, change the target and do setPosition() using the saved current position.

This would have resulted in the alpha, beta ,radius being re-calculated based on the new target while keeping the same position.

In 2.3 because of the above check all this is bypassed.

 

 

Link to comment
Share on other sites

Hum I did not think about this case.

I updated the code to be more convenient:

        public rebuildAnglesAndRadius() {
            var radiusv3 = this.position.subtract(this._getTargetPosition());
            this.radius = radiusv3.length();

            // Alpha
            this.alpha = Math.acos(radiusv3.x / Math.sqrt(Math.pow(radiusv3.x, 2) + Math.pow(radiusv3.z, 2)));

            if (radiusv3.z < 0) {
                this.alpha = 2 * Math.PI - this.alpha;
            }

            // Beta
            this.beta = Math.acos(radiusv3.y / this.radius);

            this._checkLimits();
        }

        public setPosition(position: Vector3): void {
            if (this.position.equals(position)) {
                return;
            }
            this.rebuildAnglesAndRadius();
        }

        public setTarget(target: Vector3): void {
            if (this.target.equals(target)) {
                return;
            }
            this.rebuildAnglesAndRadius();
        }

Now you can just set the target and this should be enough :)

Link to comment
Share on other sites

maybe setPosition()

        public setPosition(position: Vector3): void {
            if (this.position.equals(position)) {
                return;
            }
            this.position = position;
            this.rebuildAnglesAndRadius();
        }

Also if you change setTarget() won't that break other people existing code?

Link to comment
Share on other sites

see attached html.

It uses babylonjs 2.4

it has a simple scene with ground, box and sphere.

The camera targets sphere's position

It has 2 buttons  "save" and "load" - "save" serializes, "load" loads the serialized data.

To test,

  • open the file in browser,
  • click save then
  • click load.

Notice the camera position has changed

Now comment out the following line 

https://github.com/BabylonJS/Babylon.js/blob/master/src/Cameras/babylon.arcRotateCamera.ts#L526

and try again.

behaves as expected the second time.

index.html

Link to comment
Share on other sites

@Deltakosh

Much better now :)

Now that , that problem is fixed , you can see the the original problem.

I modified the PG demo slightly.

It was hard to see the issue in the original demo, because the camera radius was very large.

So I changed the radius from 50 to 5.

http://www.babylonjs-playground.com/#1LQGCI#1

You can clearly see the problem now, when you do "save" and then "load"

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...