We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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..
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
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...
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.