检测Google Cardboard磁性按钮的点击 [英] Detecting Google Cardboard Magnetic Button Click

查看:186
本文介绍了检测Google Cardboard磁性按钮的点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用纸板开发一个VR应用程序.当我在磁性按钮上单击两次时,我希望菜单出现.

I'm developing a VR app with cardboard. When I click twice on the magnetic button, I want the menu to appear.

如何用C#编写此条件?

How do I write this condition in C#?

推荐答案

简而言之,您需要检测到手机的指南针已经改变而没有任何倾斜(即,磁铁已经靠近手机并破坏了它的指南针,而没有改变它的方向).物理方向).

In brief, you need to detect that your phone's compass changed without any tilt (i.e., the magnet has gotten close to the phone and disrupted its compass without changing its physical orientation).

您需要查看两个向量,看看它们是否已更改.

You'll want to look at both vectors and see if they've changed.

有两种开源实现,我最简单地将其包括在下面(全部归功于Secret Ingredient Games的安德鲁·怀特):

There's two open source implementations, the simplest of which I shamelessly include below (all credit to Andrew Whyte from Secret Ingredient Games):

  1. http://www.andrewnoske.com/wiki/Unity_-__Detecting_Google_Cardboard_Click
  2. https://github.com/CaseyB/UnityCardboardTrigger/blob/develop/MagnetSensor.cs
  1. http://www.andrewnoske.com/wiki/Unity_-_Detecting_Google_Cardboard_Click
  2. https://github.com/CaseyB/UnityCardboardTrigger/blob/develop/MagnetSensor.cs

/*     -- MIT/X11 like license --
Copyright (c) 2014 Paramita ltd, (Secret Ingredient Games)
*/

//
//  Google Cardboard click code in C# for Unity.
//  Author: Andrew Whyte
//
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

//public static XmlDocument XmlDoc;
//public static XmlNodeList xnl;
//public TextAsset TA;

public class magneticClick {
  //  Concept: two FIR filters,  running on Magnetics and tilt.
  //  If the tilt hasn't changed, but the compass has, then the magnetic field moved
  //  without device this is the essence of a cardboard magnet click.
  private Vector3 lastTiltVector;
  public float tiltedBaseLine = 0f;
  public float magnetBaseLine = 0f;

  public float tiltedMagn = 0f;
  public float magnetMagn = 0f;

  private int N_SlowFIR = 25;
  private int N_FastFIR_magnet = 3;
  private int N_FastFIR_tilted = 5;  // Clicking the magnet tends to tilt the device slightly.


  public float threshold = 1.0f;

  bool click = false;
  bool clickReported = false;

  public void init() {
    Input.compass.enabled = true;

    // Note that init is platform specific to unity.
    magnetMagn = Input.compass.rawVector.magnitude;
    magnetBaseLine = Input.compass.rawVector.magnitude;
    tiltedBaseLine = Input.acceleration.magnitude;
    tiltedMagn = Input.acceleration.magnitude;
  }

  public void magUpdate(Vector3 acc,  Vector3 compass) {
    // Call this function in the Update of a monobehaviour as follows:
    // <magneticClickInstance>.magUpdate(Input.acceleration, Input.compass.rawVector);

    // we are interested in the change of the tilt not the actual tilt.
    Vector3 TiltNow = acc;
    Vector3 motionVec3 = TiltNow - lastTiltVector;
    lastTiltVector = TiltNow;

    // update tilt and compass "fast" values
    tiltedMagn = ((N_FastFIR_tilted-1) * tiltedMagn + motionVec3.magnitude) / N_FastFIR_tilted;
    magnetMagn = ((N_FastFIR_magnet-1) * magnetMagn + compass.magnitude) / N_FastFIR_magnet;

    // update the "slow" values
    tiltedBaseLine = ( (N_SlowFIR-1) * tiltedBaseLine + motionVec3.magnitude) / N_SlowFIR;
    magnetBaseLine = ( (N_SlowFIR-1) * magnetBaseLine + compass.magnitude) / N_SlowFIR;

    if( tiltedMagn < 0.2 && (magnetMagn / magnetBaseLine) > 1.1  ) {
      if( clickReported == false) {
        click = true;
      }
      clickReported = true;
    } else  {
      clickReported = false;
    }
  }

  public bool clicked()  {
    // Basic premise is that the magnitude of magnetic field should change while the 
    // device is steady.  This seems to be suiltable for menus etc.

    // Clear the click by reading (so each 'click' returns true only once)
    if(click == true) {
      click = false;
      return true;
    } else {
      return false;
    }
  }
}

这篇关于检测Google Cardboard磁性按钮的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆