2012-01-04 2 views
4

모션 API을 사용 중이며 현재 개발중인 게임에 대한 제어 구성표를 찾으려고합니다.오리엔테이션을 사용하여 위치를 계산하는 Windows phone

내가 노력하려고하는 것은 장치를 위치에 직접 배치하는 것입니다. 전화가 앞으로 기울고 왼쪽으로 기울이면 왼쪽 상단 위치를 나타내고 오른쪽으로 돌아 오면 오른쪽 하단 위치가됩니다.

사진은 명확하게 나타냅니다 (빨간색 점은 계산 된 위치 임). 까다로운 비트 이제

Tilt left top
앞으로 및 왼쪽

Tilt right bottom
뒤로하고 오른쪽

. 또한 값에 왼쪽 가로 및 오른쪽 가로 방향의 장치 방향을 고려해야합니다 (세로가 기본값이므로 계산이 필요하지 않습니다).

누구나 이와 같은 작업을 했습니까?

참고 :

  • 나는 요, 피치, 롤 및 사원 수 측정 값을 사용하여 시도했습니다.

  • 나는 방금 내가 레벨에 대해 많이 생각하는 행동을 실감했다.

샘플 :

// Get device facing vector 
public static Vector3 GetState() 
{ 
    lock (lockable) 
    { 
     var down = Vector3.Forward; 

     var direction = Vector3.Transform(down, state); 
     switch (Orientation) { 
      case Orientation.LandscapeLeft: 
       return Vector3.TransformNormal(direction, Matrix.CreateRotationZ(-rightAngle)); 
      case Orientation.LandscapeRight: 
       return Vector3.TransformNormal(direction, Matrix.CreateRotationZ(rightAngle)); 
     } 

     return direction; 
    } 
} 

답변

2

당신은 가속도 센서를 사용하여 화면에 객체를 제어하고 싶습니다.

protected override void Initialize() { 
... 
    Accelerometer acc = new Accelerometer(); 
    acc.ReadingChanged += AccReadingChanged; 
    acc.Start(); 
... 
} 

이 I 게임 내 X와 같은 센서의 게임과 Y 축 내 Y로 센서의 Z 축을 사용하고 객체

void AccReadingChanged(object sender, AccelerometerReadingEventArgs e) { 
    // Y axes is same in both cases 
    this.circlePosition.Y = (float)e.Z * GraphicsDevice.Viewport.Height + GraphicsDevice.Viewport.Height/2.0f; 

    // X axes needs to be negative when oriented Landscape - Left 
    if (Window.CurrentOrientation == DisplayOrientation.LandscapeLeft) 
     this.circlePosition.X = -(float)e.Y * GraphicsDevice.Viewport.Width + GraphicsDevice.Viewport.Width/2.0f; 
    else this.circlePosition.X = (float)e.Y * GraphicsDevice.Viewport.Width + GraphicsDevice.Viewport.Width/2.0f; 
} 

의 위치를 ​​계산하는 방법이다. 중심에서 센서의 Z 축을 뺀 값으로 교정합니다. 이 방법으로 센서 축이 화면의 위치 (백분율)에 직접 대응합니다. 우리 모두에서 센서의 X 축이 필요하지 않습니다이 작업을 수행하려면

...

이 그냥 빨리 구현입니다. 이 Viewport.Width/2f은 3 개 측정의 중심, 합 및 평균이 아니기 때문에 X 센서 축에서 보정 할 수 있으므로 센서 중심을 찾을 수 있습니다.

이 코드는 Windows Phone Device에서 테스트되었습니다! (및 작동)

+0

중력 벡터 사용에 대해 완전히 잊어 버렸습니다. –

+0

답변으로 표시해주세요 ...;) –