Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SplineConstraint extends Object;
- var MyPawn Pawn; //Pawn to Constrain to the Spline
- var array<SplineActor> SplineCache; //Cache of our Current SplineActors
- var bool bEnableDebug; //Is Debugging enabled for the SplineConstraint
- var Vector StartPosition; //Pawn's Start location on the Path
- var float LastPosition; //Last Postion on the Spline (float value not Vector)
- exec function ShowConstraint()
- {
- bEnableDebug = !bEnableDebug;
- }
- //Initializes the class and finds the SplineActors
- function Initialize(WorldInfo WorldInfo)
- {
- local SplineActor Current;
- //Add Our splines to our Cache
- foreach WorldInfo.DynamicActors( class'SplineActor', Current)
- {
- SplineCache.AddItem( Current );
- Current.nextOrdered = Current.GetBestConnectionInDirection(vect(0,1,0));
- if(Current.nextOrdered != none)
- {
- Current.nextOrdered.prevOrdered = Current;
- }
- }
- }
- function SetConstraint(Pawn PawnToConstrain)
- {
- Pawn = MyPawn(PawnToConstrain);
- }
- function Update(float DeltaTime)
- {
- local SplineActor Spline; //Used to iterate through our SplineCache
- local SplineActor CurrentSplineActor; //Current SplineActor We are Focused on
- local SplineActor PrevSplineActor; //Previous SplineActor from CurrentSplineActor
- local SplineActor NextSplineActor; //Next SplineActor from CurrentSplineActor
- local Vector RequiredLocation; //Position on the spline we should be constrained to
- local Vector Direction; //Direction we should be facing. Heading Vector. using Rotator(Direction)
- local float BestDistance; //Used to find the closest SplineActor
- local float Distance; //Distance on the Spline to find a Location
- local float DotProduct; //Used to find which side of the CurrentSplineActor we are on.
- //Set to a High Number
- BestDistance = 10000;
- //Find the closest SplineActor that has a Previous and Next Ordered SplineActor
- foreach SplineCache( Spline )
- {
- if(VSize( Pawn.Location - Spline.Location ) < BestDistance)
- {
- // If this Spline doesn't have a PrevOrdered or NextOrdered then skip it.
- // Our CurrentSplineActor needs to be one that has a Prev and Next Ordered SplineActor
- if(Spline.prevOrdered == none || Spline.nextOrdered == none)
- continue;
- //Set our CurrentSplineActor
- CurrentSplineActor = Spline;
- //Update our BestDistance to this Spline and then check for a closer SplineActor
- BestDistance = VSize( Pawn.Location - Spline.Location);
- }
- }
- //Don't continue if we don't have a CurrentSplineActor
- if(CurrentSplineActor == none)
- return;
- NextSplineActor = CurrentSplineActor.nextOrdered;
- PrevSplineActor = CurrentSplineActor.prevOrdered;
- //Need a Previous and Next SplineActor for the Constraint to Work.
- //Check here just in case
- if( NextSplineActor == none || PrevSplineActor == none)
- return;
- //Set our starting location.
- //Playerstart should be close to this in the Map.
- if(StartPosition == vect(0,0,0))
- Pawn.SetLocation(CurrentSplineActor.prevOrdered.Location); //Generally the start pos will be on a Prev SplineActor
- //Find out which side of the CurrentSplineActor we are on for our positon Calculations.
- DotProduct = Normal(Pawn.Location - CurrentSplineActor.Location) dot Normal(CurrentSplineActor.Location - NextSplineActor.Location);
- //If the DotProduct is Less than 0 than we are in between the Current and Next SplineActors
- if(DotProduct < 0)
- {
- //Find our 2D distance from Pawn.Location to Our CurrentSplineActor.Location
- Distance = VSize2D(Pawn.Location - CurrentSplineActor.Location);
- //Use Distance to find the required Location on the Spline
- RequiredLocation = CurrentSplineActor.FindSplineComponentTo(NextSplineActor).GetLocationAtDistanceAlongSpline(Distance);
- //Find our Heading -- Doesn't work yet, Haven't spent any time figuring it out.
- Direction = Normal(CurrentSplineActor.FindSplineComponentTo(NextSplineActor).GetTangentAtDistanceAlongSpline(Distance));
- `log("After: " @ DotProduct);
- }
- else
- {
- //Find our 2D distance from Pawn.Location to Our PrevSplineActor.Location
- Distance = VSize2D(Pawn.Location - PrevSplineActor.Location);
- //Use Distance to find the required Location on the Spline
- RequiredLocation = PrevSplineActor.FindSplineComponentTo(CurrentSplineActor).GetLocationAtDistanceAlongSpline(Distance);
- //Find our Heading -- Doesn't work yet, Haven't spent any time figuring it out.
- Direction = Normal(PrevSplineActor.FindSplineComponentTo(CurrentSplineActor).GetTangentAtDistanceAlongSpline(Distance));
- `log("Before: " @ DotProduct);
- }
- //Set the RequiredLocation Z to the Pawn.Location.Z Since we are only Constraining X and Y
- RequiredLocation.Z = Pawn.Location.Z;
- //********************************
- //******START PROBLEM AREA********
- //Not sure how to actually go about setting the Pawn's Location without restricting physics.
- //If I do a distance between the required location and the Pawn's Location there is a bungie cord type
- //problem that comes up. Basically the Pawn ends up being ahead of the Required location so It gets pulled
- //back. I haven't been able to find a good solution for this. (hard to explain what happens)
- //Pawn.SetLocation(RequiredLocation);
- //*******END PROBLEM AREA*********
- //********************************
- //Face our Pawn to the the direction the Spline is faceing
- //Needs to be modified to go both directions and dependent on the mouse cursor position.
- Pawn.FaceRotation(Rotator(Direction), DeltaTime);
- //Save our Last Position
- LastPosition = Distance;
- //Check if we should show Debug
- if(bEnableDebug)
- {
- //Draw a line from Pawn.Location to NextSplineActor.Location
- Pawn.DrawDebugLine(Pawn.Location, NextSplineActor.Location, 255,0,0, false);
- //Draw a line from Pawn.Location to CurrentSplineActor.Location
- Pawn.DrawDebugLine(Pawn.Location, CurrentSplineActor.Location, 0,255,0, false);
- //Draw a line from Pawn.Location to PrevSplineActor.Location
- Pawn.DrawDebugLine(Pawn.Location, PrevSplineActor.Location, 255, 0, 0, false);
- //Draw a line from Pawn.Location to RequiredLocation
- Pawn.DrawDebugLine(Pawn.Location, RequiredLocation, 0, 0, 255, false);
- //Draw Boxes around the SplineActors to show their Location
- foreach SplineCache( Spline )
- {
- Pawn.DrawDebugBox(Spline.Location, vect(10,10,10), 255, 255, 0, false);
- }
- }
- }
- DefaultProperties
- {
- bEnableDebug = true
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement