简体   繁体   中英

Best way to draw moving sphere in a smooth way in C#

Hello: I am trying to create an app which will display a moving sphere. App will vary speed and direction. I've tried Adobe Flash but cannot get it smooth. Smoothness is essential in this case. So I am trying C#.

Initially, I can see that this can be implemented by: 1) Creating a PictureBox of a sphere, and using a Timer, change its coordinates. or 2) Using the this.paint function to draw a filled circle, and somehow, with a timer, erasing and redrawing it.

Can someone recommend the best path to take? I'll have a main menu where the user will chose speed/direction/how many etc... and then simply show the "game window" with the moving spheres. Any guidance would be much appreciated.

This is to be displayed on a PC only.

Thanks -Ed

I just answered a similar question here .

NOTE : Depending on your needs, it is possible to achieve smooth animations under (under certain conditions) though you are responsible for everything. provides an animation framework but is perhaps a milestone harder.

It probably does not matter should you pursue first or WPF. You arguably could learn the basics under then move over to . may require you to learn quite a bit before you can do anything.

Summary

Essentially what this does is to create an offscreen bitmap that we will draw into first. It is the same size as the UserControl. The control's OnPaint calls DrawOffscreen passing in the Graphics that is attached to the offscreen bitmap. Here we loop around just rendering the tiles/sky that are visible and ignoring others so as to improve performance.

Once it's all done we zap the entire offscreen bitmap to the display in one operation. This serves to eliminate:

  • Flicker
  • Tearing effects (typically associated with lateral movement)

There is a Timer that is scheduled to update the positions of all the tiles based on the time since the last update . This allows for a more realistic movement and avoids speed-ups and slow-downs under load. Tiles are moved in the OnUpdate method.

If you note in the code for Timer1OnTick I call Invalidate(Bounds); after animating everything. This does not cause an immediate paint rather Windows will queue a paint operation to be done at a later time. Consecutive pending operations will be fused into one. This means that we can be animating positions more frequently than painting during heavy load. Animation mechanic is independent of paint . That's a good thing, you don't want to be waiting for paints to occur. does a similar thing

Please refer to my full SO answer complete with sample code

Here are a few hints to get you going:

First you will need to come to a decision about which platform to target: WPF or Winforms .

Then you should know what to move across what; a nice Bitmap or just a circle across an empty background or a Bitmap or a Form with controls on it.

In Winforms both your approaches will work, esp. if you set a circular region see here for an example of that. (The part in the fun comment!)

And yes, a Timer is the way to animate the sphere. Btw, a Panel or even a Label can display an Bitmap just as well as a PictureBox .

For smooth movements make sure to set the Form.Doublebuffered=true , if you move across a Form . If you move across any other control (except a PictureBox or a Label ) you will need to subclass it to get access to the DoubleBuffered property!

It is often also a good idea to keep the Location of a moving item in a variable as a PointF and use floats for its speed because this way you can fine grain the speed and Location changes and also the Timer Intervals !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM