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 › translate and mouse position
Page Index Toggle Pages: 1
translate and mouse position (Read 1426 times)
translate and mouse position
Apr 14th, 2010, 4:00am
 
I thought that mouseX and mouseY would refer to the current origin and their values would be "sensitive" to translations, but apparently this is not true.
For example, in the code below I was expecting that the circles were drawn in the mouse position. What am I doing wrong?

Code:

void draw() {
translate(100,100);

float x = mouseX;
float y = mouseY;

fill(200,0,200);
ellipse(x, y, 3, 3);
}
Re: translate and mouse position
Reply #1 - Apr 14th, 2010, 4:39am
 
Don't put the translate in the draw loop......
Re: translate and mouse position
Reply #2 - Apr 14th, 2010, 4:47am
 
Hi,

To use the translation method is best to wrapped inside the push and pop Matrix like this:
Code:
pushMatrix();
translate(x, y);
//draw here
popMatrix();


So in your code try this:
Code:
pushMatrix();
translate(mouseX, mouseY);
fill(color);
ellipse(0, 0, 3, 3);
popMatrix();


on the other hand you don't necessarily need to use the translate method to achieve your objective.

Hope that make sense

Cheers
rS
Re: translate and mouse position
Reply #3 - Apr 14th, 2010, 4:51am
 
The mouseX/Y values are indeed independent of the current transformations, that's the coordinates given in the ellipse() call which follow these transformations.
Re: translate and mouse position
Reply #4 - Apr 14th, 2010, 5:05am
 
PhiLho  wrote on Apr 14th, 2010, 4:51am:
The mouseX/Y values are indeed independent of the current transformations, that's the coordinates given in the ellipse() call which follow these transformations.


OK, this confirms what I was suspecting. Thanks for clarifying it.

I find this a bit annoying, but there must be a good reason for not having the same coordinates for the mouse and for what we draw.
Re: translate and mouse position
Reply #5 - Apr 14th, 2010, 5:12am
 
martin_p wrote on Apr 14th, 2010, 4:39am:
Don't put the translate in the draw loop......


If I don't, there will be no translation...

Re: translate and mouse position
Reply #6 - Apr 14th, 2010, 5:51am
 
Unless you put in the setup() Smiley
If you put it in the draw loop I don't think you are going to get the behavior you expected, nardove is trying to give you a steer.
Re: translate and mouse position
Reply #7 - Apr 14th, 2010, 6:33am
 
martin_p, the transformation matrix is reset each time draw() is called, so the translates won't be cumulative there.
Re: translate and mouse position
Reply #8 - Apr 14th, 2010, 7:14am
 
Its not that I'm really thinking of, I guessed that the translate is probably spurious and unnecessary (especially for each loop). I've been trying to think what he's trying to do, which is I presume to start with an ellipse in the center, that can picked up and moved. In the OO world you would create a nice ellipse object initialized in setup. Which is then controlled by the mouse in draw, with or without redrawn background, depending on whether you wanted a persistent image?
If I'm right in my assumption a solution would be to initialize x and y to the center instead, draw the ellipse, and on a boolean (mouse clicked for example) turn on the mouse tracking (but it is still a bit of mess as it depends where your mouse is). To make it less jumpy you could use a calculated displacement rather than the actual mouse coordinates for the movement.
An even better solution use the mouse dragged function to control the ellipse. To make sure x and y are visible between setup and draw, declare them before draw.
Quote:
float x;
float y;

void setup(){
  x = width/2;
  y = height/2;
  fill(200,0,200);
}

void draw(){
  ellipse(x, y, 3, 3);
}

void mouseDragged(){
  y = mouseY;
  x = mouseX;
}



Re: translate and mouse position
Reply #9 - Apr 14th, 2010, 11:26am
 
martin_p wrote on Apr 14th, 2010, 7:14am:
(...) I've been trying to think what he's trying to do, which is I presume to start with an ellipse in the center, that can picked up and moved. (...)



I realize that I should have been more clear with my first question.

I just wanted to clarify, in generic terms, whether the mouse position was affected by a translation. The example I gave intended to illustrate a situation where the phenomenon was visible. Just that...

As I am new at the forum it was a (pleasant) surprise to understand the interesting and detailed comments to my question. I apreciate all them. Thanks a lot!

This seems to be really an excellent place to learn Smiley I have to come back more often.

Cheers!


Page Index Toggle Pages: 1