11

I'm using the Sceneform SDK in Android Project.

I have sfb and the sfa objects in my project, and I want the initial rotation of my object to be rotated 90 degrees. How can I achieve it?

I found the next code in these files and I changed the scale.

But I didn't find a way for the rotation.

model: {
  attributes: [
     "Position",
     "TexCoord",
     "Orientation",
  ],
  collision: {},
  file: "sampledata/models/redmixer.obj",
  name: "redmixer",
  scale: 0.010015,
},
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
David
  • 37,109
  • 32
  • 120
  • 141
  • you want to show it already rotated or rotate it during runtime? – Fixus Jul 29 '18 at 16:05
  • @Fixus Already rotated. – David Jul 29 '18 at 17:40
  • 1
    You might want to file a feature request at https://github.com/google-ar/sceneform-android-sdk/issues as it would make sense to have something like this in the SFA file (we already have scale ... so why not rotation?) – Steven Mohr Aug 02 '18 at 14:04

4 Answers4

12

I have used setLocalRotation to successfully rotate object for 90 degrees programatically.

      // Create the Anchor.
      Anchor anchor = hitResult.createAnchor();
      AnchorNode anchorNode = new AnchorNode(anchor);
      anchorNode.setParent(arFragment.getArSceneView().getScene());

      // Create the transformable andy and add it to the anchor.
      TransformableNode node = new TransformableNode(arFragment.getTransformationSystem());

      //set rotation in direction (x,y,z) in degrees 90
      node.setLocalRotation(Quaternion.axisAngle(new Vector3(1f, 0, 0), 90f));

      node.setParent(anchorNode);
      node.setRenderable(renderable);
      node.select();

If you would be interested in more info about Quaternion I recommend: https://proandroiddev.com/arcore-cupcakes-4-understanding-quaternion-rotations-f90703f3966e.

But basically last parameter is angle in degrees. In this case 90deg -> 90f. By vector you specify direction of the rotation. In example I have rotated in x direction (x,y,z) -> (1f, 0, 0). Hope it helps.

LadyWoodi
  • 486
  • 1
  • 5
  • 12
1

Not sure if this is what you looking for but try this it looks to me nightmare though it works i have tried it somewhere at the bottom page he will explain to you how to rotate the 3dobject look for this title "Bonus: Make the heart rotate!"

how to do rotatation animation in sceneform

anuloo
  • 15
  • 10
0

I think I can help you (or at least point in a helpful direction). Try:

//Assuming you have created an anchor through hitResult or some other method and have 
//an ArFragment variable "fragment"

ModelRenderable.builder().setSource(context, Uri.parse("your-model.sfb").thenAccept{
   addModel(it, anchor)
   }

fun addModel(model: ModelRenderable, anchor: Anchor){
val aNode = AnchorNode(createAnchor)
val tNode = TransformableNode(fragment.transformationSystem)

//set rotation properties here
tNode.rotationController...
tNode.localRotation...

tNode.setParent(aNode)
fragment.ArSceneView.scene.addChild(aNode)
}

It is quite similar to the above mentioned example. Hope it helps!

popeye
  • 81
  • 2
  • 5
0

To avoid Gimbal Lock use a Quaternion rotation. For rotating a 3D object around Z axis clock wise follow this Kotlin code:

override fun onRight(value: Float) {

    objectNode.apply {

        Log.d("Clock Wise", value.toString())

        // XYZ is rotation direction, W component is angle size in degrees 
        rotation = Quaternion.axisAngle(Vector3(0.0f, 0.0f, 1.0f), -45.0f)
    }
}

Hope this helps.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220