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

better physics handling, OnPlane constraint adjusted

parent bd852c59
Branches
No related tags found
1 merge request!36Feature/interaction additions
......@@ -37,7 +37,6 @@ void UGrabbingBehaviorOnLineComponent::SetDiscreteNumberOfPoints(int Num)
void UGrabbingBehaviorOnLineComponent::HandleNewPositionAndDirection(FVector Position, FQuat Orientation)
{
FVector AttachmentPoint = this->GetRelativeLocation();
FVector ConstraintAxis = this->GetComponentQuat().GetUpVector();
FVector Direction = Orientation.GetForwardVector();
......
......@@ -9,9 +9,6 @@ UGrabbingBehaviorOnPlaneComponent::UGrabbingBehaviorOnPlaneComponent()
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
SetAbsolute(true, true, true);
// ...
}
......@@ -43,7 +40,6 @@ void UGrabbingBehaviorOnPlaneComponent::HandleNewPositionAndDirection(FVector Po
// after this NewPoint is in world position
NewPosition += AttachmentPoint;
// set new position and orientation using calculated quaternion and position
// here rotation is not changed
GetOwner()->SetActorLocation(NewPosition);
......
......@@ -52,7 +52,6 @@ void UBasicVRInteractionComponent::BeginInteraction()
}
}
void UBasicVRInteractionComponent::EndInteraction()
{
if(!InteractionRayEmitter) return;
......@@ -67,14 +66,20 @@ void UBasicVRInteractionComponent::EndInteraction()
// Detach the Actor
if (GrabbedActor->FindComponentByClass<UGrabbingBehaviorComponent>() == nullptr)
{
UPrimitiveComponent* PhysicsComp = GrabbedActor->FindComponentByClass<UPrimitiveComponent>();
if (bDidSimulatePhysics) {
UPrimitiveComponent* PhysicsComp = GetFirstComponentSimulatingPhysics(GrabbedActor);
PhysicsComp->DetachFromComponent(FDetachmentTransformRules::KeepWorldTransform);
GrabbedActor->FindComponentByClass<UPrimitiveComponent>()->SetSimulatePhysics(bDidSimulatePhysics); // do components higher up in the hierarchy move?
PhysicsComp->SetSimulatePhysics(true);
}
else {
GrabbedActor->GetRootComponent()->DetachFromComponent(FDetachmentTransformRules::KeepWorldTransform);
}
}
// forget about the actor
GrabbedActor = nullptr;
Behavior = nullptr;
bDidSimulatePhysics = false;
}
// Called every frame
......@@ -122,12 +127,18 @@ void UBasicVRInteractionComponent::Initialize(USceneComponent* RayEmitter, float
void UBasicVRInteractionComponent::HandlePhysicsAndAttachActor(AActor* HitActor)
{
UPrimitiveComponent* PhysicsComp = HitActor->FindComponentByClass<UPrimitiveComponent>();
UPrimitiveComponent* PhysicsSimulatingComp = GetFirstComponentSimulatingPhysics(HitActor);
const FAttachmentTransformRules Rules = FAttachmentTransformRules(EAttachmentRule::KeepWorld, false);
bDidSimulatePhysics = PhysicsComp->IsSimulatingPhysics(); // remember if we need to tun physics back on or not
PhysicsComp->SetSimulatePhysics(false);
FAttachmentTransformRules Rules = FAttachmentTransformRules(EAttachmentRule::KeepWorld, false);
PhysicsComp->AttachToComponent(InteractionRayEmitter, Rules);
if (PhysicsSimulatingComp) {
bDidSimulatePhysics = true; // remember if we need to tun physics back on or not
PhysicsSimulatingComp->SetSimulatePhysics(false);
PhysicsSimulatingComp->AttachToComponent(InteractionRayEmitter, Rules);
}
else {
bDidSimulatePhysics = false;
HitActor->GetRootComponent()->AttachToComponent(InteractionRayEmitter, Rules);
}
}
FTwoVectors UBasicVRInteractionComponent::GetHandRay(const float Length) const
......@@ -152,10 +163,23 @@ TOptional<FHitResult> UBasicVRInteractionComponent::RaytraceForFirstHit(const FT
const FCollisionObjectQueryParams Params;
FCollisionQueryParams Params2;
Params2.AddIgnoredActor(GetOwner()->GetUniqueID()); // prevents actor hitting itself
//if (!GetWorld()->LineTraceSingleByObjectType(Hit, Start, End, Params, Params2))
if(GetWorld()->LineTraceSingleByChannel(Hit, Start, End, ECollisionChannel::ECC_Visibility, Params2))
if (GetWorld()->LineTraceSingleByObjectType(Hit, Start, End, Params, Params2))
//if(GetWorld()->LineTraceSingleByChannel(Hit, Start, End, ECollisionChannel::ECC_Visibility, Params2))
return {Hit};
else
return {};
}
UPrimitiveComponent* UBasicVRInteractionComponent::GetFirstComponentSimulatingPhysics(const AActor* TargetActor) const
{
TArray<UPrimitiveComponent*> PrimitiveComponents;
TargetActor->GetComponents<UPrimitiveComponent>(PrimitiveComponents);
// find the component that simulates physics
for (const auto& Component : PrimitiveComponents) {
if (Component->IsSimulatingPhysics()) {
return Component;
}
}
return nullptr;
}
......@@ -44,4 +44,5 @@ private:
void HandlePhysicsAndAttachActor(AActor* HitActor);
FTwoVectors GetHandRay(float Length) const;
TOptional<FHitResult> RaytraceForFirstHit(const FTwoVectors& Ray) const;
UPrimitiveComponent* GetFirstComponentSimulatingPhysics(const AActor* TargetActor) const;
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment