Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Animate along Polyline

$
0
0
 I have been searching high and low.  At this point everything is cloudy so I thought I'd just ask my question to see if anyone could point me in the right direction.

I'm trying to animate a Path element with multiple points (think of a hand drawn car in Blend, mostly straight lines and, 2 curves) along a complex Polyline.  I cannot find anything that directly demonstrates or describes how to do this.  I usually find simple shapes along geometry, which I understand. 

I need to accomplish as much animation from codebehind, as the paths (polylines) for my animations are dynamic (depending on which layout is being loaded).

I have probably a few questions that might help me better solve my issue:

1. Should I be using a Polyline to draw a 500 + X and Y point road (for lack of a better term)?  I load the X and Y from a CSV.

2. For my object that I want to animate, being a path with multiple data points, is it fine as it is?  Is there a better way to accomplish my goal by using a different object type? This is static and doesn't need to change.. just move along the road.


Here is a sample of code that works, but I only get a downward (From north west to south east) movement.. no reverse movement (X or Y) what so ever even though the start point and end point are exactly the same.

Notes:
pthCar - Object in XAML that is the object that I want animated only the PointCollection RoadPoints.
RoadPoints - Current points collection that are bound to the Polyline.Points object in the XAML and read from a textfile.


1        private string _targetName = "pthCar";  
2        private double _xBeginTime = 5;  
3 
4        public void AnotherAnimation(PointCollection RoadPoints)  
5        {  
6            TransformGroup carTransformGroup = new TransformGroup();  
7            TranslateTransform tt = new TranslateTransform();  
8            carTransformGroup.Children.Add(tt);  
9 
10            pthCar.RenderTransform = carTransformGroup;  
11 
12            NameScope.SetNameScope(thisnew NameScope());  
13            this.RegisterName("tt", tt);  
14 
15            DoubleAnimationUsingKeyFrames daX = new DoubleAnimationUsingKeyFrames();  
16            DoubleAnimationUsingKeyFrames daY = new DoubleAnimationUsingKeyFrames();  
17 
18            daX.BeginTime = TimeSpan.FromMilliseconds(_xBeginTime);  
19            daY.BeginTime = TimeSpan.FromMilliseconds(_xBeginTime);  
20 
21            double previousX = 0;  
22 
23            int counter = 0;  
24            foreach (myRoadPoint rp in RoadPoints)  
25            {  
26                if (counter % 2 == 0)  
27                {  
28                    double xvalue = rp.X * scale;  
29                    if (xvalue < previousX) xvalue = xvalue * -1;  
30                    previousX = xvalue;  
31 
32                    LinearDoubleKeyFrame ldkf_X = new LinearDoubleKeyFrame();  
33                    ldkf_X.Value = xvalue;  
34                    ldkf_X.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(.5));  
35 
36                    daX.KeyFrames.Add(ldkf_X);  
37 
38                    LinearDoubleKeyFrame ldkf_Y = new LinearDoubleKeyFrame();  
39                    ldkf_Y.Value = rp.Y * Scale;  
40                    ldkf_Y.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(.5));  
41                    daY.KeyFrames.Add(ldkf_Y);  
42 
43                }  
44                counter++;  
45            }  
46 
47            Storyboard.SetTargetName(daX, "tt");  
48            Storyboard.SetTargetProperty(daX, new PropertyPath(TranslateTransform.XProperty));  
49 
50            Storyboard.SetTargetName(daY, "tt");  
51            Storyboard.SetTargetProperty(daY, new PropertyPath(TranslateTransform.YProperty));  
52            
53 
54            Storyboard sb = new Storyboard();  
55 
56            sb.Children.Add(daX);  
57            sb.Children.Add(daY);  
58            sb.Begin(this);  
59 
60        } 

If you need more info or clarification please feel free to ask.  I have been racking my brain over this and am probably staring right at the solution but just can't see it.

Thanks in advance.

Steve

Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>