Skip to content
Snippets Groups Projects
Select Git revision
  • 39c6c218ce94f2baca2366b0de561ee9db4d7f49
  • 5.4 default protected
  • 5.5
  • dev/5.5
  • dev/5.4
  • dev/5.3_downgrade
  • feature/experimenttime_hack
  • 5.3 protected
  • _IntenSelect5.3
  • IntenSelect5.3
  • 4.27 protected
  • 4.26 protected
  • 5.0 protected
  • 4.22 protected
  • 4.21 protected
  • UE5.4-2024.1
  • UE5.4-2024.1-rc1
  • UE5.3-2023.1-rc3
  • UE5.3-2023.1-rc2
  • UE5.3-2023.1-rc
20 results

BasicVRInteractionComponent.h

  • user avatar
    Lab1 authored
    remove second default values (in initialize parameters) and scale ray in beginplay (when the values are actually set)
    631538d5
    History
    BasicVRInteractionComponent.h 3.26 KiB
    // Fill out your copyright notice in the Description page of Project Settings.
    
    #pragma once
    
    #include "CoreMinimal.h"
    #include "Components/WidgetInteractionComponent.h"
    #include "BasicVRInteractionComponent.generated.h"
    
    DECLARE_LOG_CATEGORY_EXTERN(LogVRInteractionComponent, Log, All);
    
    class UGrabbingBehaviorComponent;
    
    UENUM()
    enum EInteractionRayVisibility
    {
    	Visible UMETA(DisplayName = "Interaction ray visible"),
    	VisibleOnHoverOnly UMETA(DisplayName = "Interaction ray only visible when hovering over Clickable or Targetable objects, or interactable widgets"),
    	Invisible UMETA(DisplayName = "Interaction ray invisible")
    };
    
    
    UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
    class RWTHVRTOOLKIT_API UBasicVRInteractionComponent : public UWidgetInteractionComponent
    {
    	GENERATED_BODY()
    
    public:	
    	// Sets default values for this component's properties
    	UBasicVRInteractionComponent();
    
    	void BeginPlay() override;
    
    	UFUNCTION(BlueprintCallable) void BeginInteraction(); 
    	UFUNCTION(BlueprintCallable) void EndInteraction();   	
    	
    	// Called every frame
    	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
    
    	UPROPERTY(EditAnywhere,BlueprintReadWrite) float MaxGrabDistance = 50;
    	UPROPERTY(EditAnywhere,BlueprintReadWrite) float MaxClickDistance = 500;
    	// Enable this if you want to interact with Targetable classes or use EInteractionRayVisibility::VisibleOnHoverOnly
    	UPROPERTY(EditAnywhere) bool bCanRaytraceEveryTick = false;
    	UPROPERTY(EditAnywhere) TEnumAsByte<EInteractionRayVisibility> InteractionRayVisibility = EInteractionRayVisibility::Invisible;
    
    	UFUNCTION(BlueprintCallable) void Initialize(USceneComponent* RayEmitter);
    	
    	UFUNCTION(BlueprintCallable, BlueprintPure) AActor* GetGrabbedActor() const { return GrabbedActor;}
    	UFUNCTION(BlueprintCallable, BlueprintPure) USceneComponent* GetInteractionRayEmitter() const { return InteractionRayEmitter;	}
    
    	UFUNCTION(BlueprintCallable) void SetInteractionRayVisibility(EInteractionRayVisibility NewVisibility);
    private:
    	/* Holding a reference to the actor that is currently being grabbed */
    	UPROPERTY() AActor* GrabbedActor;
    	/* Holds a reference to the grabbed actors physics simulating component if there was one*/
    	UPROPERTY() UPrimitiveComponent* ComponentSimulatingPhysics = nullptr;
    	UPROPERTY() UGrabbingBehaviorComponent* Behavior = nullptr;
    	UPROPERTY() USceneComponent* InteractionRayEmitter = nullptr;
    	UPROPERTY() UStaticMeshComponent* InteractionRay = nullptr;
    	
    	/* Stores the reference of the Actor that was hit in the last frame*/
    	UPROPERTY() AActor* LastActorHit = nullptr;
    	void HandlePhysicsAndAttachActor(AActor* HitActor);
    	FTwoVectors GetHandRay(float Length) const;
    	TOptional<FHitResult> RaytraceForFirstHit(const FTwoVectors& Ray) const;
    };
    
    // Free utility functions 
    /*
    	Returns the UPrimitiveComponent simulating physics that is highest in the hierarchy
    */
    UPrimitiveComponent* GetFirstComponentSimulatingPhysics(const AActor* TargetActor);
    /*
    	Recursive Function
    	If parent component simulates physics returns GetHighestParentSimulatingPhysics(Parent)
    	else returns Comp itself
    */
    UPrimitiveComponent* GetHighestParentSimulatingPhysics(UPrimitiveComponent* Comp);