Sceneform-android-sdk: Rotate the node to face to the Camera

Created on 17 Oct 2018  路  4Comments  路  Source: google-ar/sceneform-android-sdk

Hi,
I try to make the node always face to the camera and make the user feel that the ViewRenderable is always looking at him.

(UI_ArSceneView as MyArFragment).arSceneView.scene.addOnUpdateListener { frameTime -> (UI_ArSceneView as MyArFragment).arSceneView.arFrame?.let { frame -> if (frame.camera.trackingState == TrackingState.TRACKING) { textNodeArray.forEach { node -> node.worldRotation=Quaternion(frame.camera.pose.rotationQuaternion[0], frame.camera.pose.rotationQuaternion[1], frame.camera.pose.rotationQuaternion[2], frame.camera.pose.rotationQuaternion[3]) } } } }

I did. But not what I expected

Most helpful comment

The name card over the planets in the solar system sample do this. You can see the code in Planet.java line 128

You want to subtract the position of the object from the position of the camera to get the direction vector.
Then use the Quaternion.lookRotation() method to get the rotation.

Vector3 cameraPosition = getScene().getCamera().getWorldPosition();
Vector3 cardPosition = infoCard.getWorldPosition();
Vector3 direction = Vector3.subtract(cameraPosition, cardPosition);
Quaternion lookRotation = Quaternion.lookRotation(direction, Vector3.up());
infoCard.setWorldRotation(lookRotation);

All 4 comments

Hi,
i don't now if the camera pose is the correct quaternion source. I think you should get the scene camera rotation. Check here the sample from google (under "Animate in onUpdate") -> https://developers.google.com/ar/develop/java/sceneform/build-scene

The name card over the planets in the solar system sample do this. You can see the code in Planet.java line 128

You want to subtract the position of the object from the position of the camera to get the direction vector.
Then use the Quaternion.lookRotation() method to get the rotation.

Vector3 cameraPosition = getScene().getCamera().getWorldPosition();
Vector3 cardPosition = infoCard.getWorldPosition();
Vector3 direction = Vector3.subtract(cameraPosition, cardPosition);
Quaternion lookRotation = Quaternion.lookRotation(direction, Vector3.up());
infoCard.setWorldRotation(lookRotation);

@claywilkinson @bobekos Very thankful.

The name card over the planets in the solar system sample do this. You can see the code in Planet.java line 128

You want to subtract the position of the object from the position of the camera to get the direction vector.
Then use the Quaternion.lookRotation() method to get the rotation.

Vector3 cameraPosition = getScene().getCamera().getWorldPosition();
Vector3 cardPosition = infoCard.getWorldPosition();
Vector3 direction = Vector3.subtract(cameraPosition, cardPosition);
Quaternion lookRotation = Quaternion.lookRotation(direction, Vector3.up());
infoCard.setWorldRotation(lookRotation);

How to rotate the model to achieve the same effect when the phone is rotated +90 degrees, -90 degrees

Was this page helpful?
0 / 5 - 0 ratings

Related issues

khonakr picture khonakr  路  3Comments

JessHolle picture JessHolle  路  3Comments

chiaolinghong3d picture chiaolinghong3d  路  3Comments

Brian-Kwon picture Brian-Kwon  路  3Comments

tigran-babajanyan picture tigran-babajanyan  路  3Comments