Skip to main content
Up and Coming
October 14, 2013

Gravity Compensated Acceleration Vector

Here's the code to get a gravity compensated acceleration vector from the Oculus Sensor:
using UnityEngine;
using System.Collections;

public class Derp : MonoBehaviour {
public GameObject RiftCam;
float x = 0 ;
float y = 0;
float z = 0;

// Update is called once per frame
void Update () {
OVRDevice.GetAcceleration(0, ref x, ref y, ref z);
Vector3 Down = new Vector3(RiftCam.transform.position.x, RiftCam.transform.position.y+9.81f, RiftCam.transform.position.z);
Down = RiftCam.transform.InverseTransformPoint(Down);
Vector3 GravityCompensatedAcceleration = new Vector3(x-Down.x,y-Down.y,z+Down.z);
}
}


Drag the RightCamera object in the Rift Camera Prefab to the "RiftCam" slot. It needs this to use the orientation of the Rift to compensate for gravity. Not the best way, but InversePointTransform only takes transforms, so the RightCam already has one pre-made.


Enjoy.
    This topic has been closed for replies.

    2 replies

    Participant
    October 15, 2013
    noob-question: how to run the code? :)
    I've added the script into the OVRcc right below the OVRDevice script and then dragged the Right Camera into RiftCam slot, but it does nothing.
    zaloAuthor
    Up and Coming
    October 20, 2013
    Oh it doesn't do anything by itself.

    That snippet just gives you the vector by itself.

    Just copy the salient bits between "public class" and the last "}" into your own scripts and interactions.

    Or, if you want, put the whole thing into Derp.cs and add Debug.Log(GravityCompensatedAcceleration); to the end before the last "}".