How to rotate an object and then translate it to a fixed point?

I am trying to create a little speedometer for my project, which requires the pointer (a triangle) to rotate depending on a variable. I can rotate it if it is translated to the center, but when I try to move it on to the speedometer dial, it seems a little random. Is there an equation to use to put it back in the same place each time no matter its angle? I've since use a technique that draw a triangle directly at a different angle each time, but the results also seem random. Here's the current code:

PImage speedo; //this is the dial x=-260; y =-155; void setup(){ speedo = loadImage("speedo.png"); speedo.resize(112,64); } void draw(){ angle=robotSpeed/maxSpeed; speedoAngle=angle*PI*(-1); image(speedo,width-150,height-80); //pointer y+=2; stroke(26,172,130); triangle(x,y,x+3,y+30,x-3,y+30);//30 is the value that would need to be modified to rotate correctly, if I understood } Thanks

Tagged:

Answers

  • edited May 2017

    Don't. Translate first and THEN rotate.

    void setup(){
      size(400,400);
    }
    
    void draw(){
      background(0);
      noFill();
      stroke(255);
      translate(200,200);
      rotate(PI+map(mouseX,0,width,0,PI));
      triangle(-10,-10,150,0,-10,10);
      ellipse(0,0,10,10);
    }
    
Sign In or Register to comment.