参考我目前正在构建的这个编程游戏。
重要提示:向下滚动查看[编辑]
用户可以在我的游戏中调用的一些方法将是对机器人进行 Translate Transform 的方法(Canvas
基本上是一个)。
从 move 方法中,我知道了 Robot 当时将要面对的航向(角度),也知道 Robot 想要移动的像素长度。
现在,我面临的问题是如何将画布(在计时器中)平移到当前的面向角度?
替代文字 http://img8.imageshack.us/img8/3606/robottranslatemovementfu3.jpg
一定有一些我在这里遗漏的数学,但我只是不知道在每个 Timer 滴答声中要解决什么问题。
这是被调用的方法,它包含每 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();
});
}
[重要编辑]
我现在已经更改了我的逻辑,使用 WPFBeginAnimation
而不是自动收报机来为我制作动画。所以现在我不需要计算每个刻度的新坐标,但我只提供结束坐标,然后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坐标(画布当前所在的位置)和距离,我如何计算结束坐标?
更新:解决方案