Instead of timer, use a separate thread which represents the scenario. Make a loop with some
Thread.Sleep
and check up the real time using the class
System.Diagnostics.Stopwatch
:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[
^].
In all coordinate calculations, use the real time, not number of frames. Do not make any assumptions of the actual time delay between motion phases, used the ream time measurements.
The physics is very simple, but if you need to take energy losses into account, the hard point would be the criterion to stop jumping. If this is 2D or 3D, the hard point would be a moment of transition between jumping and rolling on the floor, and them from rolling to complete stop. Think about it.
Now, isolate model from presentation very well. You need some different options for presentation. I would suggest some three options: 1)
System.Windows.Forms
, custom control with direct rendering, 2) WPF direct rendering, 3) WPF
Canvas
with a objects which could be XAML vector images created separately in a vector editor like Inkscape. For WPF approaches, please see:
http://msdn.microsoft.com/en-us/library/ms748373.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.aspx[
^].
For the
System.Windows.Forms
approach, please see my past answers:
Drawing Lines between mdi child forms[
^],
capture the drawing on a panel[
^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[
^],
How to speed up my vb.net application?[
^].
With a thread (or even a timer, except
System.Windows.Time
which you should never use for animation due to its prohibitively poor accuracy), there is one little problem:
You cannot call anything related to UI from non-UI thread. Instead, you need to use the method
Invoke
or
BeginInvoke
of
System.Windows.Threading.Dispatcher
(for both Forms or WPF) or
System.Windows.Forms.Control
(Forms only).
You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[
^],
Problem with Treeview Scanner And MD5[
^].
See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[
^],
Control events not firing after enable disable + multithreading[
^].
I must say, this simple problem could be a great illustration for application of different technologies. Take the graphics along, each approach would take a really big article on each approach, to explain all the detail. Excellent exercise in programming: threading, physics, graphics and UI.
Good luck,
—SA