Mapping angle

Hello

Am sure there is a simple solution to this but can't get my poor little head around it... Basically I am to get the angle of an object in relation to a central point. That part is fine...

The problem is the mapping of the rotation. It has 0 degrees at 9 O'clock and I need it to register 0 degrees at 6 O'clock. Little diagram and the code below, just pick up the ellipse and move around (angle outputting to console). Thanks for any help you can offer..

roatation map image

float centreX, centreY;

void setup() {

  size(600, 600);
  smooth();

  centreX = width/2;
  centreY = height/2;

  initChannels();
}

void draw() {

  background(200);

  noFill();
  ellipse(centreX, centreY, 20, 20);
  ellipse(centreX, centreY, 500, 500);


  //update objects
  for (int i = 0; i < channels.length; i++) {
    channels[i].update();
    channels[i].display();
  }
}

void mousePressed() {

  for (int i = 0; i < channels.length; i ++ ) {
    channels[i].mousePressed();
  }
}

void mouseDragged() {

  if (draggedObj != null) {
    draggedObj.mouseDragged();
  }
}

void mouseReleased() {

  draggedObj = null;
} 

//----------------------------------------------------//
//***** Object stuff *****//
//----------------------------------------------------//

int numChannels = 1; //number of channel objects to create
Channel[] channels;
Channel draggedObj;

void initChannels() {

  channels = new Channel[numChannels];
  for (int i = 0; i < channels.length; i++) {
    channels[i] = new Channel(i);
  }
}

class Channel {

  float x, y, w;
  float ang = random(360);
  float radius = random(200);

  Channel(int chan) {

    x = 150;
    y = centreY;
    w = 20;
  }

  void update() {

    //*** Work out angle and map to positive ***//
    ang = atan2(y - centreY, x - centreX);
    ang = degrees(ang);
    ang = map(ang, -180, 180, 0, 360);

  }

  void display() {

    fill(255);
    ellipse(x, y, w, w);
  }

  void mousePressed() {
    if (isMouseOver()) draggedObj = this;
  }

  void mouseDragged() {
    if (draggedObj != this) return;

    x = mouseX;
    y = mouseY;
    println(ang);
  }

  boolean isMouseOver() {
    return (mouseX > (x-w) & mouseX < (x+w) & mouseY > (y-w) & mouseY < (y+w));
  }

}

Answers

  • _vk_vk
    edited May 2015 Answer ✓

    Hello, at first look I thought it should be easier than it turned out to be. I came up with a weird solution, probably stupid, but looks like it's working... Let's wait for better solutions, in meanwhile...

     //*** weird workaround to rotate coordinates ***//    
    
        // get angle to the desired position
        float temp_ang = atan2(y - centreY, x - centreX);
    
        //use it to plot the position one quarter forward in circle
        PVector shift = new PVector(cos(temp_ang+HALF_PI), sin(temp_ang+HALF_PI));
    
        //work with this position...
        ang = atan2(shift.y, shift.x);
        ang = degrees(ang);
        ang = map(ang, -180, 180, 0, 360);
    
  • Thank you _vk

    Weird solution is working for me :) I know what you mean about appearing like a more simple problem, I had a feeling I was missing something really obvious. Maybe I still am...

    Works well enough for what I need anyway so thanks.

Sign In or Register to comment.