Xav Posted December 13, 2016 Share Posted December 13, 2016 Hello, I think I am not very well understood the function of the rotation properties on a free camera ... Watch this playground: http://www.babylonjs-playground.com/#HAAJ3#0 Why does my variable rot changed? I think I must be so tired to understand the problem If someone has an explanation for this problem thank you. Ps: It works very well with an arcRotatecamera by adjusting Alpha and Beta values. Thanks Quote Link to comment Share on other sites More sharing options...
X3MC2 Posted December 13, 2016 Share Posted December 13, 2016 @Deltakosh There seems to be something wrong when assigning something to a Vector3 method like .position or .rotation, I've noticed this behavior before and I thought that I was doing something wrong, what I did is create two box meshes "Box1" and "Box2" and set : box1.position = box2.position; This somehow affected the box2.position. And @Xav's example shows exactly whats wrong, but using a variable instead. camera.rotation=rot; Is also acting like : rot = camera.rotation; Quote Link to comment Share on other sites More sharing options...
RaananW Posted December 13, 2016 Share Posted December 13, 2016 Object assign in JavaScript won't create a copy but a reference. When you say camera.rotation = rot; and later change rot, camera rotation will change as well. It is a reference to the same object. The only object types that are being copied and not referenced are the primitives (numbers, string, booleans). So, this is just the way JavaScript works. Want to prevent that? use the clone() function of the vector (box1.position = box2.position.clone()). this will create a standalone clone with the same details of the original vector3. Just to explain what happens in the demo - after clicking you assign camera.rotation = rot. Then you change the camera rotation object, which is now the rot variable. so they both change (they are the same object). Then you click again, but rot is already camera.rotation. And nothing happens. camera.rotation.copyFrom(rot) will be the right way to go here. X3MC2 1 Quote Link to comment Share on other sites More sharing options...
Xav Posted December 13, 2016 Author Share Posted December 13, 2016 Yes indeed it is much more understandable. On the other hand the cloning does not risk to overload the memory if i click of number times the elements? I corrected the problem: Http://www.babylonjs-playground.com/#HAAJ3#1 Thank you again for your explanations Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.