Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweening position doesn't work. #26

Open
marcusx2 opened this issue Nov 23, 2021 · 4 comments
Open

Tweening position doesn't work. #26

marcusx2 opened this issue Nov 23, 2021 · 4 comments

Comments

@marcusx2
Copy link

marcusx2 commented Nov 23, 2021

@yaustar The same code used to tween local position doesn't work for tweening position. It makes the UI element disappear. Use case: tween from top right corner to top left corner. Related forum post: https://forum.playcanvas.com/t/how-can-i-get-the-top-left-position-if-i-anchored-on-the-top-right/23007

@yaustar
Copy link
Collaborator

yaustar commented Nov 23, 2021

How are you calling the code? The reason it doesn't work IIRC is because the object returned by getPosition is a temp variable object so changing the values in the object doesn't do anything. Whereas getLocalPosition returns the internal vec3 that the entity transform uses.

To get around this, try using the custom update callback:

var entity = this.entity;
var pos = new pc.Vec3(10, 10, 10);
var tween = this.app.tween(pos).to(new pc.Vec3(1,1,1), 1, pc.Linear);
tween.on('update', function (dt) {
    entity.setPosition(pos);
});

@yaustar
Copy link
Collaborator

yaustar commented Nov 23, 2021

(PS Please don't @ directly to user unless it is very specific to that user. There are different team members and contributors that own different parts of the platform and codebase. I'm not in all of them)

@marcusx2
Copy link
Author

Ok sorry yaustar. Your solution worked. I think getPosition and getLocalPosition should work in the same way, should't it? This is confusing.

@yaustar
Copy link
Collaborator

yaustar commented Nov 23, 2021

Ok sorry yaustar. Your solution worked. I think getPosition and getLocalPosition should work in the same way, should't it? This is confusing.

It does require some knowledge about what is done under the hood to understand why the two are different.

An entity stores a vec3 for it's local position and that is used to calculate where it's world position is. This is also return by getLocalPosition so directly editing the object that is returned from getLocalPosition will change it's position.

getPosition returns a temporary calculated position vec3. Editing this directly doesn't do anything.

And the root of all this is because JS can't pass or return by value with objects unlike C# and C++. The reason for all these temporary variables is to reduce garbage collection.

Ideally the two should behave the same way and that getLocalPosition should also return a temporary vec3 instead of the one that it is actually using in the engine for the transform. That way, neither getLocalPosition or getPosition would work in the tween and both would have to use the update method instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants