With reference to this programming game I am currently building.
Important: Scroll down to see [Edit]
Some of the methods that a user can call in my game will be methods that will do a Translate Transform to the Robot (just a Canvas
basically).
From the move method, I know the heading (angle) at which the Robot will be facing at the time, and I also know the length of pixels that the Robot wants to move.
Now, the problem I am facing is how to translate the canvas (in a timer) to move at its current facing angle?
alt text http://img8.imageshack.us/img8/3606/robottranslatemovementfu3.jpg
There must be some Maths that I am missing here, but I just can't figure out what to work out in each Timer tick.
Here is the method that is invoked, that contains the timer that will tick every 5 milliseconds, animate the movement:
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();
});
}
[IMPORTANT EDIT]
I have now changed my logic to use the WPF BeginAnimation
instead of a ticker to do the animation for me. So now I do not need to calculate the new coordinates on every tick, but I just provide the end coordinates, and the BeginAnimation
will translate it over a period of time:
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);
});
}
So now, given the angle (0-359) the canvas is currently rotated at, starting x and y coordinates (of where the canvas is currently situated) and distance, how do I calculate to end coordinates?
UPDATE: Solution