4

I'm getting accelerometer data 10x/second from an external device and I want to have that be reflected on a 3-D SceneKit model. That is, I want the model to rotate in response to the accelerometer data. The 3-D model is a .dae file built in SketchUp that I'm loading into the SceneKit scene.

I'm currently trying to make a rotation on the SceneKit scene's root node (here self refers to a custom SCNView class):

self.scene.rootNode.transform = CATransform3DMakeRotation(angle, x, y, z);

There's a couple problems with this:

  • It's not rotating a specific amount along each axis, it's rotating a fixed amount along a certain vector. Is it possible to manipulate how many degrees a certain axis rotates by changing the relative vector values? Would it make sense to implement a transform with three CATransform3DRotates, one for each axis?
  • It creates a new rotation every time instead of only rotating the difference in angles from the last reading. For example, if the accel data was x: 60˚, y: 40˚, y: -30˚ last reading and is now x: 65˚, y: 45˚, y: -35˚, the model will rotate the full 65˚ along the x-axis, not just the 5˚ difference from last time (as it should). This seems easily fixable by subtracting the last reading from the current reading, but I'm not sure how the math works out with the rotation.
Benjamin Martin
  • 1,795
  • 3
  • 15
  • 17

1 Answers1

2

If you're getting accelerometer data as Euler angles (roll, pitch, yaw) already, perhaps you should set those on the node with its eulerAngles property.

Also, transforming the rootNode if a scene often isn't a great idea — apply that rotation to whatever node contains the model instead.


EDIT: Your question suggested you already had your device orientation data in terms of Euler angles. If you don't have those, you might consider using CMDeviceMotion to get them.

Even better, you could get the device attitude as a quaternion or rotation matrix and pass that to the node's orientation or transform property. I'm not sure the reference frame for that matches what you'll need in your app, but you can always transform it.

rickster
  • 124,678
  • 26
  • 272
  • 326
  • Thanks for the suggestion! The accelerometer data is simply x, y, and z float values between -1 and 1. Although I see the eulerAngles property in use [here](https://developer.apple.com/library/prerelease/mac/samplecode/SceneKitWWDC2014/Listings/Scene_Kit_Session_WWDC_2014_Sources_Slides_AAPLSlideShadows_m.html), it doesn't seem to be recognized by the Xcode IDE (I have SceneKit imported). The model is imported onto the scene itself, not a specific node, which is why I was using the root node: `SCNScene *scene = [SCNScene sceneWithURL:url options:nil error:&error];`, `url` is path to .dae. – Benjamin Martin Jun 26 '14 at 18:32