Advertisement
Guest User

TrackingMove

a guest
Jan 19th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. void UMover::KinematicTrackingMove(const FVector DirectionVector, const float MoveDistance, const FQuat NewRotation, const int MaxRetraces, const float RetraceDistance, float& ActualDistanceMoved, bool& HitOccurred)
  2. {
  3. float DistanceMoved = 0.0f;
  4. int RetracesRemaining = MaxRetraces + 2;
  5.  
  6. TraceAndResolvePenetration(DirectionVector * MoveDistance, NewRotation, DistanceMoved, RetracesRemaining); // Perform the initial trace
  7. if (HitResult->bBlockingHit)
  8. {
  9. HitOccurred = true;
  10. const FVector WorldTargetLocation = HitResult->TraceEnd;
  11. FVector DirectionVectorToWorldTarget = DirectionVector;
  12. float DistanceToWorldTarget = MoveDistance - DistanceMoved;
  13.  
  14. while (RetracesRemaining > 0)
  15. {
  16. const float DistanceToTarget = (HitResult->TraceEnd - HitResult->Location).Size();
  17. //const FVector PreviousSlideVector = NewMoveVector;
  18.  
  19. float VelocityScale;
  20. const FVector SlideVector = CalculateSlideMovementVector(DirectionVectorToWorldTarget, DistanceToWorldTarget, HitResult->Normal, VelocityScale).GetSafeNormal();
  21. const float SlideDistanceUncapped = DistanceToWorldTarget * VelocityScale;
  22. const float SlideDistance = FMath::Min(SlideDistanceUncapped, RetraceDistance); // Cap the slide distance at the RetraceDistance
  23.  
  24. TraceAndResolvePenetration(SlideVector * SlideDistance, NewRotation, DistanceMoved, RetracesRemaining); // Perform a slide
  25.  
  26. if (HitResult->bBlockingHit) // Slide hit
  27. {
  28. DirectionVectorToWorldTarget =
  29. // If the slide vectors are moving in an angle >=90* and there was little movement, the trace is stuck bouncing in a corner
  30. //if (HitResult->Distance < SMALL_NUMBER) // && (NewMoveVector | PreviousSlideVector) > SMALL_NUMBER)
  31. {
  32. // Break
  33. //RetracesRemaining = 0;
  34. }
  35. // Otherwise, slide again
  36. }
  37. else // No hit while sliding
  38. {
  39. CurrentDirectionVector = WorldTargetLocation - HitResult->Location;
  40. TraceAndResolvePenetration(CurrentDirectionVector, NewRotation, DistanceMoved, RetracesRemaining); // Try to move directly to NewLocation
  41. CurrentDirectionVector.Normalize();
  42. if (!HitResult->bBlockingHit) // Reached NewLocation
  43. {
  44. RetracesRemaining = 0;
  45. }
  46. // Otherwise, a hit. Slide again
  47. }
  48. }
  49. }
  50. ActualDistanceMoved += DistanceMoved;
  51. }
  52.  
  53. const FVector CalculateSlideMovementVector(const FVector TraceDirection, const float TraceDistanceRemaining, const FVector HitNormal, float& VelocityScale)
  54. {
  55. const float NegNewVelocityScale = TraceDirection | HitNormal;
  56. VelocityScale = 1.0f + NegNewVelocityScale;
  57. const FVector ScaledDirectionVector = TraceDirection + HitNormal * VelocityScale;
  58. return ScaledDirectionVector * TraceDistanceRemaining;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement