Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System.Reflection;
using System.IO;
/// <summary>
/// NovintFalconを操作デバイスとして使うscript ※Windows用(dll依存)
///
/// usage:
/// 1. http://forum.unity3d.com/threads/6494-Novint-falcon から
/// Falcon Wrapper Source - Update V1.zip をDL、FalconWrapper.dllをAssetsと同階層へコピー
/// ビルド後はWindowsのPathが通っているところにFalconWrapper.dllを置くか、バイナリと同階層に置く
/// 2. このスクリプトをプロジェクトへIn
/// 3. 操作したい対象のGameObjectにAdd Component
/// 4. Inspectorで適当に弄って察する
/// ※対象の動きをNovintFalconへHapticする方法は_feedback参照
///
/// NovintFalconの正しい使い方 - Unity Advent Calendar 2013 vol.23
/// http://yunojy.github.io/blog/2013/12/23/how-to-use-novintfalcon-unity-advent-calendar-2013-vol-dot-23/
/// </summary>
public class DeviceNovintFalcon : MonoBehaviour {
#region FalconWrapper.dll Variables
const string falcon = "Falcon Wrapper.dll";
[DllImport(falcon)]
private static extern void StartHaptics();
[DllImport(falcon)]
private static extern void StopHaptics();
[DllImport(falcon)]
private static extern bool IsDeviceCalibrated();
[DllImport(falcon)]
private static extern bool IsDeviceReady();
[DllImport(falcon)]
private static extern double GetXPos();
[DllImport(falcon)]
private static extern double GetYPos();
[DllImport(falcon)]
private static extern double GetZPos();
[DllImport(falcon)]
private static extern void SetServo(double[] speed);
[DllImport(falcon)]
private static extern void SetServoPos(double[] pos, double strength);
[DllImport(falcon)]
private static extern bool IsHapticButtonDepressed();
[DllImport(falcon)]
private static extern int GetButtonsDown();
[DllImport(falcon)]
private static extern bool isButton0Down();
[DllImport(falcon)]
private static extern bool isButton1Down();
[DllImport(falcon)]
private static extern bool isButton2Down();
[DllImport(falcon)]
private static extern bool isButton3Down();
public static DeviceNovintFalcon main;
#endregion
#region Variables
[Range(-10f, 100f)]
public float SpeedX = 0f;
[Range(-10f, 100f)]
public float SpeedY = 0f;
[Range(-10f, 100f)]
public float SpeedZ = -10f;
[Range(-1.5f, 1.5f)]
public float PosX = 0f;
[Range(-1.5f, 1.5f)]
public float PosY = 0f;
[Range(-1.5f, 1.5f)]
public float PosZ = 0f;
[Range(0f, 100f)]
public float Strength = 3f;
protected CharacterController Charactor = null;
private Vector3 MoveThrottle = Vector3.zero;
public float TranslationSensitivity = 10.0f;
#endregion
//-------------------------------------------------
#region Start()
void Start() {
StartHaptics();
StartCoroutine(_initHaptics());
Charactor = gameObject.GetComponent<CharacterController>();
}
private IEnumerator _initHaptics() {
while (!IsDeviceCalibrated()) {
Debug.LogWarning("Please calibrate the device!");
yield return new WaitForSeconds(1.5f);
}
if (!IsDeviceReady())
Debug.LogError("Device is not ready!");
main = this;
}
#endregion
//-------------------------------------------------
#region Update()
void Update() {
// デバイス動作制御
_feedback();
// キャラクター動作制御
_charaMove();
//gameObject.transform.position = new Vector3((float)GetXPos() * -2, (float)GetYPos() * 2, (float)GetZPos() * 2);
//Debug.Log(isButton0Down() + " , " + isButton1Down() + " , " + isButton2Down() + " , " + isButton3Down());
}
/// <summary>
/// デバイス動作制御
/// </summary>
private void _feedback() {
// NovintFalconのグリップをデフォ位置に戻す
SetServo(new double[3] { SpeedX, SpeedY, SpeedZ });
SetServoPos(new double[3] { PosX, PosY, PosZ }, Strength);
//Debug.Log(GetServoPos());
}
/// <summary>
/// キャラクター動作制御
/// </summary>
private void _charaMove() {
if ((float)GetZPos() >= 0.8f) MoveThrottle += __moveVector(Vector3.forward);
if ((float)GetZPos() <= -0.8f) MoveThrottle += __moveVector(Vector3.back);
if ((float)GetXPos() >= 0.8f) MoveThrottle += __moveVector(Vector3.left);
if ((float)GetXPos() <= -0.8f) MoveThrottle += __moveVector(Vector3.right);
if ((float)GetYPos() >= 0.8f) MoveThrottle += __moveVector(Vector3.up * 2);
if ((float)GetYPos() <= -0.8f) MoveThrottle += __moveVector(Vector3.down);
float motorDamp = (1.0f + 0.15f);
MoveThrottle.x /= motorDamp;
MoveThrottle.y = (MoveThrottle.y > 0.0f) ? (MoveThrottle.y / motorDamp) : MoveThrottle.y;
MoveThrottle.z /= motorDamp;
Charactor.Move(MoveThrottle);
}
private Vector3 __moveVector(Vector3 moveVector) {
return Charactor.transform.TransformDirection(
moveVector * Time.deltaTime * TranslationSensitivity);
}
#endregion
//-------------------------------------------------
#region Misc
void OnApplicationQuit() {
StopHaptics();
}
public Vector3 GetServoPos() {
return new Vector3((float)GetXPos(), (float)GetYPos(), -(float)GetZPos());
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment