Welcome to %s forums

BrainModular Users Forum

Login Register

Angle and distance in an XY Pad

I need help on a Patch
Post Reply
gthibert
Member
Posts: 46
Location: Chicoutimi, Québec
Contact:

Unread post by gthibert » 01 Mar 2011, 00:09

Hi,

It's more a math question than an patching question, but here's the idea.

To control Ambisonic plugins, I want to get the angle (0 to 360) and the distance (0 to 1) of a ball in an XY pad, relative to the center.

Examples :
Ball at the middle = angle of 0, distance of 0.
Ball at upper left corner = angle of 135, distance of 1.
Ball at bottom right corner = angle of 315, distance of 1.

Anyone have an idea ?

- Guillaume
Guillaume Thibert
www.cemproduction.com

User avatar
nay-seven
Site Admin
Posts: 5684
Location: rennes France
Contact:

Unread post by nay-seven » 01 Mar 2011, 08:30

about the distance you can find an example in the addon named Morphusine
the match formula is

distance = /¯ [ (X1-X2)² + (Y1-Y2)²]

i will look if if i find about angle

gthibert
Member
Posts: 46
Location: Chicoutimi, Québec
Contact:

Unread post by gthibert » 01 Mar 2011, 15:09

Thanks !
Guillaume Thibert
www.cemproduction.com

gthibert
Member
Posts: 46
Location: Chicoutimi, Québec
Contact:

Unread post by gthibert » 02 Mar 2011, 19:44

I've found a solution. I don't totaly understand the maths (i've found the formula on the web), but it works.

Here is the script :

Code: Select all

var xIn : Tparameter;
var yIn : Tparameter;
var angleOut: Tparameter;
var distanceOut: Tparameter;

procedure init;
begin  
	xIn := CreateParam('x',ptDataField); SetIsOutput(xIn,false);
	yIn := CreateParam('y',ptDataField); SetIsOutput(yIn,false);
	angleOut := CreateParam('angle', ptDataField ); setIsInput(angleOut,false);
	distanceOut := CreateParam('distance', ptDataField ); setIsInput(distanceOut,false);
end;

function sgn (a : real) : real;
begin
	if a < 0  then  sgn &#58;= -1
		else  sgn &#58;=  1;
end;

function atan2 &#40;y, x &#58; real&#41; &#58; real;
begin
	if x > 0       then  atan2 &#58;= arctan &#40;y/x&#41;
	else if x < 0  then  atan2 &#58;= arctan &#40;y/x&#41; + pi
	else                 atan2 &#58;= pi/2 * sgn &#40;y&#41;;
end;

Procedure Callback&#40;n&#58;integer&#41;;
var x&#58;single;
var y&#58;single;
var angle&#58;single;
var distance&#58;single;
begin
	if &#40;n=xIn&#41; or &#40;n=yIn&#41; then begin
		x &#58;= &#40;getValue&#40;xIn&#41; - 0.5&#41;;
		y &#58;= &#40;abs&#40;getValue&#40;yIn&#41; - 1&#41; - 0.5&#41;;
		
		//angle
		angle &#58;= atan2&#40;x,y&#41; * 180 / pi;
		if angle < 0 then angle&#58;= angle + 360;
		setValue&#40;angleOut, round&#40;angle&#41;&#41;;
		
		//distance
		distance &#58;= sqrt&#40;x*x + y*y&#41;;
		setValue &#40;distanceOut, distance&#41;;
	end;
end;
Guillaume Thibert
www.cemproduction.com

gthibert
Member
Posts: 46
Location: Chicoutimi, Québec
Contact:

Unread post by gthibert » 02 Mar 2011, 20:08

Does anyone knows how I can reverse those formulas ?

Getting X-Y from angle and distance ?

Guillaume
Guillaume Thibert
www.cemproduction.com

User avatar
nay-seven
Site Admin
Posts: 5684
Location: rennes France
Contact:

Unread post by nay-seven » 02 Mar 2011, 20:09

cool, but how do you use it..?
don't have anything from the out of the script...

gthibert
Member
Posts: 46
Location: Chicoutimi, Québec
Contact:

Unread post by gthibert » 02 Mar 2011, 20:18

If you connect the pos-X, pos-Y out of a XY Pad to the in of the script, it should work.

It works on my side...
Guillaume Thibert
www.cemproduction.com

User avatar
nay-seven
Site Admin
Posts: 5684
Location: rennes France
Contact:

Unread post by nay-seven » 02 Mar 2011, 20:42

strange , i see something in , nothing out...

User avatar
senso
Site Admin
Posts: 4424
Location: France
Contact:

Unread post by senso » 02 Mar 2011, 23:59

the script seems to be perfect!
to reverse the formula:

x = distance*cos(angle)
y = distance*sin(angle)

User avatar
nay-seven
Site Admin
Posts: 5684
Location: rennes France
Contact:

Unread post by nay-seven » 03 Mar 2011, 00:42

ok, forget it , was experimenting too much thing in the same time ..
works fine

gthibert
Member
Posts: 46
Location: Chicoutimi, Québec
Contact:

Unread post by gthibert » 03 Mar 2011, 01:59

Thanks Senso,

It works !

The only thing, is that if you want to control angle with a value from 0 to 360 the formula should be :

x = distance * cos(angle/360*pi*2)
y = distance * sin(angle/360*pi*2)
Guillaume Thibert
www.cemproduction.com

Post Reply

Who is online

Users browsing this forum: No registered users and 60 guests