Skip to content

Commit

Permalink
enable vessel control via joystick
Browse files Browse the repository at this point in the history
  • Loading branch information
Vivero committed Apr 27, 2020
1 parent 5c77175 commit fa38ea0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
52 changes: 49 additions & 3 deletions KerbalVR/Components/KVR_AvionicsComputer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void Awake() {

void Start() {
// define the active vessel to control
// FlightGlobals.ActiveVessel.OnFlyByWire += VesselControl;
FlightGlobals.ActiveVessel.OnFlyByWire += VesselControl;
}

void OnEnable() {
Expand Down Expand Up @@ -79,8 +79,54 @@ void OnPrecisionModeInput(int signal) {
FlightInputHandler.fetch.precisionMode = (signal != 0);
}

/*void VesselControl(FlightCtrlState state) {
}*/
void VesselControl(FlightCtrlState state) {
bool isControlling = false;
float yawControl = 0f;
float pitchControl = 0f;
float rollControl = 0f;
string logMsg = "";

SteamVR_Controller.Device rightHandState = KerbalVR.DeviceManager.GetManipulatorRightState();
if (rightHandState != null) {
logMsg += "=== RIGHT HAND ===\n";
logMsg += "Axis0: " + rightHandState.GetAxis(Valve.VR.EVRButtonId.k_EButton_Axis0) + (rightHandState.GetTouch(Valve.VR.EVRButtonId.k_EButton_Axis0) ? "pressed" : "") + "\n";
logMsg += "Axis1: " + rightHandState.GetAxis(Valve.VR.EVRButtonId.k_EButton_Axis1) + (rightHandState.GetTouch(Valve.VR.EVRButtonId.k_EButton_Axis1) ? "pressed" : "") + "\n";
logMsg += "Axis2: " + rightHandState.GetAxis(Valve.VR.EVRButtonId.k_EButton_Axis2) + (rightHandState.GetTouch(Valve.VR.EVRButtonId.k_EButton_Axis2) ? "pressed" : "") + "\n"; // index ctrl joystick
logMsg += "Axis3: " + rightHandState.GetAxis(Valve.VR.EVRButtonId.k_EButton_Axis3) + (rightHandState.GetTouch(Valve.VR.EVRButtonId.k_EButton_Axis3) ? "pressed" : "") + "\n";
logMsg += "JoyStick: " + rightHandState.GetAxis(Valve.VR.EVRButtonId.k_EButton_IndexController_JoyStick) + "\n";

if (rightHandState.GetTouch(Valve.VR.EVRButtonId.k_EButton_Axis2)) {
Vector2 joystickState = rightHandState.GetAxis(Valve.VR.EVRButtonId.k_EButton_Axis2);
yawControl = joystickState.x;
pitchControl = joystickState.y;
isControlling = true;
}
}

SteamVR_Controller.Device leftHandState = KerbalVR.DeviceManager.GetManipulatorLeftState();
if (leftHandState != null) {
logMsg += "=== LEFT HAND ===\n";
logMsg += "Axis0: " + leftHandState.GetAxis(Valve.VR.EVRButtonId.k_EButton_Axis0) + (leftHandState.GetTouch(Valve.VR.EVRButtonId.k_EButton_Axis0) ? "pressed" : "") + "\n";
logMsg += "Axis1: " + leftHandState.GetAxis(Valve.VR.EVRButtonId.k_EButton_Axis1) + (leftHandState.GetTouch(Valve.VR.EVRButtonId.k_EButton_Axis1) ? "pressed" : "") + "\n";
logMsg += "Axis2: " + leftHandState.GetAxis(Valve.VR.EVRButtonId.k_EButton_Axis2) + (leftHandState.GetTouch(Valve.VR.EVRButtonId.k_EButton_Axis2) ? "pressed" : "") + "\n"; // index ctrl joystick
logMsg += "Axis3: " + leftHandState.GetAxis(Valve.VR.EVRButtonId.k_EButton_Axis3) + (leftHandState.GetTouch(Valve.VR.EVRButtonId.k_EButton_Axis3) ? "pressed" : "") + "\n";
logMsg += "JoyStick: " + leftHandState.GetAxis(Valve.VR.EVRButtonId.k_EButton_IndexController_JoyStick) + "\n";

if (leftHandState.GetTouch(Valve.VR.EVRButtonId.k_EButton_Axis2)) {
Vector2 joystickState = leftHandState.GetAxis(Valve.VR.EVRButtonId.k_EButton_Axis2);
rollControl = joystickState.x;
isControlling = true;
}
}

if (isControlling) {
state.yaw = yawControl;
state.pitch = pitchControl;
state.roll = rollControl;
}

Utils.SetDebugText(logMsg);
}

} // class KVR_AvionicsComputer
} // namespace KerbalVR
14 changes: 1 addition & 13 deletions KerbalVR/KerbalVR_Manipulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected void Awake() {

// create game objects for this hand
CreateGloveGameObject();
CreateOtherGameObjects();
// CreateOtherGameObjects();
}

protected void Update() {
Expand Down Expand Up @@ -317,24 +317,12 @@ protected void OnManipulatorUpdated(SteamVR_Controller.Device state) {
if (TargetObject != null) {
TargetObject.SendMessage("OnMouseDown");
}

GameObject go = GameObject.Find("KVR_UI_ResetPosButton");
if (go != null) {
Utils.Log("PointerDown KVR_UI_ResetPosButton");
ExecuteEvents.Execute<IPointerDownHandler>(go, new PointerEventData(EventSystem.current), ExecuteEvents.pointerDownHandler);
}
}

if (state.GetPressUp(EVRButtonId.k_EButton_SteamVR_Trigger)) {
if (TargetObject != null) {
TargetObject.SendMessage("OnMouseUp");
}

GameObject go = GameObject.Find("KVR_UI_ResetPosButton");
if (go != null) {
Utils.Log("PointerUp KVR_UI_ResetPosButton");
ExecuteEvents.Execute<IPointerUpHandler>(go, new PointerEventData(EventSystem.current), ExecuteEvents.pointerUpHandler);
}
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions KerbalVR/KerbalVR_Scene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -573,13 +573,13 @@ public void OnManipulatorLeftUpdated(SteamVR_Controller.Device state) {
// simulate mouse touch events with the trigger
if (state.GetPressDown(EVRButtonId.k_EButton_SteamVR_Trigger)) {
foreach (var obj in DeviceManager.Instance.ManipulatorLeft.FingertipCollidedGameObjects) {
obj.SendMessage("OnMouseDown");
if (obj != null) obj.SendMessage("OnMouseDown");
}
}

if (state.GetPressUp(EVRButtonId.k_EButton_SteamVR_Trigger)) {
foreach (var obj in DeviceManager.Instance.ManipulatorLeft.FingertipCollidedGameObjects) {
obj.SendMessage("OnMouseUp");
if (obj != null) obj.SendMessage("OnMouseUp");
}
}
}
Expand Down Expand Up @@ -610,13 +610,13 @@ public void OnManipulatorRightUpdated(SteamVR_Controller.Device state) {
// simulate mouse touch events with the trigger
if (state.GetPressDown(EVRButtonId.k_EButton_SteamVR_Trigger)) {
foreach (var obj in DeviceManager.Instance.ManipulatorRight.FingertipCollidedGameObjects) {
obj.SendMessage("OnMouseDown");
if (obj != null) obj.SendMessage("OnMouseDown");
}
}

if (state.GetPressUp(EVRButtonId.k_EButton_SteamVR_Trigger)) {
foreach (var obj in DeviceManager.Instance.ManipulatorRight.FingertipCollidedGameObjects) {
obj.SendMessage("OnMouseUp");
if (obj != null) obj.SendMessage("OnMouseUp");
}
}
}
Expand Down

0 comments on commit fa38ea0

Please sign in to comment.