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 › (new)Friction physics Help
Page Index Toggle Pages: 1
(new)Friction physics Help (Read 297 times)
(new)Friction physics Help
Dec 12th, 2008, 12:15pm
 
hi i'm currently woring on a project using light sensors to move a message box. At moment i'm creating a demo using mouse dragged function i have it working to drag the box but can anyone help me. the look im going for is when you release the mouse button i want the box to carry on moving and gradually slow down to a stop. and also how do go about how fast you drag the mouse determines how far the box will travel afterwards.

thanks for any information or help that any one can give.

heres my code so far

float bx;
float by;
int bs = 20;
boolean bover = false;
boolean locked = false;
float bdifx = 0.0;
float bdify = 0.0;
float slowdown= 0.0

void setup()
{
 size(1200, 700);
 bx = width/2.0;
 by = height/2.0;
 rectMode(RADIUS);  
}

void draw()
{
 background(0);
 
 // Test if the cursor is over the box
 if (mouseX > bx-bs && mouseX < bx+bs &&
     mouseY > by-bs && mouseY < by+bs) {
   bover = true;  
   if(!locked) {
   
   
   }
 } else {
   stroke(153);
   fill(153);
   bover = false;
 }
 
 // Draw the box
 rect(bx, by, bs, bs);
}

void mousePressed() {
 if(bover) {
   locked = true;
   fill(255, 255, 255);
 } else {
   locked = false;
 }
 bdifx = mouseX-bx;
 bdify = mouseY-by;

}

void mouseDragged() {
 if(locked) {
   bx = mouseX-bdifx;
   by = mouseY-bdify;
 }
}

void mouseReleased() {
 locked = false;
 
}

Re: (new)Friction physics Help
Reply #1 - Dec 13th, 2008, 2:25pm
 
Code:
float bx;
float by;
int bs = 20;
boolean locked = false;
float bdifx = 0.0;
float bdify = 0.0;

float speedX, speedY;
float accelX, accelY;
boolean bMoving;

void setup()
{
 size(1200, 700);
 bx = width/2.0;
 by = height/2.0;
 rectMode(RADIUS);
}

void draw()
{
 background(0);

 // Test if the cursor is over the box
 if(isMouseOverBox()) {
   stroke(153);
   fill(153);
 } else {
   stroke(200);
   fill(200);
 }

 // Draw the box
 rect(bx, by, bs, bs);
 if (bMoving)
 {
   bx += speedX;
   by += speedY;
   if (!isBoxInArea()) {
// Try one step back
     bx -= speedX; by -= speedY;
     if (!isBoxInArea()) {
// Gone too far,
       // put back in playground
       bx = width/2.0;
       by = height/2.0;
     }
     bMoving = false;
     return;
   }
   float psx = speedX, psy = speedY;
   speedX -= accelX;
   speedY -= accelY;
   // Keep moving until one speed is inverted
   bMoving = sign(psx) == sign(speedX) && sign(psy) == sign(speedY);
 }
}

void mousePressed() {
 if(isMouseOverBox()) {
   locked = true;
   fill(255);
 } else {
   locked = false;
 }
 bdifx = mouseX-bx;
 bdify = mouseY-by;
}

void mouseDragged() {
 if(locked) {
   bx = mouseX-bdifx;
   by = mouseY-bdify;
 }
}

void mouseReleased() {
 locked = false;
 speedX = (mouseX - pmouseX) * 2;
 speedY = (mouseY - pmouseY) * 2;
 accelX = speedX / 10;
 accelY = speedY / 10;
 bMoving = true;
}

boolean isMouseOverBox() {
 return mouseX > bx-bs && mouseX < bx+bs &&
     mouseY > by-bs && mouseY < by+bs;
}

boolean isBoxInArea() {
 return bx > bs && by > bs && bx < width - bs && by < height - bs;
}

int sign(float v) {
 if (v < 0) return -1;
 if (v > 0) return 1;
 return 0;
}
Page Index Toggle Pages: 1