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 › interpreting mouseX,mouseY after translate+scale
Page Index Toggle Pages: 1
interpreting mouseX,mouseY after translate+scale (Read 574 times)
interpreting mouseX,mouseY after translate+scale
Aug 19th, 2006, 7:59am
 
Hi,

If I call "translate(TX,TY)" and "scale(SX,SY)",
then get the cursor coordinate (mouseX,mouseY),
then try to draw a circle at that position, ...
the circle is NOT at the cursor position.

I think this is because mouseX,mouseY might be raw screen coordinates.

What is the proper way to read the cursor position
and interpret its coordinates properly?

thanks in advance!!

David Jones
Re: interpreting mouseX,mouseY after translate+sca
Reply #1 - Aug 19th, 2006, 4:01pm
 
Are you sure the problem is not that the value from the mouse are right but your are drawing after having used (translate and scale). So also if you get the right values your draws won't be where the mouse visually is. So your problem maybe is in handling the transformations. Have a look to popMatrix and pushMatrix in this case. And a lot of practice could help to refine your technique on handling coordinates.
Re: interpreting mouseX,mouseY after translate+sca
Reply #2 - Aug 20th, 2006, 12:00am
 
I should give more details.

I realize I could use pushMatrix/popMatrix to allow me to draw using un-transformed screen coordinates.

However, I want to interpret the cursor/mouse position in the same coordinate system as the other objects I have already drawn.

I am using the traer physics ParticleSystem.
( http://www.cs.princeton.edu/~traer/randomarboretum/ )
In that example, translate/scale is used to make the drawing of all the particles fill the screen area.

I want to have a "particle" associated with the mouse, and as I move the mouse around, it will interact with the other particles in the system (through attraction/repulsion/springs).  For this to work, I need to interpret the mouse position in "object" coordinates.  Perhaps I just need to apply the inverse of the "object to screen" transformation.  I tried to do that, but I must have made a mistake, since the mouse particle I drew was never in the proper place, ... though the offset depended on the current translate/scale parameters.

-- David Jones
Re: interpreting mouseX,mouseY after translate+sca
Reply #3 - Aug 20th, 2006, 8:25am
 
There are two ways.

You can take the values that you have and apply the same transformations on the values. So, to translate, subtract your coordinates (dx, dy) and to scale, multipy your coordinates (sx, sy).

Another way is to use screenX and screenY AFTER your matrix transforms.

So, a code example.

Code:

pushMatrix();
translate(20,20);
scale(.2);
//the following values are what's on screen at 0,0,0 now
float scx = screenX(0,0,0);
float scy = screenY(0,0,0);

//draw your thing after the transforms
fill(255,255,0);
ellipse(25,25,10,10);

popMatrix();

//now you can use the values from those transforms
//since they only have meaning OUTSIDE the matrix
translate(scx,scy);

fill(0,255,255);
ellipse(25,25,5,5);
Page Index Toggle Pages: 1