public void ReadState(ControllerState outState) { if (error) { outState.connectionState = GvrConnectionState.Error; outState.errorDetails = errorDetails; return; } gvr_controller_state_update(api, 0, statePtr); outState.connectionState = ConvertConnectionState( gvr_controller_state_get_connection_state(statePtr)); gvr_quat rawOri = gvr_controller_state_get_orientation(statePtr); gvr_vec3 rawAccel = gvr_controller_state_get_accel(statePtr); gvr_vec3 rawGyro = gvr_controller_state_get_gyro(statePtr); // Convert GVR API orientation (right-handed) into Unity axis system (left-handed). pose3d.Set(Vector3.zero, new Quaternion(rawOri.x, rawOri.y, rawOri.z, rawOri.w)); pose3d.SetRightHanded(pose3d.Matrix); outState.orientation = pose3d.Orientation; // For accelerometer, we have to flip Z because the GVR API has Z pointing backwards // and Unity has Z pointing forward. outState.accel = new Vector3(rawAccel.x, rawAccel.y, -rawAccel.z); // Gyro in GVR represents a right-handed angular velocity about each axis (positive means // clockwise when sighting along axis). Since Unity uses a left-handed system, we flip the // signs to adjust the sign of the rotational velocity (so that positive means // counter-clockwise). In addition, since in Unity the Z axis points forward while GVR // has Z pointing backwards, we flip the Z axis sign again. So the result is that // we should use -X, -Y, +Z: outState.gyro = new Vector3(-rawGyro.x, -rawGyro.y, rawGyro.z); outState.isTouching = 0 != gvr_controller_state_is_touching(statePtr); gvr_vec2 touchPos = gvr_controller_state_get_touch_pos(statePtr); outState.touchPos = new Vector2(touchPos.x, touchPos.y); outState.touchDown = 0 != gvr_controller_state_get_touch_down(statePtr); outState.touchUp = 0 != gvr_controller_state_get_touch_up(statePtr); outState.appButtonDown = 0 != gvr_controller_state_get_button_down(statePtr, GVR_CONTROLLER_BUTTON_APP); outState.appButtonState = 0 != gvr_controller_state_get_button_state(statePtr, GVR_CONTROLLER_BUTTON_APP); outState.appButtonUp = 0 != gvr_controller_state_get_button_up(statePtr, GVR_CONTROLLER_BUTTON_APP); outState.clickButtonDown = 0 != gvr_controller_state_get_button_down(statePtr, GVR_CONTROLLER_BUTTON_CLICK); outState.clickButtonState = 0 != gvr_controller_state_get_button_state(statePtr, GVR_CONTROLLER_BUTTON_CLICK); outState.clickButtonUp = 0 != gvr_controller_state_get_button_up(statePtr, GVR_CONTROLLER_BUTTON_CLICK); outState.recentering = 0 != gvr_controller_state_get_recentering(statePtr); outState.recentered = 0 != gvr_controller_state_get_recentered(statePtr); }
public void ReadState(ControllerState outState, int controller_id) { if (error) { outState.connectionState = GvrConnectionState.Error; outState.apiStatus = GvrControllerApiStatus.Error; outState.errorDetails = errorDetails; return; } if (api == IntPtr.Zero || statePtr == IntPtr.Zero) { Debug.LogError("AndroidNativeControllerProvider used after dispose."); return; } gvr_controller_state_update(api, controller_id, statePtr); outState.connectionState = ConvertConnectionState( gvr_controller_state_get_connection_state(statePtr)); outState.apiStatus = ConvertControllerApiStatus( gvr_controller_state_get_api_status(statePtr)); gvr_quat rawOri = gvr_controller_state_get_orientation(statePtr); gvr_vec3 rawAccel = gvr_controller_state_get_accel(statePtr); gvr_vec3 rawGyro = gvr_controller_state_get_gyro(statePtr); gvr_vec3 rawPos = gvr_controller_state_get_position(statePtr); // Convert GVR API orientation (right-handed) into Unity axis system (left-handed). pose3d.Set(new Vector3(rawPos.x, rawPos.y, rawPos.z), new Quaternion(rawOri.x, rawOri.y, rawOri.z, rawOri.w)); pose3d.SetRightHanded(pose3d.Matrix); outState.orientation = pose3d.Orientation; outState.position = pose3d.Position; // For accelerometer, we have to flip Z because the GVR API has Z pointing backwards // and Unity has Z pointing forward. outState.accel = new Vector3(rawAccel.x, rawAccel.y, -rawAccel.z); // Gyro in GVR represents a right-handed angular velocity about each axis (positive means // clockwise when sighting along axis). Since Unity uses a left-handed system, we flip the // signs to adjust the sign of the rotational velocity (so that positive means // counter-clockwise). In addition, since in Unity the Z axis points forward while GVR // has Z pointing backwards, we flip the Z axis sign again. So the result is that // we should use -X, -Y, +Z: outState.gyro = new Vector3(-rawGyro.x, -rawGyro.y, rawGyro.z); gvr_vec2 touchPos = gvr_controller_state_get_touch_pos(statePtr); outState.touchPos = new Vector2(touchPos.x, touchPos.y); int[] gvr_buttons = new int[] { GVR_CONTROLLER_BUTTON_APP, GVR_CONTROLLER_BUTTON_HOME, GVR_CONTROLLER_BUTTON_CLICK, GVR_CONTROLLER_BUTTON_RESERVED0, GVR_CONTROLLER_BUTTON_RESERVED1, GVR_CONTROLLER_BUTTON_RESERVED2 }; GvrControllerButton[] gvrUnityButtons = new GvrControllerButton[] { GvrControllerButton.App, GvrControllerButton.System, GvrControllerButton.TouchPadButton, GvrControllerButton.Reserved0, GvrControllerButton.Reserved1, GvrControllerButton.Reserved2 }; outState.buttonsState = 0; for (int i = 0; i < gvr_buttons.Length; i++) { if (0 != gvr_controller_state_get_button_state(statePtr, gvr_buttons[i])) { outState.buttonsState |= gvrUnityButtons[i]; } } if (0 != gvr_controller_state_is_touching(statePtr)) { outState.buttonsState |= GvrControllerButton.TouchPadTouch; } outState.SetButtonsUpDownFromPrevious(lastButtonsState[controller_id]); lastButtonsState[controller_id] = outState.buttonsState; outState.recentered = 0 != gvr_controller_state_get_recentered(statePtr); outState.gvrPtr = statePtr; if (hasBatteryMethods) { outState.isCharging = 0 != gvr_controller_state_get_battery_charging(statePtr); outState.batteryLevel = (GvrControllerBatteryLevel)gvr_controller_state_get_battery_level(statePtr); } }
public void ReadState(ControllerState outState, int controller_id) { if (error) { outState.connectionState = GvrConnectionState.Error; outState.apiStatus = GvrControllerApiStatus.Error; outState.errorDetails = errorDetails; return; } IntPtr statePtr = statePtr_arr[controller_id]; gvr_controller_state_update(api, controller_id, statePtr); //Debug.Log("Reading state for controller<<>> " + controller_id.ToString()); //Debug.Log("state pointer<<>> " +statePtr.ToString()); int controller_count = gvr_controller_get_count(api); //Debug.Log("controller_count<<>> " +controller_count.ToString()); outState.connectionState = ConvertConnectionState( gvr_controller_state_get_connection_state(statePtr)); outState.apiStatus = ConvertControllerApiStatus( gvr_controller_state_get_api_status(statePtr)); gvr_quat rawOri = gvr_controller_state_get_orientation(statePtr); gvr_vec3 rawAccel = gvr_controller_state_get_accel(statePtr); gvr_vec3 rawGyro = gvr_controller_state_get_gyro(statePtr); // saketp gvr_vec3 rawPos = gvr_controller_state_get_position(statePtr); // Convert GVR API orientation (right-handed) into Unity axis system (left-handed). // saketp pose3d.Set(new Vector3(rawPos.x, rawPos.y, rawPos.z), new Quaternion(rawOri.x, rawOri.y, rawOri.z, rawOri.w)); pose3d.SetRightHanded(pose3d.Matrix); outState.orientation = pose3d.Orientation; // saketp outState.position = pose3d.Position; //Debug.Log("PositionGVR<<>> " + outState.position.ToString()); //Debug.Log("PositionGVR<<>> " + outState.position.ToString()); // Debug.Log("OrientationGVR<<>> " + outState.orientation.ToString()); // For accelerometer, we have to flip Z because the GVR API has Z pointing backwards // and Unity has Z pointing forward. outState.accel = new Vector3(rawAccel.x, rawAccel.y, -rawAccel.z); // Gyro in GVR represents a right-handed angular velocity about each axis (positive means // clockwise when sighting along axis). Since Unity uses a left-handed system, we flip the // signs to adjust the sign of the rotational velocity (so that positive means // counter-clockwise). In addition, since in Unity the Z axis points forward while GVR // has Z pointing backwards, we flip the Z axis sign again. So the result is that // we should use -X, -Y, +Z: outState.gyro = new Vector3(-rawGyro.x, -rawGyro.y, rawGyro.z); // statePtr = statePtr_arr[0]; outState.isTouching = 0 != gvr_controller_state_is_touching(statePtr); gvr_vec2 touchPos = gvr_controller_state_get_touch_pos(statePtr); outState.touchPos = new Vector2(touchPos.x, touchPos.y); outState.touchDown = 0 != gvr_controller_state_get_touch_down(statePtr); outState.touchUp = 0 != gvr_controller_state_get_touch_up(statePtr); outState.appButtonDown = 0 != gvr_controller_state_get_button_down(statePtr, GVR_CONTROLLER_BUTTON_APP); outState.appButtonState = 0 != gvr_controller_state_get_button_state(statePtr, GVR_CONTROLLER_BUTTON_APP); outState.appButtonUp = 0 != gvr_controller_state_get_button_up(statePtr, GVR_CONTROLLER_BUTTON_APP); outState.homeButtonDown = 0 != gvr_controller_state_get_button_down(statePtr, GVR_CONTROLLER_BUTTON_HOME); outState.homeButtonState = 0 != gvr_controller_state_get_button_state(statePtr, GVR_CONTROLLER_BUTTON_HOME); outState.clickButtonDown = 0 != gvr_controller_state_get_button_down(statePtr, GVR_CONTROLLER_BUTTON_CLICK); outState.clickButtonState = 0 != gvr_controller_state_get_button_state(statePtr, GVR_CONTROLLER_BUTTON_CLICK); outState.clickButtonUp = 0 != gvr_controller_state_get_button_up(statePtr, GVR_CONTROLLER_BUTTON_CLICK); outState.triggerButtonDown = 0 != gvr_controller_state_get_button_down(statePtr, GVR_CONTROLLER_BUTTON_TRIGGER); outState.triggerButtonState = 0 != gvr_controller_state_get_button_state(statePtr, GVR_CONTROLLER_BUTTON_TRIGGER); outState.triggerButtonUp = 0 != gvr_controller_state_get_button_up(statePtr, GVR_CONTROLLER_BUTTON_TRIGGER); outState.recentering = 0 != gvr_controller_state_get_recentering(statePtr); outState.recentered = 0 != gvr_controller_state_get_recentered(statePtr); outState.gvrPtr = statePtr; if (hasBatteryMethods) { outState.isCharging = 0 != gvr_controller_state_get_battery_charging(statePtr); outState.batteryLevel = (GvrControllerBatteryLevel)gvr_controller_state_get_battery_level(statePtr); } }