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 › help with motion and detection for shapes
Page Index Toggle Pages: 1
help with motion and detection for shapes (Read 415 times)
help with motion and detection for shapes
May 24th, 2009, 9:07am
 
Hello, I'm trying to make a sort of game where you move around a box on the screen with the arrow keys and whenever any part of the box (specifically the borders since as soon as the shapes touch, i want action to happen) touches any part (again, the border) of a randomly generated circle, the box will speed up and a new circle will appear.  My problem is jumping into that if statement;  how do i set up an if statement to check if any part of the box comes in contact with the circle.  Here is my code thus far, thanks for any and all help.

float xelip, yelip;
boolean wFlag;
float xvel = 0;
float yvel = 0;
float xpos = 400;
float ypos = 400;
//***************************************************
void setup()
{
 size(800, 800);
 background(102);
 smooth();
 rectMode(CENTER);
 rect(xpos,ypos,20,20);
 xelip=random(5, width-5);
 yelip=random(5, height-5);
 
}
//*************************************************
void draw()
{
 if (wFlag == true)
 {
   
     if (keyCode == UP)
     {
       xvel=0;
       yvel=-2;
       move();
     }
     if (keyCode == LEFT)
     {
       xvel=-2;
       yvel=0;
       move();
     }
     if (keyCode == DOWN)
     {
       xvel=0;
       yvel=2;
       move();
     }
     if (keyCode == RIGHT)
     {
       xvel=2;
       yvel=0;
       move();
     }
 }
 display();
}
   
//***************************************************    

void move()
{
   xpos += xvel;
   ypos += yvel;
   if ((xpos+10 >= width) || (xpos-10 <= 0) || (ypos+10 >= height) || (ypos-10 <= 0))
   {
     wFlag=false;
     xpos=400;
     ypos=400;
     display();
   }

}
//************************************************
void display()
{
 background(102);
 rectMode(CENTER);
 rect(xpos,ypos,20,20);
 ellipse(xelip, yelip, 20, 20);
}
//**************************************************
void keyPressed()
{
 
 if (key == CODED)
 {
   if ((keyCode == UP) || (keyCode == LEFT) || (keyCode == DOWN) || (keyCode == RIGHT))
   {
     wFlag = true;
   }
 }
}
Re: help with motion and detection for shapes
Reply #1 - May 25th, 2009, 3:43am
 
The simple answer is that collision detection is non-trivial, unless you're dealing with two circles...  With circles you just have to test whether the distance between the two centre points is less than or equal to the sum of the radius of the two circles:

Code:
dx = X1 - X2;
dy = Y1 - Y2;
distance = sqrt(dx*dx + dy*dy);
if (distance <= radius1 + radius2) {
 // hit!
}


IIRC this is often used as a preliminary test to check whether collision is imminent.  You'd then invoke some more complex code to check for actual collision.  In your case this would need to check whether any point on the rectangle border was within the radius of the circle...

I must admit I have little experience of this, and there seem to be various approaches.  There may be a library to facilitate it in Processing.  If not there seem to be plenty of resources online that could probably be adapted for use in Processing...
Page Index Toggle Pages: 1