Minimum distance step movement for orbiting circle

I've partly borrowed some code that creates a 'clock' by splitting 360 degrees around the center of the screen into 12 steps. While the circle does 'jump' between steps, the 'jumping' behaviour doesn't work the way it is supposed to. The problem is that the circle will only jump forward by one step, if the angle of the mouse is >= the angle of the step, and it jumps backwards a step as soon as the mouse angle goes below the angle of the step.

I've through various different methods tried (and failed) to create an effective method to make the stepping circle jump to the step which angle is closest to the angle of the mouse, so that it would jump step 1 when the angle was greater than 15 degrees and less than 45.

Any ideas?

(Also: I am for some reason completely unable to format the code into the forum(???) so i've uploaded it to github):



  • to format the code, edit your post, highlight the code, press ctrl-o.

    i must've written those words 100000 times.

    anyway, your code:

    float rad;    //radius of circle
    float crad;   //distance to orbit
    PVector vCent, vMouse;
    float angle1; 
    final float half=0.5;
    int step;
    float stepAngle;
    final float twelve=12;
    float cxubx;
    float cyubx;
    float ccx;
    void setup() {
      vCent= new PVector(0, -height*half); //Straight upwards
    void draw() { 
      translate(width/2, height/2);
      vMouse= new PVector(mouseX-width/2, mouseY-height/2);
      ellipse(mouseX-half*width, mouseY-half*height, rad, rad);
      angle1=(mouseX<=ccx) ? 360-degrees(PVector.angleBetween(vCent, vMouse)) : degrees(PVector.angleBetween(vCent, vMouse));
      step=int(map(angle1, 0, 360, 0, twelve)); //dividing 360 degrees into 12 steps
      if (step==12) { 
      stepAngle=((step / twelve) * TWO_PI);
      ellipse(cxubx, cyubx, rad, rad);
      text("mouseangle;"+angle1, 100, 100);
      text("step:"+step, 100, 150);
      text("stepangle:"+degrees(stepAngle), 100, 200);
  • edited April 2017

    I know exactly what you mean and have an idea for how to solve it that I know works as I'we done it years ago, but it wasn't in processing or even for mouse,
    actually it was for a xbox 360 controller, and 8 steps rather than 12,
    but the principle should be the same, you check the "change of rotation" in steps and add it to the clock, rather than direct the clock towards the mouse.

    Something like this..


    You need a special case for when it goes from 12 to 1 and vice versa

    Also having 36 mouse steps and 12 clock steps would help

  • solution to this was to use round() instead of int() when mapping the angles

Sign In or Register to comment.