lightngames’s blog

DirectXを使ったレンダリング技術を周りに紹介しています。

C++でVRのモーションコントローラーを使う方法

 

今回はC++VRのモーションコントローラーを使い方法をご紹介します。

 

モーションコントローラーについてはご説明しませんのでこちらを参考にどうぞ…(適当)

 

早速サンプルコードです。

VRHand.h


#pragma once #include "GameFramework/Actor.h" #include "MotionControllerComponent.h" #include "VRHand.generated.h" UCLASS() class VRTest_API AVRHand : public AActor {   GENERATED_BODY() public:   AVRHand();   virtual void BeginPlay() override;   virtual void Tick( float DeltaSeconds ) override; private:   UPROPERTY(VisibleAnywhere)   UMotionControllerComponent* MotionController;//モーションコントローラー };

 

VRHand.cpp

#include "VRTest.h"
#include "VRHand.h"
AVRHand::AVRHand()
{
  PrimaryActorTick.bCanEverTick = true;

  MotionController = CreateDefaultSubobject(TEXT("MotionController"));
  Mesh = CreateDefaultSubobject(TEXT("Mesh"));

  MotionController->AttachTo(RootComponent);
  Mesh->AttachTo(MotionController);

}

void AVRHand::BeginPlay()
{
  Super::BeginPlay();
}

void AVRHand::Tick( float DeltaTime )
{
  Super::Tick( DeltaTime );
}

 

ちなみに上の二つだけでは動きません。ここがはまりポイントです。

ソリューションのデフォルトの設定ではVR系のヘッダーをどうやらビルドしてくれないらしくてこれらを一緒にビルドしてくれるように若干設定がいります。

「プロジェクト名.Builld.cs」というファイルにHTC VIVEなら

using UnrealBuildTool;

public class VRTest : ModuleRules
{
  public VRTest(TargetInfo Target)
  {
    PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });

    /* VR Required Modules */
    PrivateDependencyModuleNames.AddRange(new string[] { "HeadMountedDisplay", "SteamVR" });


    // Uncomment if you are using Slate UI
    // PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
		
    // Uncomment if you are using online features
    // PrivateDependencyModuleNames.Add("OnlineSubsystem");

    // To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
  }
}

のように追加してください。

これでビルドが通るようになります。