Apple Sudden Motion Sensor
Keywords:
accelerometer,
tracking,
tilt,
interaction
Author(s): Michael Zoellner
Date: 2007-06-14
Summary: This tutorial shows you how to use the Apple Sudden Motion Sensor inside a 3d scene.
Introduction
In 2005 Apple introduced the Sudden Motion Sensor for its portable computers in order to protect the hardrive. This example only works on Apple Powerbooks, iBooks, MacBooks and MacBooks Pro build after 2005.
The sensor is a 3-axis accelerometer. The AppleMotionSensor backend delivers a Vec3f with the three acceleration values.
Shaking
In the first example we create an IOSensor of the type AppleMotionSensor and route its values to a Transform node. The values are getting smoothed by a PositionDamper.
The AppleMotionSensor is loaded like any other Instant IO device via an IOSensor. The acceleration values are stored in the field Motion:
- Motion (SFVec3f): Acceleration values (x, y, z)
Code: IOSensor
<IOSensor DEF='AppleMotionSensor' type='AppleMotionSensor'> <field accessType='outputOnly' name='Motion' type='SFVec3f'/> </IOSensor>
The acceleration values could be routed to a Transform node. But in order to smooth the values we are putting a PositionDamper inbetween.
Code: Mapping the acceleration values
<Transform DEF='tr'> <Shape> <Appearance> <Material diffuseColor='1 1 1' /> </Appearance> <Box/> </Shape> </Transform> <PositionDamper DEF='pd' tau='0.1' /> <ROUTE fromNode='AppleMotionSensor' fromField='Motion' toNode='pd' toField='set_destination'/> <ROUTE fromNode='pd' fromField='value_changed' toNode='tr' toField='set_translation'/>
Tilt
In this second example we are mapping the acceleration on the orientation of an object.
Calling the SFRotation() constructor with the acceleration vector and a vector SFVec3f(0,1,0) calculates the sensor's orientation. By routing that value on a Transform's rotation the object seems to keep its position while rotating the notebook.
Code: Creating rotation relative to the ground
<Script DEF='script'> <field accessType='inputOnly' name='set_motion' type='SFVec3f'/> <field accessType='outputOnly' name='rotation_changed' type='SFRotation'/> <![CDATA[javascript: var rotation_changed; var vector = new SFVec3f(0,1,0); function set_motion(motion) { rotation_changed = new SFRotation(motion, vector); } ] ]> </Script> <ROUTE fromNode='AppleMotionSensor' fromField='Motion' toNode='script' toField='set_motion'/> <ROUTE fromNode='script' fromField='rotation_changed' toNode='tr' toField='set_rotation'/>
Comments
This tutorial has no comments.
Add a new comment
Due to excessive spamming we have disabled the comment functionality for tutorials. Please use our forum to post any questions.