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

Switching between global/local rotation (for Viro3DObject) #332

Open
Sejadis opened this issue Jun 20, 2018 · 4 comments
Open

Switching between global/local rotation (for Viro3DObject) #332

Sejadis opened this issue Jun 20, 2018 · 4 comments

Comments

@Sejadis
Copy link

Sejadis commented Jun 20, 2018

Is there a way to switch between global and local rotation for objects?
We get rotation values from the server that considers each axis to always point in the same direction world wise
The rotation values given to the Viro3DObjects seem to get used as local rotation so applying our rotation values results in different angles when the rotation contains more than 1 axis
A bit OT: having support for Quaternions would be nice :)

@dthian
Copy link
Member

dthian commented Jun 21, 2018

Hey @Sejadis, thanks for reaching out. We actually do support Quaternions - you can view the documentation on them here.

Saying that, you should still be able to grab the world transform of a given node, and then to extract the Quaternion rotation from that.

Hope that helps! Do let us know if you are having troubles with this!

@Sejadis
Copy link
Author

Sejadis commented Jun 22, 2018

Hey @dthian thanks for the answer.
It seems this is all ViroCore which i understand is android only?
Im new to react so i may mix some things up but i would think that you stay in (Viro)React if you want both iOS and android? Or is it common to mix in ViroCore?
Is any of that available in ViroReact?
I also posted this question in slack as that probably is the more appropriate place

@dthian
Copy link
Member

dthian commented Jun 22, 2018

Apologies, i was thinking about ViroCore instead of ViroReact. No, they are not interchangeable at the javascript layer.

When you mention "Switch between global and local", did you mean convert? You should be able to grab the world transform through async calls, and local transforms through props for most Viro controls. The only caveat here is in dynamic situations where the object is constantly moved by the renderer via animations, physics, etc.

From the above, it sounds like the rotation that you are providing to set with is a global one, since "rotation values from the server considers each axis to always point in the same direction world wise". Since the rotation props set on a Viro3DObject represents a local rotation, you can't really just apply that same rotation values directly from the server without defects, especially if any parent node of the 3D object is rotated (it probably wouldn't work). Unfortunately, there isn't a way currently to set the world transform directly on a ViroNode, only local.

Having said that, a mitigation may be possible: to instead find the desired local rotation to be set on your Viro3DObject that properly represents the world rotation given by your servers. Thus, we'll need to find the localRotation, given the current world rotation of that ViroObject3D parent's node, and your desired world rotation. That is:

desiredLocalRotation = desiredWorldRotation - parentWorldRotation;

So for example:
desiredWorldRot = [0, 45, 45]
parentWorldRot = [0, 10, 0]
localTransformOfViro3DObject = [0, 35, 45]

Where you can get currentWorld Rotation through getTransformAsync(). Also, the parentWorldRot is the rotation (with getTransformAsync) of the parent node of the Viro3DObject.

This should ideally realign your Viro3DObject towards the world rotation provided by your servers, by setting a properly calculated local rotation on your 3D object.

Is this something that you have tried? If you are having problems with the above, please let us know, and provide the full code, and as well as input rotation values from your servers.

Hope that helps!

@dthian
Copy link
Member

dthian commented Jun 22, 2018

Having said that as well, the above mitigation is kind of gnarly. While the mitigation is implemented, in the mean time we can also file a story to implement a feature that makes it easier to convert to and from world transforms to local transforms. For example:

nodeRef.convertToWorldAsync(localTransform).then((worldTrans) => { 
// Do things with converted worldTransform.
});

Where the transforms must include rot,scale,pos, and the conversion is done with respect to that node.

How does this sound?

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