Advertisement
Guest User

Untitled

a guest
Jan 12th, 2010
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1. class SplineConstraint extends Object;
  2.  
  3. var MyPawn Pawn; //Pawn to Constrain to the Spline
  4. var array<SplineActor> SplineCache; //Cache of our Current SplineActors
  5. var bool bEnableDebug; //Is Debugging enabled for the SplineConstraint
  6. var Vector StartPosition; //Pawn's Start location on the Path
  7. var float LastPosition; //Last Postion on the Spline (float value not Vector)
  8.  
  9. exec function ShowConstraint()
  10. {
  11. bEnableDebug = !bEnableDebug;
  12. }
  13.  
  14. //Initializes the class and finds the SplineActors
  15. function Initialize(WorldInfo WorldInfo)
  16. {
  17. local SplineActor Current;
  18.  
  19. //Add Our splines to our Cache
  20. foreach WorldInfo.DynamicActors( class'SplineActor', Current)
  21. {
  22. SplineCache.AddItem( Current );
  23.  
  24. Current.nextOrdered = Current.GetBestConnectionInDirection(vect(0,1,0));
  25.  
  26. if(Current.nextOrdered != none)
  27. {
  28. Current.nextOrdered.prevOrdered = Current;
  29. }
  30. }
  31. }
  32.  
  33. function SetConstraint(Pawn PawnToConstrain)
  34. {
  35. Pawn = MyPawn(PawnToConstrain);
  36. }
  37.  
  38.  
  39. function Update(float DeltaTime)
  40. {
  41. local SplineActor Spline; //Used to iterate through our SplineCache
  42. local SplineActor CurrentSplineActor; //Current SplineActor We are Focused on
  43. local SplineActor PrevSplineActor; //Previous SplineActor from CurrentSplineActor
  44. local SplineActor NextSplineActor; //Next SplineActor from CurrentSplineActor
  45. local Vector RequiredLocation; //Position on the spline we should be constrained to
  46. local Vector Direction; //Direction we should be facing. Heading Vector. using Rotator(Direction)
  47. local float BestDistance; //Used to find the closest SplineActor
  48. local float Distance; //Distance on the Spline to find a Location
  49. local float DotProduct; //Used to find which side of the CurrentSplineActor we are on.
  50.  
  51. //Set to a High Number
  52. BestDistance = 10000;
  53.  
  54. //Find the closest SplineActor that has a Previous and Next Ordered SplineActor
  55. foreach SplineCache( Spline )
  56. {
  57. if(VSize( Pawn.Location - Spline.Location ) < BestDistance)
  58. {
  59. // If this Spline doesn't have a PrevOrdered or NextOrdered then skip it.
  60. // Our CurrentSplineActor needs to be one that has a Prev and Next Ordered SplineActor
  61. if(Spline.prevOrdered == none || Spline.nextOrdered == none)
  62. continue;
  63.  
  64. //Set our CurrentSplineActor
  65. CurrentSplineActor = Spline;
  66. //Update our BestDistance to this Spline and then check for a closer SplineActor
  67. BestDistance = VSize( Pawn.Location - Spline.Location);
  68. }
  69. }
  70.  
  71. //Don't continue if we don't have a CurrentSplineActor
  72. if(CurrentSplineActor == none)
  73. return;
  74.  
  75. NextSplineActor = CurrentSplineActor.nextOrdered;
  76. PrevSplineActor = CurrentSplineActor.prevOrdered;
  77.  
  78. //Need a Previous and Next SplineActor for the Constraint to Work.
  79. //Check here just in case
  80. if( NextSplineActor == none || PrevSplineActor == none)
  81. return;
  82.  
  83. //Set our starting location.
  84. //Playerstart should be close to this in the Map.
  85. if(StartPosition == vect(0,0,0))
  86. Pawn.SetLocation(CurrentSplineActor.prevOrdered.Location); //Generally the start pos will be on a Prev SplineActor
  87.  
  88. //Find out which side of the CurrentSplineActor we are on for our positon Calculations.
  89. DotProduct = Normal(Pawn.Location - CurrentSplineActor.Location) dot Normal(CurrentSplineActor.Location - NextSplineActor.Location);
  90.  
  91. //If the DotProduct is Less than 0 than we are in between the Current and Next SplineActors
  92. if(DotProduct < 0)
  93. {
  94. //Find our 2D distance from Pawn.Location to Our CurrentSplineActor.Location
  95. Distance = VSize2D(Pawn.Location - CurrentSplineActor.Location);
  96.  
  97. //Use Distance to find the required Location on the Spline
  98. RequiredLocation = CurrentSplineActor.FindSplineComponentTo(NextSplineActor).GetLocationAtDistanceAlongSpline(Distance);
  99.  
  100. //Find our Heading -- Doesn't work yet, Haven't spent any time figuring it out.
  101. Direction = Normal(CurrentSplineActor.FindSplineComponentTo(NextSplineActor).GetTangentAtDistanceAlongSpline(Distance));
  102. `log("After: " @ DotProduct);
  103. }
  104. else
  105. {
  106. //Find our 2D distance from Pawn.Location to Our PrevSplineActor.Location
  107. Distance = VSize2D(Pawn.Location - PrevSplineActor.Location);
  108.  
  109. //Use Distance to find the required Location on the Spline
  110. RequiredLocation = PrevSplineActor.FindSplineComponentTo(CurrentSplineActor).GetLocationAtDistanceAlongSpline(Distance);
  111.  
  112. //Find our Heading -- Doesn't work yet, Haven't spent any time figuring it out.
  113. Direction = Normal(PrevSplineActor.FindSplineComponentTo(CurrentSplineActor).GetTangentAtDistanceAlongSpline(Distance));
  114. `log("Before: " @ DotProduct);
  115. }
  116.  
  117. //Set the RequiredLocation Z to the Pawn.Location.Z Since we are only Constraining X and Y
  118. RequiredLocation.Z = Pawn.Location.Z;
  119.  
  120. //********************************
  121. //******START PROBLEM AREA********
  122. //Not sure how to actually go about setting the Pawn's Location without restricting physics.
  123. //If I do a distance between the required location and the Pawn's Location there is a bungie cord type
  124. //problem that comes up. Basically the Pawn ends up being ahead of the Required location so It gets pulled
  125. //back. I haven't been able to find a good solution for this. (hard to explain what happens)
  126.  
  127. //Pawn.SetLocation(RequiredLocation);
  128.  
  129. //*******END PROBLEM AREA*********
  130. //********************************
  131.  
  132. //Face our Pawn to the the direction the Spline is faceing
  133. //Needs to be modified to go both directions and dependent on the mouse cursor position.
  134. Pawn.FaceRotation(Rotator(Direction), DeltaTime);
  135.  
  136. //Save our Last Position
  137. LastPosition = Distance;
  138.  
  139. //Check if we should show Debug
  140. if(bEnableDebug)
  141. {
  142. //Draw a line from Pawn.Location to NextSplineActor.Location
  143. Pawn.DrawDebugLine(Pawn.Location, NextSplineActor.Location, 255,0,0, false);
  144.  
  145. //Draw a line from Pawn.Location to CurrentSplineActor.Location
  146. Pawn.DrawDebugLine(Pawn.Location, CurrentSplineActor.Location, 0,255,0, false);
  147.  
  148. //Draw a line from Pawn.Location to PrevSplineActor.Location
  149. Pawn.DrawDebugLine(Pawn.Location, PrevSplineActor.Location, 255, 0, 0, false);
  150.  
  151. //Draw a line from Pawn.Location to RequiredLocation
  152. Pawn.DrawDebugLine(Pawn.Location, RequiredLocation, 0, 0, 255, false);
  153.  
  154. //Draw Boxes around the SplineActors to show their Location
  155. foreach SplineCache( Spline )
  156. {
  157. Pawn.DrawDebugBox(Spline.Location, vect(10,10,10), 255, 255, 0, false);
  158. }
  159. }
  160. }
  161.  
  162. DefaultProperties
  163. {
  164. bEnableDebug = true
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement