cancel
Showing results for 
Search instead for 
Did you mean: 

Please provide C++ examples for the Meta XR Meta XR Interaction

jsbmg
Honored Guest

Is C++ supported for these plugins? There is no documentation on it, and I get errors when I try and use the components in C++ Pawn classes.

Are these plugins only meant to be used with blueprints?

1 REPLY 1

4vit
Explorer

At least what I see is the Meta XR plugins are primarily designed for Blueprint and C# (Unity) development).  Here is an example of using in C++ and you can check it out.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MetaXRInteractionComponent.h"
#include "MetaXRGrabbableActor.generated.h"

UCLASS()
class YOURPROJECT_API AMetaXRGrabbableActor : public AActor
{
    GENERATED_BODY()
    
public:    
    AMetaXRGrabbableActor();

protected:
    virtual void BeginPlay() override;
    virtual void Tick(float DeltaTime) override;

    UPROPERTY(VisibleAnywhere)
    UStaticMeshComponent* MeshComponent;

    UPROPERTY(VisibleAnywhere)
    UMetaXRInteractionComponent* InteractionComponent;

    UFUNCTION()
    void OnGrabBegin(EControllerHand Hand);

    UFUNCTION()
    void OnGrabEnd(EControllerHand Hand);

private:
    bool bIsGrabbed;
    EControllerHand GrabbingHand;
    FTransform OriginalTransform;
    FVector GrabOffset;
};

// MetaXRGrabbableActor.cpp
#include "MetaXRGrabbableActor.h"

AMetaXRGrabbableActor::AMetaXRGrabbableActor()
{
    PrimaryActorTick.bCanEverTick = true;

    // Create and setup the mesh component
    MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
    RootComponent = MeshComponent;
    MeshComponent->SetSimulatePhysics(true);

    // Create and setup the interaction component
    InteractionComponent = CreateDefaultSubobject<UMetaXRInteractionComponent>(TEXT("InteractionComponent"));

    bIsGrabbed = false;
}

void AMetaXRGrabbableActor::BeginPlay()
{
    Super::BeginPlay();
    
    // Bind to interaction events
    InteractionComponent->OnHandGrabBegin.AddDynamic(this, &AMetaXRGrabbableActor::OnGrabBegin);
    InteractionComponent->OnHandGrabEnd.AddDynamic(this, &AMetaXRGrabbableActor::OnGrabEnd);
}

void AMetaXRGrabbableActor::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    if (bIsGrabbed)
    {
        // Update position based on hand movement
        FTransform HandTransform = InteractionComponent->GetHandTransform(GrabbingHand);
        FTransform NewTransform = HandTransform;
        NewTransform.AddToTranslation(GrabOffset);
        SetActorTransform(NewTransform);
    }
}

void AMetaXRGrabbableActor::OnGrabBegin(EControllerHand Hand)
{
    if (!bIsGrabbed)
    {
        bIsGrabbed = true;
        GrabbingHand = Hand;
        OriginalTransform = GetActorTransform();
        
        // Calculate grab offset
        FTransform HandTransform = InteractionComponent->GetHandTransform(Hand);
        GrabOffset = GetActorLocation() - HandTransform.GetLocation();

        // Disable physics while grabbed
        MeshComponent->SetSimulatePhysics(false);
    }
}

void AMetaXRGrabbableActor::OnGrabEnd(EControllerHand Hand)
{
    if (bIsGrabbed && Hand == GrabbingHand)
    {
        bIsGrabbed = false;
        
        // Re-enable physics
        MeshComponent->SetSimulatePhysics(true);
        
        // Apply velocity based on hand movement if desired
        // Add implementation here
    }
}