1 Attachment(s)
[C#] Circular movement - 2D
This is really a simple thing: It is just a ball that circulates a point.
You can define the coordinates and the circunference radius. (Do not set it to a too small value).
Here is a screenshot:
http://i46.tinypic.com/1z70cav.png
How to use:
Adjust the values as you want.
Click Draw Center.
Click Start to start circulating/end to stop
The checkbox will draw a line between the circulating object and the circumference center.
Sources attached.
Some funny thing I noticed: the distance between the trajetory center and the circulating object is not always exactly the same as the trajetory radius.
Maybe because of the irrational PI number? And if you pay attention, the circulating object does not follow strictly the pre-defined trajetory,it goes sometimes out of the boundaries and sometimes in, depending in which point of the trajetory the circulating object is.
If it is not normal, please tell me so I can fix it!
PS: RAIO = RADIUS
Re: [C#] Circular movement - 2D
Re: [C#] Circular movement - 2D
Did you use Point or PointF for calculation of them points? (p.s. havent looked at source)
Perhaps doing all calculations in PointF would allow the object to traverse the trajectory more closely.
Re: [C#] Circular movement - 2D
I am using PointF already, indeed because of the precision =p
Re: [C#] Circular movement - 2D
In your button1_click event,
Code:
this.obj_pos.X = r * (float)Math.Cos(ctr * Math.PI / 180.0f) + circle_center.X;
this.obj_pos.Y = r * (float)Math.Sin(ctr * Math.PI / 180.0f) + circle_center.Y;
For every graphical object in C#, it's position is defined as the top-leftmost point (well im not sure but at least for WinForms its defined this way). Which means for circular objects, it's position isn't defined as its centre but rather by the top-leftmost point of the square it is bounded to. So we'll have to offset its position by half the diameter for both X and Y coordinates to get the coordinates of the coordinates of the centre.
In short,
Centre = (Position.X + 0.5*Radius, Position.Y + 0.5*Radius)
= (Position.X + 0.5*Width, Position.Y + 0.5*Height)
Some simple math (I.E. deducting the offsets from both sides) will give :
Code:
this.obj_pos.X = r * (float)Math.Cos(ctr * Math.PI / 180.0f) + circle_center.X - this.obj_pos.Width / 2;
this.obj_pos.Y = r * (float)Math.Sin(ctr * Math.PI / 180.0f) + circle_center.Y- this.obj_pos.Height / 2;
The same applies for ovals as well in case you're ever gonna work with ellipses :)
BTW, I would strongly recommend you to organize your tabindexes for the controls (it's jumping all over the place now) or disable it completely by setting tabstop = false