Jump to content

[SOLVED]animation default ease/tween ? onAnimationEnd late ?


meteoritool
 Share

Recommended Posts

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

Link to comment
Share on other sites

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 :lol: 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 

:lol:

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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 :lol:

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 :)

Link to comment
Share on other sites

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 ! 

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