FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   Input 07:mouse function problem
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Input 07:mouse function problem  (Read 676 times)
pwang

WWW Email
Input 07:mouse function problem
« on: Feb 22nd, 2003, 11:19pm »

In "Input 07 : mouse function" example, I found sometimes I cannot throw the square as wish. I wrote println() in mouseReleased() function as follows, and I found that sometimes we just get mouseX - pmouseX = 0.
 
//---------------------------------------------------
void mouseReleased() {  
  if(locked) {  
    bxdir = mouseX - pmouseX;  
    bydir = mouseY - pmouseY;  
    bspeed = abs(sqrt(sq(mouseX-pmouseX)+sq(mouseY-pmouseY)))/2;  
    if (bspeed > maxspeed) { bspeed = maxspeed; }  
    print("mouseX - pmouseX = " + (mouseX - pmouseX));
    println(" & mouseY - pmouseY = " + (mouseY - pmouseY));
  }  
  locked = false;  
}  
//---------------------------------------------------
 
I guess it's the same problem as Ryan posted in "pmouseX & Y " topic.
 
 
benelek

35160983516098 WWW Email
Re: Input 07:mouse function problem
« Reply #1 on: Feb 23rd, 2003, 12:56am »

that may also be you letting go of the mouse button only once uve stopped the mouse... but anywho, there's another problem with that example: if u drag the square out of the applet's area, it just follows you off the screen area.
 
pwang

WWW Email
Re: Input 07:mouse function problem
« Reply #2 on: Feb 23rd, 2003, 3:15am »

I made 2 tricks to fix the problem I mentioned above.
take a look by this link  
 
http://pratt.edu/~pwang/proce55ing/mouse_functions/index.htm
 
Maybe somebody could figure out the problem by this example
« Last Edit: Feb 23rd, 2003, 3:24am by pwang »  
benelek

35160983516098 WWW Email
Re: Input 07:mouse function problem
« Reply #3 on: Feb 23rd, 2003, 5:58am »

mm, reliable velocities...
 
but it doesnt solve the problem of simply releasing the square off-screen (while the mouse isnt in motion).
 
fry


WWW
Re: Input 07:mouse function problem
« Reply #4 on: Feb 24th, 2003, 6:32am »

pmouseX isn't gonna be enough information to make this work. it gets updated as soon as anything at all happens with the mouse, so it's not the right thing to use if you want that much control over things.
 
if you were to store the last few pmouseX-mouseX amounts, you'd be in better shape. i.e.
 
Code:
int deltaIndex = 0;
float deltaX[] = new float[5];
float deltaY[] = new float[5];
 
void mousePressed() {
  for (int i = 0; i < 5; i++) {
    deltaX[i] = 0;
    deltaY[i] = 0;
  }
}
 
void mouseDragged() {
  deltaX[deltaIndex] = pmouseX - mouseX;
  deltaY[deltaIndex] = pmouseY - mouseY;
  deltaIndex++;
  if (deltaIndex == 5) deltaIndex = 0;
}
 
void mouseReleased() {
  float dx = deltaX[0] + deltaX[1] .. etc
  // sum things up and do something fancy
}

 
(moved to programming questions.. this doesn't look like it's a bug..)
« Last Edit: Feb 24th, 2003, 6:33am by fry »  
REAS


WWW
Re: Input 07:mouse function problem
« Reply #5 on: Feb 24th, 2003, 7:18pm »

if anyone would like to fix the example using suggestions from Po_Sheng_Wang and fry i'm happy to update the examples and give credit where it's due.
 
fry


WWW
Re: Input 07:mouse function problem
« Reply #6 on: Feb 24th, 2003, 11:53pm »

fwiw, i don't know if it's necessarily a fault of the example, since i think it's successful in the sense of getting the basic movement to work and it gets the idea across.
 
however, it'd be nice if someone were to make a slightly more expanded example as a 'next step' to complement the more basic version (which is more appropriate for beginners). this second example could incorporate smoother mouse movement (the code i wrote above) but it would also be nice if someone were to expand on the visuals and interaction as well (not too much, just enough to keep it example length )
 
benelek

35160983516098 WWW Email
Re: Input 07:mouse function problem
« Reply #7 on: Feb 25th, 2003, 3:55am »

here ya go.
 
Code:

//Mouse functions (Input07)
//   created for Proce55ing.net, Feb 25th 2003.
//   modified by Bnlk from original example by REAS.
 
 
int[] avX = new int[25]; //avX and avY should be the same length.
int[] avY = new int[25];
int velocityX=0;
int velocityY=0;
 
boolean active=false; //whether or not the square should be moving.
boolean mouseOn=false; //whether or not the mouse is down on the square.
boolean mouseOver=false; //whether or not the mouse if over the square.
int radiusX=15; //radius of square.
int radiusY=15;
int posX; //position of square.
int posY;
 
void setup() {
  size(300,300);
  background(200);
  rectMode(CENTER_DIAMETER);
  fill(153);
  posX=width/2;
  posY=height/2;
  for(int j=0; j<avX.length; j++) {
    avX[j]=0;
    avY[j]=0;
  }
}
 
void loop() {
  if(active==true) { //if square is allowed to move.
    tempVelocity();
    avVelocity();
     
    if(mouseOn==true) { //move square with mouse.
 posX=mouseX;
 posY=mouseY;
 if(mouseX>width) { posX=width; }
 if(mouseX<0) { posX=0; }
 if(mouseY>height) { posY=height; }
 if(mouseY<0) { posY=0; }
    } else { //move square without mouse.
 posX+=velocityX;
 posY+=velocityY;
 if(posX>width) { posX=0; }
 if(posX<0) { posX=width; }
 if(posY>height) { posY=0; }
 if(posY<0) { posY=height; }
    }
     
    for(int j=avX.length-1; j>0; j--) { //bump velocities down the arrays.
 avX[j]=avX[j-1];
 avY[j]=avY[j-1];
    }
     
    if(mouseOn==false && velocityX==0 && velocityY==0) {
 active=false;
    }
  }
  drawSqr();
}
 
void mouseMoved() {
  if(mouseX>posX-radiusX && mouseX<posX+radiusX && mouseY>posY-radiusY && mouseY<posY+radiusY) {
    mouseOver=true;
  } else { mouseOver=false; }
}
 
void mousePressed() {
  if(mouseOver==true) {
    mouseOn=true;
    active=true;
  }
}
 
void mouseReleased() {
  mouseOn=false;
}
 
 
void drawSqr() {
  if(mouseOn==true || mouseOver==true) {
    stroke(255);
  } else {
    stroke(153);
  }
  int sqrWidth = 2*(radiusX + 2*abs(velocityX));
  int sqrHeight = 2*(radiusY + 2*abs(velocityY));
  rect(posX, posY, sqrWidth, sqrHeight);
}
 
void tempVelocity() {
  if(mouseOn==true) {
    avX[0] = mouseX-pmouseX;
    avY[0] = mouseY-pmouseY;
  } else {
    avX[0] = 0;
    avY[0] = 0;
  }
}
 
void avVelocity() {
  velocityX=1;
  velocityY=1;
  for(int j=0; j<avX.length; j++) {
   velocityX+=avX[j];
   velocityY+=avY[j];
  }
  velocityX = velocityX/avX.length;
  velocityY = velocityY/avY.length;
}
 
Pages: 1 

« Previous topic | Next topic »