Skip to content
Snippets Groups Projects
Commit 5a6c3266 authored by Kamil Karwacki's avatar Kamil Karwacki
Browse files

everything

parent 6a431c1c
No related branches found
No related tags found
No related merge requests found
......@@ -10,8 +10,6 @@ UGrabbingBehaviorOnLineComponent::UGrabbingBehaviorOnLineComponent()
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
SetAbsolute(true, true, true);
this->Distance = 0;
}
......@@ -23,13 +21,23 @@ void UGrabbingBehaviorOnLineComponent::SetDistance(float Dist)
this->Distance = Dist;
}
float UGrabbingBehaviorOnLineComponent::GetDistance() const
{
return this->Distance;
}
void UGrabbingBehaviorOnLineComponent::SetDiscreteNumberOfPoints(int Num)
{
NumPoints = Num;
bIsDiscrete = true;
}
void UGrabbingBehaviorOnLineComponent::HandleNewPositionAndDirection(FVector Position, FQuat Orientation)
{
FVector AttachmentPoint = this->GetRelativeLocation();
FVector ConstraintAxis = this->GetComponentQuat().GetUpVector();
FVector Direction = Orientation.GetForwardVector();
......@@ -49,6 +57,19 @@ void UGrabbingBehaviorOnLineComponent::HandleNewPositionAndDirection(FVector Pos
FVector NewPosition = FVector::DotProduct(FromOriginToIntersection, ConstraintAxis) * ConstraintAxis;
NewPosition = NewPosition.GetClampedToMaxSize(Distance);
if (bIsDiscrete)
{
float lengthOfSegment = 1.f / static_cast<float>(NumPoints + 1.f);
FVector LineBeginning = -ConstraintAxis * Distance;
float LengthOnLine = (FVector::DotProduct(FromOriginToIntersection, ConstraintAxis) / Distance + 1.f) / 2.f; // is between 0 and 1
float VectorSize = FMath::CeilToFloat(LengthOnLine / lengthOfSegment);
if (VectorSize <= 0) VectorSize = 1;
if (VectorSize > NumPoints) VectorSize = NumPoints;
NewPosition = LineBeginning + VectorSize * ConstraintAxis * lengthOfSegment * Distance * 2.f;
}
NewPosition += AttachmentPoint;
// transform the targeted actor which is owner of this component with calculated quaternion and posiition
......@@ -56,6 +77,7 @@ void UGrabbingBehaviorOnLineComponent::HandleNewPositionAndDirection(FVector Pos
GetOwner()->SetActorLocation(NewPosition);
}
// Called when the game starts
void UGrabbingBehaviorOnLineComponent::BeginPlay()
{
......
// Fill out your copyright notice in the Description page of Project Settings.
#include "Interaction/Targetable.h"
UTargetable::UTargetable(const FObjectInitializer& ObjectInitializer)
:Super(ObjectInitializer)
{}
......@@ -30,21 +30,20 @@ void UBasicVRInteractionComponent::BeginInteraction()
// will be filled by the Line Trace Function
FHitResult Hit;
//if hit was not found return
const FCollisionObjectQueryParams Params;
if (!GetWorld()->LineTraceSingleByObjectType(Hit, Start, End, Params))
FCollisionQueryParams Params2;
Params2.AddIgnoredActor(GetOwner()->GetUniqueID()); // prevents actor hitting itself
//if hit was not found return
if (!GetWorld()->LineTraceSingleByObjectType(Hit, Start, End, Params, Params2))
return;
AActor* HitActor = Hit.GetActor();
// try to cast HitActor into a Grabable if not succeeded will become a nullptr
IGrabable* GrabableActor = Cast<IGrabable>(HitActor);
IClickable* ClickableActor = Cast<IClickable>(HitActor);
if (GrabableActor != nullptr && Hit.Distance < MaxGrabDistance)
if (HitActor->Implements<UGrabable>() && Hit.Distance < MaxGrabDistance)
{
// call grabable actors function so he reacts to our grab
GrabableActor->OnGrabbed_Implementation();
IGrabable::Execute_OnBeginGrab(HitActor);
// save it for later, is needed every tick
Behavior = HitActor->FindComponentByClass<UGrabbingBehaviorComponent>();
......@@ -54,9 +53,9 @@ void UBasicVRInteractionComponent::BeginInteraction()
// we save the grabbedActor in a general form to access all of AActors functions easily later
GrabbedActor = HitActor;
}
else if (ClickableActor != nullptr && Hit.Distance < MaxClickDistance)
else if (HitActor->Implements<UClickable>() && Hit.Distance < MaxClickDistance)
{
ClickableActor->OnClicked_Implementation(Hit.Location);
IClickable::Execute_OnClick(HitActor, Hit.Location);
}
}
......
......@@ -22,8 +22,6 @@ class IClickable
public:
// function that will be called when clickable actor got clicked, and passed the world pos of the click
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = Gameplay)
void OnClicked(FVector WorldPositionOfClick);
void OnClick(FVector WorldPositionOfClick);
};
......@@ -21,10 +21,9 @@ class IGrabable
public:
// function that will be called when grabbed by a pawn
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = Gameplay)
void OnGrabbed();
void OnBeginGrab();
// called when pawn released the object
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = Gameplay)
void OnReleased();
void OnEndGrab();
};
......@@ -19,6 +19,7 @@ public:
// defining a constraint line with these 3 parameters
UFUNCTION(BlueprintCallable) void SetDistance(float Dist);
UFUNCTION(BlueprintCallable) float GetDistance() const;
UFUNCTION(BlueprintCallable) void SetDiscreteNumberOfPoints(int Num);
virtual void HandleNewPositionAndDirection(FVector position, FQuat orientation) override;
......@@ -30,8 +31,8 @@ public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
private:
UPROPERTY(EditAnywhere) float Distance; // distance the object can be moved from the center
UPROPERTY(EditAnywhere) float Distance = 100; // distance the object can be moved from the center
UPROPERTY(EditAnywhere) bool bIsDiscrete = false;
UPROPERTY(EditAnywhere) int NumPoints = 1;
};
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "UObject/Object.h"
#include "UObject/Interface.h"
#include "Targetable.generated.h"
UINTERFACE(BlueprintType)
class DISPLAYCLUSTEREXTENSIONS_API UTargetable: public UInterface
{
// has to be empty, this is Unreals syntax to make it visible in blueprints
GENERATED_UINTERFACE_BODY()
};
class ITargetable
{
GENERATED_IINTERFACE_BODY()
public:
// function that will be called when clickable actor got clicked, and passed the world pos of the click
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = Gameplay)
void OnTargeted(FVector WorldPositionOfTarget);
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment