You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF含碰撞检测的动画实现:如何让车辆避行火车并队列行驶?

WPF车辆动画开发问题

需求说明

  • 车辆沿预设道路行驶,遇到火车时需等待火车通过后再继续行驶
  • 车辆需成队列行驶,解决当前车辆互相穿透的问题
  • 不确定上述需求是否可行

当前实现代码

namespace Symulator_ruchu
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {       
        Random r = new Random();
        IEnumerable<PathFigure> colle;

        DispatcherTimer dispatcherTimer = new DispatcherTimer();
        DispatcherTimer train = new DispatcherTimer();
     
        public MainWindow()
        {
            InitializeComponent();
            dispatcherTimer.Tick += new EventHandler(dispTimer_tick);
            dispatcherTimer.Interval = new TimeSpan(0,0,1);
            dispatcherTimer.Start();
            train.Tick += new EventHandler(train_tick);
            train.Interval = new TimeSpan(0, 0, 10);
            train.Start();
        }

        private void train_tick(object sender, EventArgs e)
        {
            Path car = new Path
            {
                Name = "AnimatedMatrixTrain",
                Fill = Train.Fill,
                Data = new RectangleGeometry
                {

                    Rect = new Rect(0, 0, 55, 270)
                },
                LayoutTransform = new RotateTransform
                {
                    Angle = 90
                },
                RenderTransform = new MatrixTransform
                {

                    Matrix = new Matrix
                    {
                        OffsetX = 100,
                        OffsetY = 100
                    }
                }

            };

            MatrixAnimationUsingPath maup = new MatrixAnimationUsingPath
            {
                Duration = TimeSpan.FromSeconds(r.Next(5, 10)),
                DoesRotateWithTangent = true,
                AutoReverse = false,
                PathGeometry = new PathGeometry
                {
                    Figures = PathFigureCollection.Parse("M 745,900 L 745,0")
                }
            };

            Storyboard.SetTarget(maup, car);
            Storyboard.SetTargetProperty(maup, new PropertyPath("(UIElement.RenderTransform).(MatrixTransform.Matrix)"));

            Storyboard storyboard = new Storyboard();
            storyboard.Children.Add(maup);

            //Canvas.SetTop(car, r.Next(1, 500));
            //Canvas.SetLeft(car, r.Next(1, 500));

            storyboard.Begin(car);

            Canv.Children.Add(car);
        }

        private void dispTimer_tick(object sender, EventArgs e)
        {
            Path car = new Path
            {
                Name = "AnimatedMatrixCar",
                Fill = Car.Fill,
                Data = new RectangleGeometry
                {
                    
                   Rect = new Rect(0,0,60,30)
                },
                LayoutTransform = new RotateTransform
                {
                    Angle = 180
                },
                RenderTransform = new MatrixTransform
                {
                    
                    Matrix = new Matrix
                    {
                        OffsetX = 100,
                        OffsetY = 100
                    }
                }

            };

           
            MatrixAnimationUsingPath maup = new MatrixAnimationUsingPath
            {
                Duration = TimeSpan.FromSeconds(r.Next(5,10)),
                DoesRotateWithTangent = true,
                AutoReverse = false,
                PathGeometry = new PathGeometry
                {
                    Figures = PathFigureCollection.Parse("m 10,286 h 800 c 130,10  190, 200 -105, 180 h -450 c -160,10 -200,250 -35,290 H 1200")
                }
            };

          
            Storyboard.SetTarget(maup, car);
            Storyboard.SetTargetProperty(maup, new PropertyPath("(UIElement.RenderTransform).(MatrixTransform.Matrix)"));

            Storyboard storyboard = new Storyboard();
            storyboard.Children.Add(maup);

            //Canvas.SetTop(car, r.Next(1, 500));
            //Canvas.SetLeft(car, r.Next(1, 500));

            storyboard.Begin(car);

            Canv.Children.Add(car);

          
        }
    }
}

窗口效果

窗口中展示了道路、行驶的车辆与火车,车辆通过MatrixAnimationUsingPath沿预设路径做动画,但当前存在车辆互相穿透的问题。

内容的提问来源于stack exchange,提问作者Mateusz Rosiński

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 22:15:27