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 & HelpPrograms › drag and release
Page Index Toggle Pages: 1
drag and release (Read 1106 times)
drag and release
Feb 27th, 2006, 7:11pm
 
hello guys, first post of many (sorry, really interested in learning processing but still in the beginning).

I've been looking throughout the forum but couldn't find a clear answer to my question. I am creating an interface for a MAX patch, with small units of sounds. each unit will be represented by a ball, that will be dragged and released in an environment. once you release, you set the speed of it.

I know how to do it in flash but really want to move to processing, specially because of OSC that allows me to easily talk to MAX. I understand I need to record the x and y position of one frame and compare it to  the other, so I set the vector speed. I tried (adapting my flash skills to processing), but it won't work.

I know it is simple and probably someone answered that before in this forum, I just couldn't find it.

cheers
Re: drag and release
Reply #1 - Feb 27th, 2006, 7:39pm
 
sorry guys, managed to work it out. I am posting the code in case it is useful for someone.


-----

int arena = 420;
int arenatop = 90;
int arenaleft = 90;

int num = 2;
Unit[] units = new Unit[num];

void setup()
{
 size(800, 600);
 noStroke();
 noSmooth();
 units[0] = new Unit(arena+arenaleft-40, arenatop+40, units, 0);
 units[1] = new Unit(arena+arenaleft-40, arenatop+80, units, 1);
 framerate(15);
 
}

void draw()
{
 background(100);
 
 stroke(150);
 fill(0);
 rect(arenatop, arenaleft, arena, arena);
 
 noStroke();
 
 for(int i=0; i<num; i++) {
   units[i].update();
   units[i].draw();
 }  
}

void mousePressed()
{
 for(int i=0; i<num; i++) {
   units[i].pressed();
 }
}

void mouseReleased()
{
 for(int i=0; i<num; i++) {
   units[i].released();
 }
}



class Unit
{
 // Screen values
 float xpos, ypos;
 float old_xpos, old_ypos;
 float vely, velx;
 int size = 20;
 boolean over = false;
 boolean move = false;

 Unit[] friends;
 int me;
 
 // Constructor
 Unit(float x, float y, Unit[] others, int id)
 {
   xpos = x;
   ypos = y;
   old_xpos = x;
   old_ypos = y;
   friends = others;
   me = id;
 }

 void update()
 {
   if(move) {
   
     old_xpos = xpos;
     old_ypos = ypos;    
     ypos = mouseY;
     xpos = mouseX;
     
     velx = (xpos - old_xpos)/2;
     vely = (ypos - old_ypos)/2;    
         
   }


   ypos += vely;
   xpos += velx;



   if( ypos + size/2 > (arena+arenatop) ) {
       ypos = (arena+arenatop) - size/2;
       vely *= -1;
   }

   if( ypos < arenatop+size/2 ) {
       ypos = arenatop+size/2;
       vely *= -1;
   }

   if( xpos + size/2 > (arena+arenaleft) ) {
       xpos = (arena+arenaleft) - size/2;
       velx *= -1;
   }

   if( xpos < arenaleft+size/2 ) {
   xpos = arenaleft+size/2;
   velx *= -1;
   }

   
   
   
   
   
   if((over() || move) && !otherOver() ) {
     over = true;
   } else {
     over = false;
   }
 }
 
 // Test to see if mouse is over this spring
 boolean over() {
   float disX = xpos - mouseX;
   float disY = ypos - mouseY;
   if(sqrt(sq(disX) + sq(disY)) < size/2 ) {
     return true;
   } else {
     return false;
   }
 }
 
 // Make sure no other units are active
 boolean otherOver() {
   for(int i=0; i<num; i++) {
     if(i != me) {
       if (friends[i].over == true) {
         return true;
       }
     }
   }
   return false;
 }

 void draw()
 {
   if(over) {
     fill(153);
   } else {
     fill(255, 255, 255, 50);
   }
   ellipse(xpos, ypos, size, size);
 }

 void pressed()
 {
   if(over) {
     move = true;
   } else {
     move = false;
   }  
 }

 void released()
 {
   move = false;
 }
}


----
Re: drag and release
Reply #2 - Jun 12th, 2006, 7:33am
 
You may be interested in knowing that processing automatically keep track of the previous mouse position, in variables called ( i believe) PMouseX and PMouseY
Re: drag and release
Reply #3 - Jun 12th, 2006, 6:02pm
 
pmouseX and pmouseY actually, and you don't need the othersOver function, as when you click and release the different buttons, it will (according to your code) set the current one active, and if mouse is released, all objects are deactivated..

-seltar
Page Index Toggle Pages: 1