Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi guys
I'm pretty new to C# and I've been trying to make a Bouncing but unfortunately but I just failed I also looked at many source codes but neither of them were too hard nor stupid like putting 4 timer in a form
but i need very simple and easy code
Posted

1 solution

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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900