Herbert Posted September 27, 2017 Share Posted September 27, 2017 I have a box and a ground with physicsImpostor, how can I keep the rotation of the ground up to date with the rotation of the box. here is the playgroundhttps://www.babylonjs-playground.com/#T310H5 If I use "ground.rotation = box.rotation", then the ground rotate but the box do not. If I use "ground.rotation.x = box.rotation.x", then the ground will keep rotating. Quote Link to comment Share on other sites More sharing options...
brianzinn Posted September 27, 2017 Share Posted September 27, 2017 You can't rotate a mesh using rotation when it has a physicsImpostor. You can mesh.physicsImpostor.sleep(), then rotate, and then mesh.physicsImpostor.wakeUp(), but inbetween those sleep/awake it does not maintain physics properties. To maintain physics you could apply forces/impulses. Read the post from Mackey titled "Physics Movement" - lots of good info in there. Quote Link to comment Share on other sites More sharing options...
Herbert Posted September 27, 2017 Author Share Posted September 27, 2017 sleep and wakeUp does not work as I tested scene.registerBeforeRender(() => { ground.physicsImpostor.sleep(); ground.rotation.x = box.rotation.x; ground.physicsImpostor.wakeUp(); }); What I want is to keep the ground having the same rotation as the box, I think it will be complicated to implement it by applying forces/impulses. Thanks for your recommended post, I will read it tomorrow~ Quote Link to comment Share on other sites More sharing options...
Raggar Posted September 27, 2017 Share Posted September 27, 2017 If you would be fine using quaternions instead, this should work: https://www.babylonjs-playground.com/#T310H5#3 Herbert 1 Quote Link to comment Share on other sites More sharing options...
RaananW Posted September 27, 2017 Share Posted September 27, 2017 You could also use the mesh.rotate(...) function, if you are not a big quaternion fan. Herbert 1 Quote Link to comment Share on other sites More sharing options...
Herbert Posted September 28, 2017 Author Share Posted September 28, 2017 Thanks,using quaternions works, but mesh.rotate(...) function seems having the same issue as changing rotation directly. Quote Link to comment Share on other sites More sharing options...
RaananW Posted September 28, 2017 Share Posted September 28, 2017 That seems odd. The rotate function is internally using quaternions to rotate the mesh. Can you reproduce this on the playground? Quote Link to comment Share on other sites More sharing options...
Herbert Posted September 28, 2017 Author Share Posted September 28, 2017 oh, I found what I was missing when I tried to reproduce it, so I have to set the rotationQuaternion for my box first. https://www.babylonjs-playground.com/#T310H5#4 box.rotationQuaternion = new BABYLON.Quaternion(); and it turns out after this both calling rotate function or setting rotation.x directly work, while setting the whole rotation still not work. scene.registerBeforeRender(() => { box.rotation.x -= -0.001; console.log(box.rotation.x); // not work // ground.rotation = box.rotation; // works ground.rotation.x = box.rotation.x; // works // ground.rotate(new BABYLON.Vector3(1, 0, 0), box.rotation.x); }); I think there might be a setter function when I set rotation.x directly, which will actually check rotationQuaternion? I log the value of box.rotation.x right after I change it, but it shows 0.001 constantly, does that mean once rotationQuaternion exists the value of rotation doesn't mean much anymore? Quote Link to comment Share on other sites More sharing options...
RaananW Posted September 28, 2017 Share Posted September 28, 2017 Once a rotationQuaternion exists, the rotation variable is being ignored and should not be used. The rotate function will set a rotationQuaternion for you, if it doesn't already exist (of couse, taking your current rotation in account). Setting a physicsImpostor to your mesh will do the same. afterwards, the rotation should not be used. Hint regarding .rotate, the first variable is an Axis. If you need either X, Y or Z, you can use the static BABYLON.Axis.X (or Y or Z ) instead of creating a new vector. Will save you a few microseconds per frame. Quote Link to comment Share on other sites More sharing options...
Herbert Posted September 28, 2017 Author Share Posted September 28, 2017 As you said, I shouldn't use rotation once a rotationQuaternion exists, then I am confused how my playground still work(I actually think it shouldn't be working since box.rotation.x is constantly 0.001). and if rotation is not referable, what can I be used to pass to the second variable of rotate function? Quote Link to comment Share on other sites More sharing options...
RaananW Posted September 28, 2017 Share Posted September 28, 2017 About the 2nd variable to the rotate function, it is an angle, expressed in radians. Pretty straight forward. Regarding the rotation update when quaternion already exists - a few months ago I added a code to the AbstractMesh's computeWorldMatrix that updates the mesh's rotationQuaternion, if rotation was used. This is to keep people from changing habits, but is relatively discouraged, mainly due to the conversion between euler units and quaternion. that's the code, if you are interested - https://github.com/BabylonJS/Babylon.js/blob/master/src/Mesh/babylon.abstractMesh.ts#L1258 Quote Link to comment Share on other sites More sharing options...
Herbert Posted September 28, 2017 Author Share Posted September 28, 2017 Thanks for being patient and your very detailed answers, it helps a lot I know the 2nd variable's meaning~ just curious is there any other attribute of the box that I can just take and pass to the second variable to keep their rotation the same(since taking rotation.x is relatively discouraged ). If no such attribute exists, I will do it another way or just set the rotationQuaternion instead. Quote Link to comment Share on other sites More sharing options...
RaananW Posted September 28, 2017 Share Posted September 28, 2017 The rotate function adds rotation, not setting it. So, doing that: ground.rotate(BABYLON.Axis.X, -0.001); Is the same as ground.rotation.x -= 0.001; Quote Link to comment Share on other sites More sharing options...
Herbert Posted September 28, 2017 Author Share Posted September 28, 2017 It make sense to me now "ground.rotaion.x -= 0.001" works fine "ground.rotaion.x = -0.001" has the similar effect although it make no sense to me, but as you said it shouldn't be used this way after all https://www.babylonjs-playground.com/#T310H5#5 Quote Link to comment Share on other sites More sharing options...
RaananW Posted September 28, 2017 Share Posted September 28, 2017 The reason it works (in case anyone is interested) is those few lines of code in AbstractMesh i mentioned earlier. Each time the computeWorldMatrix is called, the rotation variable is being inspected. if it is not only 0,0,0, it is being added to the rotationQuaternion and being reset. So, you either add -0.001 and then reset to 0, or set it to be -0.001 and reset it back to 0. As simple as that Why it is not accumulated, you ask? it is practically impossible to go back to rotation from rotationQuaternion. So in order to prevent users from thinking rotation === rotationQuaternion, I reset the variable. Herbert 1 Quote Link to comment Share on other sites More sharing options...
Herbert Posted September 29, 2017 Author Share Posted September 29, 2017 Totally get it, cheers 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.