Skip to content
Snippets Groups Projects
Commit 6a3264ef authored by Timon Römer's avatar Timon Römer
Browse files

Refactors and improves readability

parent edb97e03
Branches
No related tags found
1 merge request!88Merges IntenSelect 5.3 into dev 5.3
Showing with 83 additions and 186 deletions
......@@ -56,7 +56,7 @@ void UIntenSelectable::BeginPlay()
}
}
void UIntenSelectable::HandleOnSelectStartEvents(const UIntenSelectComponent* IntenSelect, const FHitResult& HitResult)
void UIntenSelectable::HandleOnHoverStartEvents(const UIntenSelectComponent* IntenSelect, const FHitResult& HitResult)
{
for (const UHoverBehaviour* CurrentHoverBehaviour : OnHoverBehaviours)
{
......@@ -64,7 +64,7 @@ void UIntenSelectable::HandleOnSelectStartEvents(const UIntenSelectComponent* In
}
}
void UIntenSelectable::HandleOnSelectEndEvents(const UIntenSelectComponent* IntenSelect)
void UIntenSelectable::HandleOnHoverEndEvents(const UIntenSelectComponent* IntenSelect)
{
for (const UHoverBehaviour* CurrentHoverBehaviour : OnHoverBehaviours)
{
......@@ -72,7 +72,7 @@ void UIntenSelectable::HandleOnSelectEndEvents(const UIntenSelectComponent* Inte
}
}
void UIntenSelectable::HandleOnClickStartEvents(UIntenSelectComponent* IntenSelect)
void UIntenSelectable::HandleOnActionStartEvents(UIntenSelectComponent* IntenSelect)
{
for (const UActionBehaviour* CurrentActionBehaviour : OnActionBehaviours)
{
......@@ -82,7 +82,7 @@ void UIntenSelectable::HandleOnClickStartEvents(UIntenSelectComponent* IntenSele
}
}
void UIntenSelectable::HandleOnClickEndEvents(UIntenSelectComponent* IntenSelect, FInputActionValue& InputValue)
void UIntenSelectable::HandleOnActionEndEvents(UIntenSelectComponent* IntenSelect, FInputActionValue& InputValue)
{
for (const UActionBehaviour* CurrentActionBehaviour : OnActionBehaviours)
{
......
// Fill out your copyright notice in the Description page of Project Settings.
#include "Interaction/Interactables/IntenSelect/IntenSelectableCircleScoring.h"
#include "DrawDebugHelpers.h"
#include "Kismet/KismetMathLibrary.h"
......@@ -26,15 +23,17 @@ FVector UIntenSelectableCircleScoring::GetClosestSelectionPointTo(const FVector&
const FVector CircleNormalWorld =
this->GetComponentTransform().TransformPositionNoScale(FVector::ForwardVector) - CenterWorld;
float t;
FVector Intersect;
if (!UKismetMathLibrary::LinePlaneIntersection_OriginNormal(Point, Point + Direction * 100000, CenterWorld,
CircleNormalWorld, t, Intersect))
float IntersectionRatio;
FVector IntersectionPoint;
constexpr float MaxDistance = 100000;
if (!UKismetMathLibrary::LinePlaneIntersection_OriginNormal(Point, Point + Direction * MaxDistance, CenterWorld,
CircleNormalWorld, IntersectionRatio,
IntersectionPoint))
{
return CenterWorld;
}
const FVector CenterToPoint = Intersect - CenterWorld;
const FVector CenterToPoint = IntersectionPoint - CenterWorld;
FVector Result;
if (OnlyOutline)
......@@ -51,15 +50,13 @@ FVector UIntenSelectableCircleScoring::GetClosestSelectionPointTo(const FVector&
}
else
{
Result = Intersect;
Result = IntersectionPoint;
}
}
FVector Y = CenterToPoint.GetSafeNormal();
FVector Z = FVector::CrossProduct(Y, CircleNormalWorld.GetSafeNormal());
// Y = FVector(0, 0, 1);
// Z = FVector(1, 0, 0);
DrawDebugCircle(GetWorld(), CenterWorld, Radius, 80, FColor::Green, false, -1, 0, 1, Y, Z, false);
return Result;
......
// Fill out your copyright notice in the Description page of Project Settings.
#include "Interaction/Interactables/IntenSelect/IntenSelectableCubeScoring.h"
#include "DrawDebugHelpers.h"
#include "Intersection/IntrRay3AxisAlignedBox3.h"
#include "Kismet/KismetMathLibrary.h"
// Sets default values for this component's properties
......@@ -18,7 +14,7 @@ UIntenSelectableCubeScoring::GetBestPointScorePair(const FVector& ConeOrigin, co
const float ConeBackwardShiftDistance, const float ConeAngle,
const float LastValue, const float DeltaTime)
{
FVector Point = GetClosestSelectionPointTo(ConeOrigin, ConeForwardDirection);
const FVector Point = GetClosestSelectionPointTo(ConeOrigin, ConeForwardDirection);
float Score = Super::GetScore(ConeOrigin, ConeForwardDirection, ConeBackwardShiftDistance, ConeAngle, Point,
LastValue, DeltaTime);
FHitResult Result = FHitResult{GetOwner(), nullptr, Point, FVector::ForwardVector};
......@@ -27,10 +23,10 @@ UIntenSelectableCubeScoring::GetBestPointScorePair(const FVector& ConeOrigin, co
FVector UIntenSelectableCubeScoring::GetClosestPointToRectangle(const FVector& StartPoint, const FVector& Direction,
const FVector& Corner00, const FVector& Corner01,
const FVector& Corner10, const FVector& Corner11) const
const FVector& Corner10) const
{
const float X = FVector::Distance(Corner00, Corner10);
const float Y = FVector::Distance(Corner00, Corner01);
const float LengthX = FVector::Distance(Corner00, Corner10);
const float LengthY = FVector::Distance(Corner00, Corner01);
const FVector PlaneNormal = FVector::CrossProduct(Corner10 - Corner00, Corner01 - Corner00).GetSafeNormal();
FVector Intersection;
......@@ -40,87 +36,9 @@ FVector UIntenSelectableCubeScoring::GetClosestPointToRectangle(const FVector& S
FVector LocalIntersection = this->GetComponentTransform().InverseTransformPosition(Intersection);
if (LocalIntersection.Y > X / 2)
{
LocalIntersection.Y = X / 2;
}
else if (LocalIntersection.Y < -X / 2)
{
LocalIntersection.Y = -X / 2;
}
LocalIntersection.Y = FMath::Clamp(LocalIntersection.Y, -LengthX / 2, LengthX / 2);
LocalIntersection.Z = FMath::Clamp(LocalIntersection.Z, -LengthY / 2, LengthY / 2);
if (LocalIntersection.Z > Y / 2)
{
LocalIntersection.Z = Y / 2;
}
else if (LocalIntersection.Z < -Y / 2)
{
LocalIntersection.Z = -Y / 2;
}
/*
if(OnlyOutline)
{
const float DistToBottom = LocalIntersection.Z + (YLength / 2);
const float DistToLeft = LocalIntersection.Y + (XLength / 2);
if(LocalIntersection.Z < 0)
{
if(LocalIntersection.Y < 0)
{
//Bottom and left
if(DistToLeft < DistToBottom)
{
//snap left
LocalIntersection.Y = -(XLength / 2);
}else
{
//snap bottom
LocalIntersection.Z = -(YLength / 2);
}
}else
{
//bottom and right
if(XLength - DistToLeft < DistToBottom)
{
//snap right
LocalIntersection.Y = XLength / 2;
}else
{
//snap bottom
LocalIntersection.Z = -(YLength / 2);
}
}
}else
{
if(LocalIntersection.Y < 0)
{
//top and left
if(DistToLeft < YLength - DistToBottom)
{
//snap left
LocalIntersection.Y = -(XLength / 2);
}else
{
//snap top
LocalIntersection.Z = (YLength / 2);
}
}else
{
//top and right
if(XLength - DistToLeft < YLength - DistToBottom)
{
//snap right
LocalIntersection.Y = XLength / 2;
}else
{
//snap top
LocalIntersection.Z = (YLength / 2);
}
}
}
}
*/
return this->GetComponentTransform().TransformPosition(LocalIntersection);
}
......@@ -128,65 +46,61 @@ FVector UIntenSelectableCubeScoring::GetClosestPointToRectangle(const FVector& S
bool UIntenSelectableCubeScoring::LineToLineIntersection(const FVector& FromA, const FVector& FromB, const FVector& ToA,
const FVector& ToB, FVector& OutIntersection)
{
const FVector Da = ToA - FromA;
const FVector DB = ToB - FromB;
const FVector DC = FromB - FromA;
const FVector DistanceA = ToA - FromA;
const FVector DistanceB = ToB - FromB;
const FVector DistanceC = FromB - FromA;
const FVector CrossDaDb = FVector::CrossProduct(Da, DB);
const float Prod = CrossDaDb.X * CrossDaDb.X + CrossDaDb.Y * CrossDaDb.Y + CrossDaDb.Z * CrossDaDb.Z;
const FVector CrossProductAB = FVector::CrossProduct(DistanceA, DistanceB);
const float Prod =
CrossProductAB.X * CrossProductAB.X + CrossProductAB.Y * CrossProductAB.Y + CrossProductAB.Z * CrossProductAB.Z;
const float Res = FVector::DotProduct(FVector::CrossProduct(DC, DB), FVector::CrossProduct(Da, DB) / Prod);
if (Res >= -0.02f && Res <= 1.02f)
const float Result = FVector::DotProduct(FVector::CrossProduct(DistanceC, DistanceB),
FVector::CrossProduct(DistanceA, DistanceB) / Prod);
constexpr float IntersectionThreshold = 0.02f;
if (Result >= -IntersectionThreshold && Result <= 1 + IntersectionThreshold)
{
OutIntersection = FromA + Da * FVector(Res, Res, Res);
OutIntersection = FromA + DistanceA * FVector(Result, Result, Result);
return true;
}
return false;
}
FVector UIntenSelectableCubeScoring::GetClosestSelectionPointTo(const FVector& RayOrigin, const FVector& RayDirection)
FVector UIntenSelectableCubeScoring::GetClosestSelectionPointTo(const FVector& RayOrigin,
const FVector& RayDirection) const
{
auto Scale = GetRelativeTransform().GetScale3D();
const FVector X = this->GetForwardVector() * Scale.X;
const FVector Y = this->GetRightVector() * Scale.Y;
const FVector Z = this->GetUpVector() * Scale.Z;
const FVector ForwardVectorScaled = this->GetForwardVector() * Scale.X;
const FVector RightVectorScaled = this->GetRightVector() * Scale.Y;
const FVector UpVectorScaled = this->GetUpVector() * Scale.Z;
TArray<FPlane> CubeSides;
// bottom
const FVector BottomWorld = this->GetComponentTransform().TransformPositionNoScale(-Z / 2);
const FVector BottomWorld = this->GetComponentTransform().TransformPositionNoScale(-UpVectorScaled / 2);
CubeSides.Add(FPlane{BottomWorld, -this->GetUpVector()});
// top
const FVector TopWorld = this->GetComponentTransform().TransformPositionNoScale(Z / 2);
const FVector TopWorld = this->GetComponentTransform().TransformPositionNoScale(UpVectorScaled / 2);
CubeSides.Add(FPlane{TopWorld, this->GetUpVector()});
// left
const FVector LeftWorld = this->GetComponentTransform().TransformPositionNoScale(-Y / 2);
const FVector LeftWorld = this->GetComponentTransform().TransformPositionNoScale(-RightVectorScaled / 2);
CubeSides.Add(FPlane{LeftWorld, -this->GetRightVector()});
// right
const FVector RightWorld = this->GetComponentTransform().TransformPositionNoScale(Y / 2);
const FVector RightWorld = this->GetComponentTransform().TransformPositionNoScale(RightVectorScaled / 2);
CubeSides.Add(FPlane{RightWorld, this->GetRightVector()});
// front
const FVector FrontWorld = this->GetComponentTransform().TransformPositionNoScale(-X / 2);
const FVector FrontWorld = this->GetComponentTransform().TransformPositionNoScale(-ForwardVectorScaled / 2);
CubeSides.Add(FPlane{FrontWorld, -this->GetForwardVector()});
// back
const FVector BackWorld = this->GetComponentTransform().TransformPositionNoScale(X / 2);
const FVector BackWorld = this->GetComponentTransform().TransformPositionNoScale(ForwardVectorScaled / 2);
CubeSides.Add(FPlane{BackWorld, this->GetForwardVector()});
/*
const TRay3<float> Ray{Point, Direction, false};
const TAxisAlignedBox3<float> Box;
float OutT;
if(TIntrRay3AxisAlignedBox3<float>::FindIntersection(Ray, Box, OutT))
{
}*/
float MinDistance = TNumericLimits<float>::Max();
FVector ClosestPoint = GetComponentLocation();
bool IsSet = false;
......@@ -304,9 +218,6 @@ FVector UIntenSelectableCubeScoring::GetClosestSelectionPointTo(const FVector& R
const float Distance = FMath::PointDistToLine(CurrentPoint, RayDirection, RayOrigin);
// DrawDebugPoint(GetWorld(), CurrentPoint, 10, FColor::Black.WithAlpha(1), false, -1, 0);
// GEngine->AddOnScreenDebugMessage(INDEX_NONE, -1, FColor::Red, FString::SanitizeFloat(Distance));
if (Distance < 0.001)
{
if (MinDistance < 0.001)
......
......@@ -564,7 +564,7 @@ void UIntenSelectComponent::OnFireDown()
if (CurrentSelection)
{
const FHitResult GrabbedPoint = *ContactPointMap.Find(CurrentSelection);
CurrentSelection->HandleOnClickStartEvents(this);
CurrentSelection->HandleOnActionStartEvents(this);
LastKnownSelection = CurrentSelection;
LastKnownGrabPoint =
LastKnownSelection->GetOwner()->GetRootComponent()->GetComponentTransform().InverseTransformPosition(
......@@ -593,7 +593,7 @@ void UIntenSelectComponent::OnFireUp()
if (LastKnownSelection)
{
FInputActionValue v;
LastKnownSelection->HandleOnClickEndEvents(this, v);
LastKnownSelection->HandleOnActionEndEvents(this, v);
}
}
......@@ -664,14 +664,14 @@ void UIntenSelectComponent::HandleActorSelected(UIntenSelectable* NewSelection)
{
if (CurrentSelection)
{
CurrentSelection->HandleOnSelectEndEvents(this);
CurrentSelection->HandleOnHoverEndEvents(this);
}
if (NewSelection)
{
UIntenSelectable* NewIntenSelectable = NewSelection;
const FHitResult GrabbedPoint = *ContactPointMap.Find(NewIntenSelectable);
NewIntenSelectable->HandleOnSelectStartEvents(this, GrabbedPoint);
NewIntenSelectable->HandleOnHoverStartEvents(this, GrabbedPoint);
}
CurrentSelection = NewSelection;
......@@ -709,7 +709,7 @@ void UIntenSelectComponent::HandleNoActorSelected()
if (CurrentSelection)
{
CurrentSelection->HandleOnSelectEndEvents(this);
CurrentSelection->HandleOnHoverEndEvents(this);
}
if (bDrawForwardRay && ParameterCollectionInstance)
......
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
......@@ -9,7 +7,6 @@
#include "Interaction/Interactables/HoverBehaviour.h"
#include "IntenSelectable.generated.h"
class UIntenSelectableScoring;
class UClickBehaviour;
class USelectionBehaviour;
......@@ -41,10 +38,10 @@ public:
const float ConeBackwardShiftDistance, const float ConeAngle,
const float LastValue, const float DeltaTime) const;
void HandleOnSelectStartEvents(const UIntenSelectComponent* IntenSelect, const FHitResult& HitResult);
void HandleOnSelectEndEvents(const UIntenSelectComponent* IntenSelect);
void HandleOnClickStartEvents(UIntenSelectComponent* IntenSelect);
void HandleOnClickEndEvents(UIntenSelectComponent* IntenSelect, FInputActionValue& InputValue);
void HandleOnHoverStartEvents(const UIntenSelectComponent* IntenSelect, const FHitResult& HitResult);
void HandleOnHoverEndEvents(const UIntenSelectComponent* IntenSelect);
void HandleOnActionStartEvents(UIntenSelectComponent* IntenSelect);
void HandleOnActionEndEvents(UIntenSelectComponent* IntenSelect, FInputActionValue& InputValue);
void InitDefaultBehaviourReferences();
......
......@@ -13,7 +13,7 @@ protected:
static bool LineToLineIntersection(const FVector& FromA, const FVector& FromB, const FVector& ToA,
const FVector& ToB, FVector& OutIntersection);
FVector GetClosestSelectionPointTo(const FVector& RayOrigin, const FVector& RayDirection);
FVector GetClosestSelectionPointTo(const FVector& RayOrigin, const FVector& RayDirection) const;
virtual void TickComponent(float DeltaTime, ELevelTick TickType,
FActorComponentTickFunction* ThisTickFunction) override;
......@@ -30,20 +30,11 @@ public:
UPROPERTY(EditAnywhere)
bool OnlyOutline = false;
// UPROPERTY(EditAnywhere)
// float XLength = 100;
// UPROPERTY(EditAnywhere)
// float YLength = 100;
// UPROPERTY(EditAnywhere)
// float ZLength = 100;
virtual TPair<FHitResult, float> GetBestPointScorePair(const FVector& ConeOrigin,
const FVector& ConeForwardDirection,
const float ConeBackwardShiftDistance, const float ConeAngle,
const float LastValue, const float DeltaTime) override;
FVector GetClosestPointToRectangle(const FVector& StartPoint, const FVector& Direction, const FVector& Corner00,
const FVector& Corner01, const FVector& Corner10, const FVector& Corner11) const;
const FVector& Corner01, const FVector& Corner10) const;
};
......@@ -19,16 +19,21 @@ public:
UPROPERTY(BlueprintReadOnly)
float CurrentScore = 0;
UPROPERTY(EditAnywhere)
bool IsSelectable = true;
UPROPERTY(EditAnywhere)
float Stickiness = 10;
UPROPERTY(EditAnywhere)
float Snappiness = 15;
UPROPERTY(EditAnywhere)
float CompensationConstant = 0.8;
bool bOverwritingContrib = false;
float Contrib = 0;
virtual TPair<FHitResult, float> GetBestPointScorePair(const FVector& ConeOrigin,
......
......@@ -13,7 +13,9 @@ FIntenSelectableCircleScoringVisualizer::FIntenSelectableCircleScoringVisualizer
GET_MEMBER_NAME_CHECKED(UIntenSelectableCircleScoring, Radius));
}
FIntenSelectableCircleScoringVisualizer::~FIntenSelectableCircleScoringVisualizer() {}
FIntenSelectableCircleScoringVisualizer::~FIntenSelectableCircleScoringVisualizer()
{
}
FVector FIntenSelectableCircleScoringVisualizer::GetCurrentVectorWorld() const
{
......@@ -61,8 +63,6 @@ bool FIntenSelectableCircleScoringVisualizer::VisProxyHandleClick(FEditorViewpor
{
bool bEditing = false;
// UE_LOG(LogTemp, Warning, TEXT("Handling Click"));
if (VisProxy && VisProxy->Component.IsValid())
{
bEditing = true;
......@@ -115,7 +115,9 @@ void FIntenSelectableCircleScoringVisualizer::DrawVisualization(const UActorComp
}
}
void FIntenSelectableCircleScoringVisualizer::EndEditing() {}
void FIntenSelectableCircleScoringVisualizer::EndEditing()
{
}
UActorComponent* FIntenSelectableCircleScoringVisualizer::GetEditedComponent() const
{
......@@ -130,8 +132,6 @@ bool FIntenSelectableCircleScoringVisualizer::HandleInputDelta(FEditorViewportCl
if (CurrentSelectionIndex != INDEX_NONE)
{
// UE_LOG(LogTemp, Warning, TEXT("Current Selection! %s"), *DeltaTranslate.ToString());
if (CurrentSelectionIndex == 0)
{
const FVector LocalCenter = GetEditedScoringComponent()->GetComponentLocation();
......@@ -162,10 +162,6 @@ bool FIntenSelectableCircleScoringVisualizer::HandleInputDelta(FEditorViewportCl
Properties.Add(PointsProperty);
NotifyPropertiesModified(GetEditedScoringComponent(), Properties, EPropertyChangeType::ValueSet);
}
else
{
// UE_LOG(LogTemp, Warning, TEXT("No Current Selection!"));
}
return bHandled;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment