views:

547

answers:

3
+1  A: 

To help with the math:

If you want to move d pixel distances in t seconds then you will need to move on average x/t (pixels/seconds).

For this you will need to store the start time of your animation (start_time).

In every timer tick, compute the percentage of animation completed:

percent_complete= (now - start_time) / t

Multiply percent_complete by the distance you want to move to get the distance you should be from where you started (d_now). Remember to keep track of the start distance when you start the animation.

d_now = percent_complete * d

The robot's x and y for the current timer tick can then be computed by:

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

You may need to tweak the signs of things but that's the gist of it. Math aside, aren't basic animation routines already provided for you in WPF? I've never used it but that was my impression. Link.

colithium
Thanks for the reply, but what I can't understand from your answer is how do I get the 't' in [percent_complete= (now - start_time) / t] ? Because I only know the distance the bot wants to move, not the time it will take to do so
Andreas Grech
And also, what exactly do you mean by 'start_time' ? Isn't start_time always a 0ms ?
Andreas Grech
How long it should take is: distance/velocity. You can pick the velocity. Start time may not always be time = 0, it may be given in ticks/milliseconds/UNIX time. All you care about is that start_time and now use the same reference point.
sixlettervariables
I changed my logic, and now I only need to get the end coordinates. How can I calculate the end coordinates?
Andreas Grech
x = start.x + d * cos(heading)y = stary.y + d * sin(heading)where d is the total distance you want to move. A tip: sin and cos take radians, if you're getting weird results, it's probably because you're passing degrees.
colithium
A: 

I would suggest at least one attempt using a DoubleAnimation/DoubleAnimationUsingPath or a PointAnimation/PointAnimationUsingPath (I'm not sure which is easier to animate the location of a Canvas). This way you're not (necessarily) duplicating effort that WPF perhaps does better. It may end up that manipulating storyboards is more expensive than custom animations, and thus you should go with @colithium's solution with one addition: logic to not move anywhere if it is at its destination, and perhaps decouple the animation if that occurs (to save on computation time).

sixlettervariables
A: 

Solution

Thanks to the help of both Andy Mikula and Cameron MacFarland, it is finally working as intended.

And here is the working code:

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);
Andreas Grech