Jump to content

[Solved] Variable versus Constructor with addRotation


JohnK
 Share

Recommended Posts

Having had a look at the playgrounds using Jerome's new addRotation I started wondering about line 39 in http://www.babylonjs-playground.com/#1PON40#4

torus.rotation.copyFromFloats(0.0, 0.0, Math.PI / 2);

the torus had already been set as vertical so why was it necessary to re-do it. On commenting out this line and running the PG again the wheel wobbles (quite a lot very soon). So I decided it had to do with stopping error accumulation. My next thought was why copyFromFloats and not just use.

torus.rotation = new BABYLON.Vector3(0, 0, Math.PI/2);

This does work as in http://www.babylonjs-playground.com/#1PON40#9

From advice about something else that Jerome gave me I decided that it had something to do with avoiding constructing new variables and garbage collection. My next thought was would this be avoided if I set a variable to be the vector3 (0, 0, Math.PI/2) and used this each time.

So I created

var vRot = new BABYLON.Vector3(0, 0, Math.PI/2);

and then in the render loop used

torus.rotation = vRot;

result more extreme wobbling as in http://www.babylonjs-playground.com/#1PON40#10. If you do a console.log(vRot) in the loop its value changes.

The simple question is why? What fundamental misunderstanding have I made this time? (Probably something to do with my use of Javascript rather than BJS)

Link to comment
Share on other sites

Well, the goal of addRotation() is to compute the final value of mesh.rotation step by step.

I don't know this final value (and I don't care), I just want my mesh rotated this way, then this way, etc.

So, I need to begin each frame from a starting state rotation.z = PI /2, then to add my steps to reach the final wanted orientation.

I can't keep the last stored values, because the next use of addRotation() will accumulate a new rotation on this last state what will lead to something unpredictable (by me).

Just keep in mind that the WorldMatrix needs only a final value in the property rotation (or rotationQuaternion) whatever the way this value is set : manually or by using addRotation() or rotate()

I've added a summary in the rotation doc here : https://github.com/BabylonJS/Documentation/blob/master/content/overviews/Standard/How_Rotations_and_Translations_Work.md

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...