Easing + rotating past 360 or 0 fix?

Hi im making a simple compass using an arduino and processing, to make things smoother im using "easing code" but when i rotate past 360 instead of rotating right to 0, it takes the long way around. Can someone please help me with this code, i would appreciate it a lot, thanks! :)

Here is my draw() code:

void draw()
{ 

background(255);
 //                     this is the input.
ArInputComp = controlInput[0] - SmInputComp;
SmInputComp += ArInputComp * 0.1;

pushMatrix();
imageMode(CENTER);
translate(400,300);
rotate((SmInputComp) * TWO_PI/360);
image(img,0,0);
popMatrix();

Serial_Loop();

}
Tagged:

Answers

  • I think the issue is with the value of controlInput[0] what range of values does it produce.

    Also please explain what the variables ArInputComp and SmInputComprepresent?

  • do you know the function radians? It converts degree to radians.

    Does arduino deliver degree or radians?

    Did you try

    while (SmInputComp>360)
       SmInputComp=SmInputComp-360;
    
  • Hi quark :D

    controlInput[0] goes from 0 to 360

    ArInputComp and SmInputComp =

    They are just temporary values for the "smoothing/Easing" of the rotation, i dont use them anywhere else in the code :)

    Hi Chrisir :D

    Arduino delivers degrees, il try your code and see if it works! :)

  • while (SmInputComp>360)
       SmInputComp=SmInputComp-360;
    

    didnt do much :( thx anyway! :)

  • Please confirm these statements.

    1) controlInout is the angle indicated by the arduino device?

    2) SmInputComp is the current rotateion of the image?

    3) ArInputComp is the angle from the last rotation value and the current rotation value?

  • edited January 2016
    1. Yes
    2. Yes

    3.Im not sure, i didn't make the equation, i don't know exactly how it works :(

  • Are SmInputComp and ArInputComp used outside the draw() method?

    Working on it :)

  • Thx! :D no its not outside draw :)

  • Answer ✓

    Took me a while because I have been doing other things. The solution is shown below. Obviously I don't have the arduino device to play with so I have simulated it by the mouse position about the window centre (mouse must be inside the red circle to be considered. I have got rid of the variables SmInputComp and ArInputComp and replaced them with currAngle and deltaAngle because these names better represent their meaning.

    The ofiginal sketch measures angles in degrees and I have kept to that practice, although it is more usual to use radians.

    If you have questions about the code ask away :)

    // Dummy variable to simulate arduino device and match
    // exisiting code Included here to get the example working
    float[] controlInput = {0};
    
    // New global variables required
    float currAngle = 0, deltaAngle;
    
    void setup() {
      size(800, 600);
    }
    
    void draw() { 
      background(255);
      // Draw mouse sensitive ellispe
      mouseEllipse();
      // Check for wrap over the 0 degree mark
      if (currAngle > 270 && controlInput[0] < 90)
        deltaAngle = controlInput[0] + 360 - currAngle;
      else if (controlInput[0] > 270 && currAngle < 90) {
        deltaAngle =  -(currAngle + 360 - controlInput[0]);
        println(deltaAngle);
      } else
        deltaAngle = controlInput[0] - currAngle;
      currAngle += deltaAngle * 0.05; // Alter the constant to change easing rate
      currAngle = normaliseAngle(currAngle);
    
      pushMatrix();
      imageMode(CENTER);
      translate(400, 300);
      //rotate((SmInputComp) * TWO_PI/360);
      rotate(radians(currAngle));
    
      //image(img, 0, 0); don't have the image so
      // use this to simulate an image
      stroke(0, 0, 200);
      strokeWeight(2);
      noFill();
      rect(-80, -50, 160, 100);
      fill(0, 0, 255);
      rect(-80, -50, 20, 20);
      // end of image simulator
      popMatrix();
    
      // Now uses mouse position arround centre of screeen
      // to simulate arduino input
      Serial_Loop();
    }
    
    void mouseEllipse() {
      fill(255, 200, 200);
      noStroke();
      ellipse(400, 300, 400, 400);
    }
    
    // This method takes any angle and returns the equivaent 
    // angle in the range 0 to 360 degrees
    float normaliseAngle(float angle) {
      while (angle < 0) angle += 360;
      while (angle > 360) angle -= 360;
      return angle;
    }
    
    // Simulates the input from the arduino
    void Serial_Loop() {
      if (dist(mouseX, mouseY, 400, 300) < 200) {
        // Use mouse position to simulate arduino device
        float a = atan2(mouseY - 300, mouseX - 400);
        if (a < 0) a += TWO_PI;
        controlInput[0] = degrees(a);
      }
    }
    
  • This is exactly what i was looking for! Thank you! :D

Sign In or Register to comment.