[C#] Circular movement - 2D

Results 1 to 5 of 5
  1. #1
    Fuck. SheenBR is offline
    ModeratorRank
    Feb 2008 Join Date
    Jaú, BrazilLocation
    2,433Posts

    [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:



    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
    Attached Files Attached Files
    Last edited by SheenBR; 13-09-12 at 07:50 AM.


  2. #2
    Self-claimed retard Shibi is offline
    MemberRank
    May 2012 Join Date
    SwedenLocation
    220Posts

    Re: [C#] Circular movement - 2D

    Nice program ;o !

  3. #3
    JavaScript Is Best Script Jash is offline
    MemberRank
    Dec 2010 Join Date
    SingaporeLocation
    683Posts

    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.

  4. #4
    Fuck. SheenBR is offline
    ModeratorRank
    Feb 2008 Join Date
    Jaú, BrazilLocation
    2,433Posts

    Re: [C#] Circular movement - 2D

    I am using PointF already, indeed because of the precision =p

  5. #5
    JavaScript Is Best Script Jash is offline
    MemberRank
    Dec 2010 Join Date
    SingaporeLocation
    683Posts

    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
    Last edited by Jash; 05-10-12 at 01:07 PM.



Advertisement