meteoritool Posted February 28, 2018 Share Posted February 28, 2018 Hi, I came across a problem where I couldn't have an animation behave as intended : https://playground.babylonjs.com/#7NPQG7 Click on the plane (aka ground) to move the ball to clicked location. On animation end, the color of the ball randomly changes. The movement from point A to point B has a sort of easingFunction to it, and the onAnimationEnd is called much later after ball arrived. But I expected the animation easing to be linear by default ???, and the onAnimationEnd function be called right on time at "arrival" ??? If someone can shed some light on this for me, would be very cool ^^ Quote Link to comment Share on other sites More sharing options...
Guest Posted February 28, 2018 Share Posted February 28, 2018 This is because the keys were not independent: https://playground.babylonjs.com/#7NPQG7#1 Check the clone function in lines #32 and #43 meteoritool 1 Quote Link to comment Share on other sites More sharing options...
meteoritool Posted February 28, 2018 Author Share Posted February 28, 2018 Thanks a lot you are dope ! I don't quite understand, I only ever used .clone() for meshes, I usually use .getAbsolutePosition() to obtain a value needed... I sense something bigger than me there something with the way js arrays work ? or the whole concept of object in general ??? Should I use .getAbsolutePosition().Clone() all the time from now on ??? Anyways thanks a lot ! You saved my day ! I also changed the animation loop mode to 'constant' (instead of 'cycle') to avoid a glitch: https://playground.babylonjs.com/#7NPQG7#2 Quote Link to comment Share on other sites More sharing options...
brianzinn Posted February 28, 2018 Share Posted February 28, 2018 You don't always have to clone the absolute position, but if you are going to *change* (ie: use for animation) or *assign* that Vector3, then cloning is good. Realize that the position is just a Vector3, so if you set two meshes to the same position: meahA.position = meshB.position When you move either mesh, both will move... It's not really the season, but check out this snowman that I just made for you. Try to remove the .clone() calls - it shows how the "instances" of position the same will lead to undesired results: https://playground.babylonjs.com/#EAXFJ7 position https://playground.babylonjs.com/#EAXFJ7#1 getAbsolutePosition() The takeaway is that BJS is highly optimized and will re-use the same instances. So, you need to be explicit in your code. edit: here is the code (https://github.com/BabylonJS/Babylon.js/blob/25023c1dc83925a513ed1186ec95f52c566b6d04/src/Mesh/babylon.transformNode.ts#L285). You are given access to an internal cache variable, which you're not meant to change. BJS *could* clone() it for you in the getter, but it's built for speed, so we clone() when required meteoritool 1 Quote Link to comment Share on other sites More sharing options...
meteoritool Posted February 28, 2018 Author Share Posted February 28, 2018 Thank you very much, it kind of clarifies it but I somehow never encountered this before, somehow it seems essential ! In my mind I was copying values from one object.attribute to another object.attribute, but that's not how it works it seems Thanks a lot it improves my knowledge !!! brianzinn 1 Quote Link to comment Share on other sites More sharing options...
brianzinn Posted February 28, 2018 Share Posted February 28, 2018 17 minutes ago, meteoritool said: In my mind I was copying values from one object.attribute to another object.attribute, but that's not how it works it seems Here's another way to look at it - depends if object.attribute is an object or a data type. ie: these look the same, but have different results. This is copying the object reference: meshA.position = meshB.position vs. copying the object values: meshA.position.x = meshB.position.x meshA.position.y = meshB.position.y meshA.position.z = meshB.position.z You use clone() to create a new independent copy. You could, however, assign without clone() like above - I likely wouldn't inline this code: https://playground.babylonjs.com/#EAXFJ7#2 The clone is just a shortcut and actually creates a new Vector3: https://github.com/BabylonJS/Babylon.js/blob/master/src/Math/babylon.math.ts#L1735 Vector3 also has a more memory efficient copyFrom() to not unnecessarily new up a Vector3 instance: https://playground.babylonjs.com/#EAXFJ7#3 If you look in the Math file you see lots of methods like xxx, xxxInPlace, xxxToRef. That lets use easily code our intentions and avoiding writing error-prone (and maybe harder to read) code in my example above meteoritool and GameMonetize 2 Quote Link to comment Share on other sites More sharing options...
meteoritool Posted March 3, 2018 Author Share Posted March 3, 2018 Thx a LOT ! I guess there are many cases I should have used copyFrom(), then I am lucky I never had bugs before ^^ How wonder how much having unnecessary Vector3 impact performances ? My guess is that's really negligible but maybe I'm wrong ? Anyway I'm glad I can have a a more clean script thanks to you ! I LOVE so much BABYLON.js and the community ! GameMonetize 1 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.