Skip to content
Snippets Groups Projects
Commit da084de8 authored by mmeierkrueger's avatar mmeierkrueger :art:
Browse files

Added the option to move components with the...

Added the option to move components with the GrabbingBehaviorOnPlaneComponent.cpp on the plane relative to the point they were grabbed at (instead of snapping them to the point where the interaction ray intersects the plane). This behaviour can be set with a boolean and required some minor changes in the BasicVRInteractionComponent.cpp
parent 62813009
No related branches found
No related tags found
1 merge request!21Changed the way how the grabbing behaviors work: The behaviours now know from...
......@@ -18,7 +18,7 @@ UGrabbingBehaviorComponent::UGrabbingBehaviorComponent()
void UGrabbingBehaviorComponent::BeginPlay()
{
Super::BeginPlay();
OriginalPosition = GetOwner()->GetActorLocation();
// ...
}
......
......@@ -14,15 +14,15 @@ UGrabbingBehaviorOnPlaneComponent::UGrabbingBehaviorOnPlaneComponent()
}
void UGrabbingBehaviorOnPlaneComponent::SetDistance(float Dist)
void UGrabbingBehaviorOnPlaneComponent::SetMaxDistance(float Dist)
{
check(Dist > 0 && "max distance has to be greater than 0");
this->Distance = Dist;
//check(Dist > 0 && "max distance has to be greater than 0"); //imo not really useful since there are cases where limitations are undesired
this->MaxDistance = Dist;
}
float UGrabbingBehaviorOnPlaneComponent::GetDistance() const
{
return this->Distance;
return this->MaxDistance;
}
......@@ -40,15 +40,19 @@ void UGrabbingBehaviorOnPlaneComponent::HandleNewPositionAndDirection(FVector Po
FVector Intersection = FMath::LinePlaneIntersection(Position, Position + Direction, AttachmentPoint, PlaneNormal);
if(bShouldSnapToRayEnd == true)
{
//DrawDebugSphere(GetWorld(), Intersection, 10, 5, FColor::Purple, false, 10);
//DrawDebugSphere(GetWorld(), Intersection.GetClampedToMaxSize(MaxDistance), 10, 5, FColor::Red, false, 10);
//DrawDebugSphere(GetWorld(), GetComponentLocation(), 15, 5, FColor::Black, false, 10);
FVector NewPosition = -AttachmentPoint + Intersection;
//DrawDebugSphere(GetWorld(), NewPosition, 30, 10, FColor::Green, false, 10);
//DrawDebugSphere(GetWorld(), CurrentPosition, 40, 10, FColor::Magenta, false, 10);
// clamp size by maxDistance
NewPosition = NewPosition.GetClampedToMaxSize(Distance);
//DrawDebugSphere(GetWorld(), NewPosition, 10, 5, FColor::Green, false, 10);
NewPosition = NewPosition.GetClampedToMaxSize(MaxDistance);
//DrawDebugSphere(GetWorld(), NewPosition, 10, 5, FColor::Red, false, 10);
// after this NewPoint is in world position
NewPosition += AttachmentPoint;
//if(NewPosition-)
//DrawDebugSphere(GetWorld(), NewPosition, 10 , 5, FColor::Magenta, false, 10);
// set new position and orientation using calculated quaternion and position
// here rotation is not changed
GetOwner()->SetActorLocation(NewPosition);
......@@ -60,34 +64,73 @@ void UGrabbingBehaviorOnPlaneComponent::HandleNewPositionAndDirection(FVector Po
FVector ImpactPointOnConstraintPlane = FVector::PointPlaneProject(InitialImpactPoint, ConstraintPlane);
if (isInitialInteraction == true)
{
UE_LOG(LogTemp, Warning, TEXT("initial interaction"));
//if the function is called for the first time, the information necessary for all further offset computations are stored
InteractionPlane = FPlane(InitialImpactPoint, PlaneNormal); //create plane that runs parallel to constrain plane and runs through the initial interaction point
OffsetToParentLocation = AttachmentPoint - ImpactPointOnConstraintPlane;
/*DrawDebugDirectionalArrow(GetWorld(), AttachmentPoint, ImpactPointOnConstraintPlane, 100, FColor::Red, false, 200);
DrawDebugSphere(GetWorld(), InitialImpactPoint, 30, 10, FColor::Green, false, 10);
DrawDebugSphere(GetWorld(), ImpactPointOnConstraintPlane, 30, 10, FColor::Red, false, 10);*/
//DrawDebugSolidPlane(GetWorld(), ConstraintPlane, AttachmentPoint, 500, FColor::Black, false, 10);
//DrawDebugSolidPlane(GetWorld(), InteractionPlane, InitialImpactPoint, 500, FColor::Magenta, false, 10);
}
else
{
//get the InteractionRay intersection with the Interaction plane of the constraint
FVector ConstraintPlaneIntersection = FMath::LinePlaneIntersection(Position, Position + Direction, InteractionPlane);
//DrawDebugSphere(GetWorld(), ImpactPointOnConstraintPlane, 30, 10, FColor::Cyan, false, 10);
//Project this point onto the Constraint plane
FVector PointOnConstrainPlane = FVector::PointPlaneProject(ConstraintPlaneIntersection, ConstraintPlane);
//Add offset of the Parent Actor to create the correct position vector
PointOnConstrainPlane += OffsetToParentLocation;
//DrawDebugSphere(GetWorld(), PointOnConstrainPlane, 30, 30, FColor::Red, false, 10);
FVector PointAsVector;
if(MaxDistance >= 0)
{
//TODO clamp this and add to original position
float DistanceFromOrigin = FVector::Dist(PointOnConstrainPlane, OriginalPosition); // PointOnConstrainPlane - OriginalPosition;
PointAsVector = PointOnConstrainPlane - OriginalPosition;
//DrawDebugSphere(GetWorld(), PointAsVector, 30, 5, FColor::Magenta, false, 10);
//DrawDebugSphere(GetWorld(), PointOnConstrainPlane, 30, 5, FColor::Blue, false, 10);
//DrawDebugSphere(GetWorld(), PointOnConstrainPlane.GetClampedToMaxSize(MaxDistance), 30, 30, FColor::Cyan, false, 10);
//UE_LOG(LogTemp, Warning, TEXT("DistanceFromOrigin = %f"), DistanceFromOrigin);
if (DistanceFromOrigin > MaxDistance)
{
//UE_LOG(LogTemp, Error, TEXT("Distance too large"));
//PointOnConstrainPlane = PointOnConstrainPlane.GetClampedToMaxSize(MaxDistance);
//DrawDebugSphere(GetWorld(), PointAsVector, 30, 5, FColor::Green, false, 10);
FVector ClampedVector = PointAsVector.GetClampedToMaxSize(MaxDistance);
FVector OffsetForNewPos = ClampedVector - PointAsVector;
//PointAsVector += GetComponentLocation();
//DrawDebugSphere(GetWorld(), PointAsVector, 30, 5, FColor::Red, false, 10);
GetOwner()->SetActorLocation(PointOnConstrainPlane + OffsetForNewPos);
}
else
{
GetOwner()->SetActorLocation(PointOnConstrainPlane);
//TODO ADD DISTANCE LIMITATIONS
}
}
else
{
//UE_LOG(LogTemp, Warning, TEXT("No Distance Limit found"));
GetOwner()->SetActorLocation(PointOnConstrainPlane);
}
//GetOwner()->SetActorLocation(PointOnConstrainPlane);
this->SetRelativeLocation(GetOwner()->GetActorLocation());
}
}
//UE_LOG(LogTemp, Warning, TEXT("Owner = %s"), *GetOwner()->GetName());
//this->SetWorldLocation(GetOwner()->GetActorLocation());
}
......@@ -95,6 +138,8 @@ void UGrabbingBehaviorOnPlaneComponent::HandleNewPositionAndDirection(FVector Po
void UGrabbingBehaviorOnPlaneComponent::BeginPlay()
{
Super::BeginPlay();
SetWorldLocation(GetOwner()->GetActorLocation(), false);
// ...
......@@ -106,6 +151,7 @@ void UGrabbingBehaviorOnPlaneComponent::TickComponent(float DeltaTime, ELevelTic
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// ...
}
......
......@@ -72,7 +72,6 @@ void UBasicVRInteractionComponent::BeginInteraction()
// save it for later, is needed every tick
Behavior = HitActor->FindComponentByClass<UGrabbingBehaviorComponent>();
//TODO Rename to "InitialImpactPoint"
Behavior->SetInitialImpactPoint(ImpactPoint);
Behavior->SetInitialImpactStatus(true);
if ( Behavior == nullptr)
......
......@@ -32,12 +32,10 @@ public:
}
bool GetInitialImpactStatus()
{
//UE_LOG(LogTemp, Warning, TEXT("Getting initial impact Status"));
return isInitialImpact;
}
void SetInitialImpactStatus(bool ImpactStatus)
{
UE_LOG(LogTemp, Warning, TEXT("Setting initial impact Status"));
isInitialImpact = ImpactStatus;
}
......@@ -52,6 +50,7 @@ protected:
UPROPERTY() bool bShouldSnapToRayEnd = true;
UPROPERTY() FVector InitialImpactPoint;
UPROPERTY() bool isInitialImpact = true;
UPROPERTY() FVector OriginalPosition; //used to enforce distance limitations
......
......@@ -17,7 +17,7 @@ public:
UGrabbingBehaviorOnPlaneComponent();
// defining the constraint plane with these 3 parameters
UFUNCTION(BlueprintCallable) void SetDistance(float Dist);
UFUNCTION(BlueprintCallable) void SetMaxDistance(float Dist);
UFUNCTION(BlueprintCallable) float GetDistance() const;
virtual void HandleNewPositionAndDirection(FVector position, FQuat orientation, bool isInitialInteraction) override;
......@@ -32,10 +32,11 @@ public:
private:
UPROPERTY(EditAnywhere) float Distance; // distance the object can be moved from the center
UPROPERTY(EditAnywhere) float MaxDistance = -1; // distance the object can be moved from the center. -1 for no limit
UPROPERTY() FPlane ConstraintPlane; //the plane on which the parent actor is allowed to move
UPROPERTY() FPlane InteractionPlane; // the same plane as the ConstraintPlane, but shifted onto the intersection point of the parent object and the interaction ray
UPROPERTY() FVector OffsetToParentLocation;
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment