Need help with dist()

Hello Processing user,

in my program I want that the moveable character be stopped by a wall(a rect) if it collide with it.

int speed = 5;
int xPosChell = width/2;       //Chell(movable Character)
int yPosChell = height/2;


/*int wall1X = 140;
int wall1Y = 120;*/

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
void setup() {    
  size(750, 500); 
}                 

////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

void draw() {
  background(255);             

/*______________________________________________*/

  ellipseMode(CENTER);                  
  ellipse(xPosChell, yPosChell, 25, 25); 
  
/*_____________________________________________*/

  //rect(wall1X, wall1Y, 140, 130);       <------- should be the wall  

/*_____________________________________________*/

  if (keyPressed) {                      
    if (key == 'w' || keyCode == UP) {    
      yPosChell = yPosChell - speed;    
    }                                    
  }                                 

  if (keyPressed) {                   
    if (key == 's' || keyCode == DOWN) {
      yPosChell = yPosChell + speed;  
    }                                   
  }                                   

  if (keyPressed) {                       
    if (key == 'a' || keyCode == LEFT) { 
      xPosChell = xPosChell - speed;      
    }                                     
  }                                     
                                        
  if (keyPressed) {                       
    if (key == 'd' || keyCode == RIGHT) {
      xPosChell = xPosChell + speed;    
    }                                   
  }                                  

/*_____________________________________________*/

  //collide with Edges
  if(xPosChell >= width - 10) {   
    xPosChell = xPosChell - speed; 
  }                            

  if(xPosChell <= 10) {            
    xPosChell = xPosChell + speed; 
  }                               

    if(yPosChell >= height - 10) { 
    yPosChell = yPosChell - speed; 
  }                               

  if(yPosChell <= 10) {        
    yPosChell = yPosChell + speed; 
  }     
  
}
1 Like

Hi,

Welcome to forums. It’s a bit hard to answer such an open question, so what have you so far? It’s much easier and more productive to everybody to answer questions with related code attached.

Please have a look at guidelines for questions Guidelines—Asking Questions

2 Likes

Also, when you know the positions of all wall segments, check the letter prior to moving it whether the new position is allowed

1 Like

So you have code that checks collision with edges. Check of collision with the wall is similar, but you have to check both x and y values. And you have to keep in mind that there are four walls your object can collide to, so four if clauses, right.

Next if clause would check if object is about to hit the left wall. I assumed that coordinates of top left corner of rectangle are wall1X and wall1Y and lower right corner are wall2X and wall2Y

if(yPosChell >= wall1Y && yPosChell <= wall2Y && xPosChell >= wallx1 - 10 && xPosChell <= wallx1) {
   .....
}

First two term check if y-coordinate is between rectangles top and bottom. They tell if object is either on the left side or right side of the rectangle. Last two terms check if object is no more than 10 pixels away on the left side of the rectangle.

I tend to split up those long lines so we can see the pattern (twice yPosChell, then twice xPosChell etc.)

Like here :

if(yPosChell >= wall1Y && 
   yPosChell <= wall2Y && 
   xPosChell >= wallx1 - 10 && 
   xPosChell <= wallx1  ) {
1 Like