Source/xsite_ue/VRPNFlystickActor.cpp
// Called when the game starts or when spawned

Called when the game starts or when spawned
Called when the game starts or when spawned
void AVRPNFlystickActor::BeginPlay()
{
    Super::BeginPlay();
    UE_LOG(LogCave, Warning, TEXT("Spawning Flystick on %s"), HasAuthority() ? TEXT("Server") : TEXT("Client"));
    if (!GetWorld()->IsServer())
        return;
    auto CaveGameInstance = Cast<UCaveGameInstance>(GetWorld()->GetGameInstance());
    auto vrpnController = CaveGameInstance->GetVRPNControllerActor("DTrack")->GetController();
    FVector EyeOrigin = FVector(0,0,0);
    if (CaveGameInstance->GetCaveController() != nullptr)
    {
        EyeOrigin = CaveGameInstance->GetCaveController()->EyeOrigin;
    }
    if (vrpnController == nullptr)
    {
        UE_LOG(LogCave, Warning, TEXT("[AVRPNFlystickActor::BeginPlay] Could not obtain VrpnController from CaveController or GameInstance"));
        return;
    }
    auto CaveHead = CaveGameInstance->GetCaveHeadCharacter();
    if(IsValid(CaveHead))
        this->PlayerStartLocation = CaveHead->GetPlayerStartLocation();
    vrpnController->AddTrackerChangedCallback(
        [this, vrpnController, EyeOrigin](int32 sensor, VRPNController::TrackerData trackerData) {
            if (sensor != 18 && sensor != 15)
                return;
            float xOffset = -EyeOrigin.X;
            float yOffset = -EyeOrigin.Y;
            float zOffset = -EyeOrigin.Z;
            this->Start = FVector(
                (float)(trackerData.Pos[1] * 100.f + xOffset),
                (float)(trackerData.Pos[0] * 100.f + yOffset),
                (float)(trackerData.Pos[2] * 100.f + zOffset));
            FRotator FlystickOrientation(
                FQuat(
                    -1.0f * (float)trackerData.Quat[1],
                    -1.0f * (float)trackerData.Quat[0],
                    -1.0f * (float)trackerData.Quat[2],
                    (float)trackerData.Quat[3]));
            FlystickOrientation = FRotator(
                FlystickOrientation.Pitch + PitchOffset.GetValueOnGameThread(),
                FlystickOrientation.Yaw + YawOffset.GetValueOnGameThread(),
                FlystickOrientation.Roll + RollOffset.GetValueOnGameThread());
            this->End = FlystickOrientation.RotateVector(FVector(500, 0, 0)) + this->Start;
            this->Start = this->CaveHeadCharacter->NetRot.RotateVector(this->Start) + this->PlayerStartLocation;
            this->End = this->CaveHeadCharacter->NetRot.RotateVector(this->End) + this->PlayerStartLocation;
        });
    vrpnController->AddButtonPressedCallback(
        [this, vrpnController](int32 button, ButtonState state) {
            if (button == 0)
            {
                bPrimaryClick = true;
                PrimaryButtonState = state;
                OnPrimaryButtonStateChanged(state);
            }
            if (button == 4)
                FlystickMode = Manipulate;
            if (button == 3)
                FlystickMode = Navigate;
        });
    vrpnController->AddAnalogChangedCallback(
        [this, vrpnController](VRPNController::AnalogData analogData) {
            this->AnalogX = analogData.Channel[0]; 
            this->AnalogY = analogData.Channel[1]; 
        });
}
void AVRPNFlystickActor :: BeginPlay ( ) { Super :: BeginPlay ( ) ; UE_LOG ( LogCave , Warning , TEXT ( " " ) , HasAuthority ( ) ? TEXT ( " " ) : TEXT ( " " ) ) ; if ( ! GetWorld ( ) -> IsServer ( ) ) return ; auto CaveGameInstance = Cast < UCaveGameInstance > ( GetWorld ( ) -> GetGameInstance ( ) ) ; auto vrpnController = CaveGameInstance -> GetVRPNControllerActor ( " " ) -> GetController ( ) ; FVector EyeOrigin = FVector ( 0 , 0 , 0 ) ; if ( CaveGameInstance -> GetCaveController ( ) != nullptr ) { EyeOrigin = CaveGameInstance -> GetCaveController ( ) -> EyeOrigin ; } if ( vrpnController == nullptr ) { UE_LOG ( LogCave , Warning , TEXT ( " " ) ) ; return ; } auto CaveHead = CaveGameInstance -> GetCaveHeadCharacter ( ) ; if ( IsValid ( CaveHead ) ) this -> PlayerStartLocation = CaveHead -> GetPlayerStartLocation ( ) ; vrpnController -> AddTrackerChangedCallback ( [ this , vrpnController , EyeOrigin ] ( int32 sensor , VRPNController :: TrackerData trackerData ) { if ( sensor != 18 && sensor != 15 ) return ; float xOffset = - EyeOrigin . X ; float yOffset = - EyeOrigin . Y ; float zOffset = - EyeOrigin . Z ; this -> Start = FVector ( ( float ) ( trackerData . Pos [ 1 ] * 100.f + xOffset ) , ( float ) ( trackerData . Pos [ 0 ] * 100.f + yOffset ) , ( float ) ( trackerData . Pos [ 2 ] * 100.f + zOffset ) ) ; FRotator FlystickOrientation ( FQuat ( -1.0f * ( float ) trackerData . Quat [ 1 ] , -1.0f * ( float ) trackerData . Quat [ 0 ] , -1.0f * ( float ) trackerData . Quat [ 2 ] , ( float ) trackerData . Quat [ 3 ] ) ) ; FlystickOrientation = FRotator ( FlystickOrientation . Pitch + PitchOffset . GetValueOnGameThread ( ) , FlystickOrientation . Yaw + YawOffset . GetValueOnGameThread ( ) , FlystickOrientation . Roll + RollOffset . GetValueOnGameThread ( ) ) ; this -> End = FlystickOrientation . RotateVector ( FVector ( 500 , 0 , 0 ) ) + this -> Start ; this -> Start = this -> CaveHeadCharacter -> NetRot . RotateVector ( this -> Start ) + this -> PlayerStartLocation ; this -> End = this -> CaveHeadCharacter -> NetRot . RotateVector ( this -> End ) + this -> PlayerStartLocation ; } ) ; vrpnController -> AddButtonPressedCallback ( [ this , vrpnController ] ( int32 button , ButtonState state ) { if ( button == 0 ) { bPrimaryClick = true ; PrimaryButtonState = state ; OnPrimaryButtonStateChanged ( state ) ; } if ( button == 4 ) FlystickMode = Manipulate ; if ( button == 3 ) FlystickMode = Navigate ; } ) ; vrpnController -> AddAnalogChangedCallback ( [ this , vrpnController ] ( VRPNController :: AnalogData analogData ) { this -> AnalogX = analogData . Channel [ 0 ] ; this -> AnalogY = analogData . Channel [ 1 ] ; } ) ; }
Called when the game starts or when spawned
Called when the game starts or when spawned
// only on server!
 //UE_LOG(LogCave, Warning, TEXT("Tracker ID: %d"), sensor);
 // 18 = Flystick, 15 = WiiTarget aka Flystick substitute
 // UE_LOG(LogCave, Warning, TEXT("PlayerStartLocation: %s"), *this->PlayerStartLocation.ToString());
 // UE_LOG(LogCave, Warning, TEXT("EyeOrigin: %s"), *EyeOrigin.ToString());
 // UE_LOG(LogCave, Warning, TEXT("StartBefore: %s"), *this->Start.ToString());
 // this is exactly our case
 //https://stackoverflow.com/questions/39235842/switch-axes-and-handedness-of-a-quaternion
 // add the player start offset (TODO: or should it be the head position?)
 // UE_LOG(LogCave, Warning, TEXT("StartAfter: %s"), *this->Start.ToString());
 /* WIImote
                 * 0 - home
                 * 1 - 1
                 * 2 - 2
                 * 3 - A
                 * 4 - Trigger
                 * 5 - -
                 * 6 - +
                 * 7 - left
                 * 8 - right
                 * 9 - down
                 * 10- up
                 *
                 * Flystick
                 * 0 - Trigger
                 * 1 - 4/4 Button row
                 * 2 - 3/4 Button row
                 * 3 - 2/4 Button row
                 * 4 - 1/4 Button row
                 * 6 - analog stick press
                 */ //TODO: we got exactly two channels, nonetheless this should be more generic
 // left right
 // up down
