WPF:以一定角度翻译画布

时间:2009-02-09 23:45:58

标签: wpf math animation timer

参考我目前正在制作的this programming game

重要提示:向下滚动以查看[编辑]

用户可以在我的游戏中调用的一些方法将是对机器人进行翻译转换的方法(基本上只有Canvas)。

从移动方法,我知道机器人当时将面向的方向(角度),我也知道机器人想要移动的像素长度。

现在,我面临的问题是如何翻译画布(在计时器中)以当前的角度移动?

alt text http://img8.imageshack.us/img8/3606/robottranslatemovementfu3.jpg

我必须在这里找到一些我缺少的数学,但我无法弄清楚每个计时器中的工作内容。

这是调用的方法,它包含每5毫秒勾选一次的计时器,为运动设置动画:

public void Ahead(int pix)
    {
        eventQueue.Enqueue((TimerDelegate)delegate(DispatcherTimer dt)
        {
            /* Available Variables:
            Translate_Body.X <= Current Robot Location on X-axis
            Translate_Body.Y <= Current Robot Location on Y-axis
            Bot.Body <= The Canvas that needs to be translated (moved)

            To translate the robot, I just basically do this:
                Translate_Body.X++; or Translate_Body.YY++; to increment, or -- to decrement ofcourse

            Now I need to figure out the logic on when to increment (or decrement, according to the angle I suppose) the Y and X
            */

            IsActionRunning = true;
            dt.Tick += new EventHandler(delegate(object sender, EventArgs e)
            {
                lock (threadLocker)
                {
                     //The logic needs to happen in here, with each timer tick.
                     //so with each timer tick, the robot is moved on pixel according to its angle.
                     //Then I need to make a condition that will indicate that the robot has arrived at its destination, and thus stop the timer.
                }
            });    
            dt.Start();
        });
    }

[重要编辑]

我现在改变了逻辑,使用WPF BeginAnimation而不是自动收报机为我做动画。所以现在我不需要计算每个刻度线上的新坐标,但我只提供结束坐标,BeginAnimation将在一段时间内翻译它:

public void Ahead(int pix)
    {
        eventQueue.Enqueue((TimerDelegate)delegate
        {

            Animator_Body_X.From = Translate_Body.X;
            Animator_Body_X.To = //The end X-coordinate
            Translate_Body.BeginAnimation(TranslateTransform.XProperty, Animator_Body_X);

            Animator_Body_Y.From = Translate_Body.Y;
            Animator_Body_Y.To = //The end Y-coordinate
            Translate_Body.BeginAnimation(TranslateTransform.YProperty, Animator_Body_Y);
        });
    }

所以现在,给定角度(0-359)画布当前旋转,开始x和y坐标(画布当前所在的位置)和距离,如何计算结束坐标? /强>


更新:Solution

2 个答案:

答案 0 :(得分:1)

帮助数学:

如果您想在 t 秒内移动 d 像素距离,则需要平均移动x / t(像素/秒)。

为此,您需要存储动画的开始时间(start_time)。

在每个计时器刻度中,计算完成的动画的百分比:

percent_complete= (now - start_time) / t

将percent_complete乘以您想要移动的距离,以获得距您开始的距离(d_now)。记得在开始动画时跟踪起始距离。

d_now = percent_complete * d

然后可以通过以下方式计算当前计时器标记的机器人x和y:

x = d_now * cos(heading) + start.x
y = d_now * sin(heading) + start.y

你可能需要调整事物的迹象,但这是它的要点。除了数学,不是WPF中已经为您提供的基本动画例程吗?我从来没有用过它,但那是我的印象。 Link

答案 1 :(得分:0)

解决方案

感谢Andy MikulaCameron MacFarland的帮助,它最终按预期工作。

这是工作代码:

double headingRadians = Heading * (Math.PI / 180);

Animator_Body_X.From = Translate_Body.X;
Animator_Body_X.To = Math.Sin(headingRadians) * pix + Translate_Body.X;
Translate_Body.BeginAnimation(TranslateTransform.XProperty, Animator_Body_X);

Animator_Body_Y.From = Translate_Body.Y;
Animator_Body_Y.To = ((Math.Cos(headingRadians) * pix) * -1) + Translate_Body.Y;
Translate_Body.BeginAnimation(TranslateTransform.YProperty, Animator_Body_Y);