Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
zalo's avatar
zalo
Explorer
13 years ago

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.

2 Replies

Replies have been turned off for this discussion
  • 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.
  • 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 "}".