We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Trigonometric knob
Page Index Toggle Pages: 1
Trigonometric knob (Read 1863 times)
Trigonometric knob
Feb 24th, 2009, 9:40pm
 
hi,
im creating a knob wich would increment with the when you dragged on it. I made a simple ellipse with a line to take it easy, and now im stuck with the atan2 and my mouse coordinate... i know about radians and trigonometric laws but when i try it the angle is given by my atan2 does not correspond with the angle i need... The other thing i wasn't sure about it if i should use a mousePressed or a mouseDragged function because i need it to update when i click on it too. anyway.. here is my code, have a look please;

float px,py;
float radius = 100;
float angle = 0;
float x = 200, y = 200;
void setup() {
 size(400,400);
 ellipseMode(CENTER);
}
void draw() {
px = x + cos(degrees(angle))*(radius/2);
py = y + sin(degrees(angle))*(radius/2);
 ellipse(x,y,radius,radius);
 line(x,y,px,py);
}
void mouseDragged() {
if(mouseX >= x-radius/2 && mouseX <= x+radius/2 && mouseY >= y-radius/2 && mouseY <= y+radius/2) {
angle = atan2(mouseY,mouseX);
}
}

Thanks for answers,
Odin
Re: Trigonometric knob
Reply #1 - Feb 24th, 2009, 10:08pm
 
Replace "degreees(angle)" with just "angle".
Sin() and Cos() accept radians as input.


Also, atan2 needs to be offset by the center of your circle:
angle = atan2(mouseY-y, mouseX-x);


Oh, and to do the same when clicking, just copy from mouseDragged() to mouseReleased().
Re: Trigonometric knob
Reply #2 - Feb 24th, 2009, 10:29pm
 
Thanks you very much it work great and you replied fast Cheesy
Re: Trigonometric knob
Reply #3 - Feb 26th, 2010, 3:31pm
 
Hello. I'm having a similar problem where I want the angle added to the knob to be relative (because, let's face it, making it absolute is silly)

However, like all functions based on tangent, there's an asymptote at -PI and +PI. This makes things rather annoying if you cross over the boundary between the bottom left corner to the top right corner.

How does one handle free movement so that you can just keep rotating any way you want?

Code:
float angle = PI;

void setup(){
 size(300,300);
 smooth();
}

void draw(){
 background(200);
 noFill();
 strokeWeight(1);
 ellipse(width/2, height/2, 200,200);
 fill(255);
 strokeWeight(6);
 arc(width/2, height/2, 200,200, 0, angle);
 pushMatrix();
   translate(width/2, height/2);
   rotate(angle);
   line(0,0,100,0);
 popMatrix();
}

void mouseDragged(){
 float thisAngle = atan2(mouseY-height/2, mouseX-width/2);
 float prevAngle = atan2(pmouseY-height/2, pmouseX-width/2);
 angle += thisAngle - prevAngle;
 println(angle);
}


(by the way, i realize that the arc can't render in negatives, so don't worry about that. I'm more concerned for the angle value getting messed up)
Re: Trigonometric knob
Reply #4 - Feb 27th, 2010, 12:29am
 
The "angle" seems fine; it is more the number of revolutions that seems the problem. Do you really mean for the knob values to forever increase (or decrease), or instead always represent a value within a single revolution?

Try this:

Code:
  angle = (angle + thisAngle - prevAngle + TWO_PI) % TWO_PI; 



-spxl
Re: Trigonometric knob
Reply #5 - Feb 27th, 2010, 12:45am
 
To allow looping, try something like this:

Code:
void mouseDragged(){
float thisAngle = atan2(mouseY-height/2, mouseX-width/2);
float prevAngle = atan2(pmouseY-height/2, pmouseX-width/2);
float diff = thisAngle - prevAngle;
println("diff: " + diff);
if (diff >= PI)
{
diff -= TWO_PI;
println("Big positive diff corrected to: " + diff);
}
else if (diff <= -PI)
{
diff += TWO_PI;
println("Big negative diff corrected to: " + diff);
}

angle += diff;
println("New angle: " + angle);
}


-spxl
spxlSuperKnob
Reply #6 - Feb 27th, 2010, 7:45am
 
After more than a little tweaking...

A new SuperKnob class to play with!

OpenProcessing link: spxlSuperKnob

A GUI knob that allows multiple revolutions.

Features:
  • Rotation limited to specified number of revolutions in each direction
  • Revolutions marker with graduated colour indicator
  • Snap to full revolutions (visual cue: changed marker colours)
  • Plays well with other knobs (ie doesn't hog mouseDragged())
  • Resizable


Howzat  Smiley

-spxl
Re: Trigonometric knob
Reply #7 - Feb 28th, 2010, 10:26pm
 
FANCY! Thanks subpixel. You're the freaking man.  Cheesy
Page Index Toggle Pages: 1