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 › Calculating drag distance
Page Index Toggle Pages: 1
Calculating drag distance (Read 1458 times)
Calculating drag distance
Mar 14th, 2010, 6:11am
 
Hallo,

I`m struggling with the following problem:

I need to measure the distance from the x,y where the mouse has been pressed and the x,y where it has been released.  I`ve tried with mousePressed() & mouseReleased() to grab the values and another function to the maths, but then I cant  use the whole action at mouseClick.
I`ve tried with mouseDragged() - there I cant take the values for pressed and released.

I want to use this distance as a radius for a sphere. You click&drag and  when you release a sphere is built.

Any suggestions? Thank you in Advance!
Re: Calculating drag distance
Reply #1 - Mar 14th, 2010, 8:07am
 
fankof wrote on Mar 14th, 2010, 6:11am:
Hallo,

I`m struggling with the following problem:

I need to measure the distance from the x,y where the mouse has been pressed and the x,y where it has been released.  I`ve tried with mousePressed() & mouseReleased() to grab the values and another function to the maths, but then I cant  use the whole action at mouseClick.


I would have thought this approach would work perfectly well.  Why didn't it work for you  It would help if you posted your code Wink
Re: Calculating drag distance
Reply #2 - Mar 14th, 2010, 12:17pm
 
yep, sounds like the right approach, probably just a little problem with your code.
Re: Calculating drag distance
Reply #3 - Mar 15th, 2010, 10:05am
 
What you need:

1. sketch-level ("global") variables/object(s) to remember the starting coordinates, ending coordinates, and maybe the radius
2. when mouse is pressed - eg, in mousePressed() - save the mouse coordinates in the place you set up in (1).
3. when mouse is released - eg, in mouseReleased() - save the current mouse coordinates and use along with coordinates stored by (2) in (1) to determine the total drag distance; save this as your radius if you need to, or just "build a sphere" right now

Extra things to think about:
- When you "build a sphere", can you build a second and third and so on? Do you need to remember where all the spheres are?
- Maybe it is a good idea to draw the sphere centred on the starting coordinates, not the ending coordinates


-spxl
Re: Calculating drag distance
Reply #4 - Mar 17th, 2010, 4:28am
 
fankof wrote on Mar 14th, 2010, 6:11am:
I`ve tried with mouseDragged() - there I cant take the values for pressed and released.

You can do it this way:

Code:

int mouseDragX, mouseDragY;

void mousePressed() {
 mouseDragX = mouseDragY = 0;  // The drag starts here
}

void mouseDragged() {
 mouseDragX += (mouseX - pmouseX);
 mouseDragY += (mouseY - pmouseY);
}

void mouseReleased() {
 // Don't need to really do anything, but at this point,
// mouseDragX and mouseDragY should equal the
// total X and Y distances dragged.
}


(I agree with the posters above that your other method is sound as well.  Either way should be doable.)

Re: Calculating drag distance
Reply #5 - Mar 18th, 2010, 12:15am
 
Thanks Smitty for the pmouseX/Y approach. I've never used it like that and was never quite sure what "last" mouse position in pmouseX/Y means. From your code it becomes clearer now. Am I right in assuming that whenever the "mouseDragged" event is updated, the pmouse field gets updated at the same moment? (This is mainly important for my sketches where I only use the pmouse coordinates but no mouse functions.) Is this done "once a frame" in processing?
Re: Calculating drag distance
Reply #6 - Mar 18th, 2010, 9:23am
 
Bejoscha wrote on Mar 18th, 2010, 12:15am:
Thanks Smitty for the pmouseX/Y approach. I've never used it like that and was never quite sure what "last" mouse position in pmouseX/Y means. From your code it becomes clearer now. Am I right in assuming that whenever the "mouseDragged" event is updated, the pmouse field gets updated at the same moment (This is mainly important for my sketches where I only use the pmouse coordinates but no mouse functions.) Is this done "once a frame" in processing

I can't prove that it is, but I've always assumed it's done that way and never found reason to believe otherwise.

Glad to be of help!
Re: Calculating drag distance
Reply #7 - Mar 20th, 2010, 7:55am
 
Hallo guys and thank you for your posts. I was away for some days and now I`m back to business working on my spheres.

Well yes, my idea is to click & drag the mouse and when released to call a function that creates a sphere at the position where the mouse has been pressed, with a radius of the dragged distance. Simple at first glance.

I`ll start straight away with your suggestions ! Much appreciated !!
Page Index Toggle Pages: 1