I am new to Silverlight and want to animate a ball that is shot out of a cannon then moves through the air under the laws of physics (so roughly, on an elliptical path).
My inclination is to use a callback timer and move the little ball every 50ms by changing its Canvas.LeftProperty and Canvas.TopProperty values. Is this the right approach or should I be using a DoubleAnimation? My resistance to using animations here is that I would have to create many different animation in succession.
Which path would a Silver开发者_高级运维light guru take?
Have a look at a question I asked some time ago here the accepted answer gives a comparison of five different ways to do animation for games in silverlight linking to this blog. The article concludes that the method you choose will depend on a number of factors.
To answer you question I personally would use the dispatcher timer which is fired each time the screen re-draws and set the properties then to move the X and Y.
I also found Game Physics 101 a really good resource.
Might be a little overkill bit are you aware of the physics helper libraray
I don't think you would have to create several animations in succession. The ball only changes direction once.
The Y value goes down (making the ball look like it's going up) and then goes up (making the ball look like it's going down). The deceleration as it's going down should be constant (something representing 9.8 m/s/s) and the acceleration as the Y value goes up (cannonball goes down) is constant, until it hits the point representing the ground.
The X value should be constant, or else decelerate slightly should air resistance be taken into account.
Therefore, you would work out the following:
- Where does it start (the cannon, obviously)
- Where does the ball hit it's vertical peak
- Where does the ball hit the ground
So now you can animate the Y value as a start value, and end value and one waypoint.
And you can animate the X value with just a start and an end.
Of course, it's going to involve non-trivial mathematics to determine the exact numbers to enter into the AccelerationRatio, DecelerationRatio, SpeedRatio, Duration, From and To parameters. But you would have to work that out no matter what method you're using.
精彩评论