Processing collision rect and circle (PacMan)

So this is my code<3 So my question is how can the main character not be able to go through the “obstacles” as you want to call them. Yes, that, I need collision or something I​ do not know:( thanks a lot<3

Face f1;
Enemy e1,e2,e3,e4;
Message m1, m2, m3, m4;
int currentTime;





void setup()
{
  size (600,600);
  f1=new Face(50,200,300,0,255,0); 
  e1 = new Enemy(200,300,30,-5,255,0,0);
  e2=new Enemy(400,200,40,-8,255,0,0);
  e3=new Enemy(100,500,50,-7,255,0,0);
  e4=new Enemy(550,400,30,-6,255,0,0);
  m1 = new Message(width/2, height/2, 1000,4000, "wear a mask");
  m2 = new Message(width/2, height/2, 5000,9000, "avoid contacts");
  m3 = new Message(width/2, height/2, 10000,14000, "wash your hands");
  m4 = new Message(width/2, height/2, 15000,19000, "stay safe");
 
  
}
 



void draw()
{
  background (0);
  currentTime = millis();
  f1.display();
  e1.move();
  e1.display();
  e2.display();
  e2.move();
  e3.display();
  e3.move();
  e4.display();
  e4.move();
  m1.setTime(currentTime);
  m2.setTime(currentTime);
  m3.setTime(currentTime);
  m4.setTime(currentTime);
  m1.display();
  m2.display();
  m3.display();
  m4.display();
  
fill(35, 20, 219);
  rect(200, 240, 180, 30); 
  rect(0, 0, 600, 10); 
  rect(0, 0, 10, 600); 
  rect(0, 590, 600, 10);
  rect(590, 0, 10, 600);
  rect(40, 50, 50, 20);
  rect(140, 50, 50, 20);
  rect(0, 220, 40, 140);
  rect(0, 400, 40, 20);
  rect(80, 580, 200, 20);
  rect(340, 580, 200, 20);
  rect(240, 500, 150, 20);
  rect(300, 500, 20, 50);
  rect(240, 380, 150, 20);
  rect(300, 380, 20, 70);
  rect(390, 450, 60, 15);
  rect(500, 450, 90, 15);
  rect(500, 480, 20, 50);
  rect(120, 420, 20, 80);
  rect(90, 500, 90, 20);
  rect(280, 0, 20, 60);
  rect(380, 50, 40, 20);
  rect(480, 50, 40, 20);
  rect(480, 280, 20, 100);
  rect(450, 260, 80, 20);
  rect(150, 120, 300, 20);
  rect(275, 140, 25, 40);
  rect(100, 270, 20, 100);fill(35, 20, 219);
  rect(200, 240, 180, 30); 
  rect(0, 0, 600, 10); 
  rect(0, 0, 10, 600); 
  rect(0, 590, 600, 10);
  rect(590, 0, 10, 600);
  rect(40, 50, 50, 20);
  rect(140, 50, 50, 20);
  rect(0, 220, 40, 140);
  rect(0, 400, 40, 20);
  rect(80, 580, 200, 20);
  rect(340, 580, 200, 20);
  rect(240, 500, 150, 20);
  rect(300, 500, 20, 50);
  rect(240, 380, 150, 20);
  rect(300, 380, 20, 70);
  rect(390, 450, 60, 15);
  rect(500, 450, 90, 15);
  rect(500, 480, 20, 50);
  rect(120, 420, 20, 80);
  rect(90, 500, 90, 20);
  rect(280, 0, 20, 60);
  rect(380, 50, 40, 20);
  rect(480, 50, 40, 20);
  rect(480, 280, 20, 100);
  rect(450, 260, 80, 20);
  rect(150, 120, 300, 20);
  rect(275, 140, 25, 40);
  rect(100, 270, 20, 100);
  

  
  
 
  
  if(dist(f1.x, f1.y, e1.x, e1.y) < ( (f1.diameter + e1.diameter) / 2) ){
  noLoop();
  }
  if(dist(f1.x, f1.y, e2.x, e2.y) < ( (f1.diameter + e2.diameter) / 2) ){
  noLoop();
  }
  if(dist(f1.x, f1.y, e3.x, e3.y) < ( (f1.diameter + e3.diameter) / 2) ){
  noLoop();
  }
  if(dist(f1.x, f1.y, e4.x, e4.y) < ( (f1.diameter + e4.diameter) / 2) ){
  noLoop();
  }
  if(dist(f1.x, f1.y, e4.x, e4.y) < ( (f1.diameter + e4.diameter) / 2) ){
  noLoop();
  }
  }
   }
   void keyPressed()
{
  if(key==CODED)
  {
    if(keyCode==LEFT)
    {
     f1.moveLeft();
    }
    if(keyCode==RIGHT)
    {
      f1.moveRight();
    }
    if(keyCode==UP)
    {
     f1.moveUp();
    }
    if(keyCode==DOWN)
    {
     f1.moveDown();
    }
  }
  
 
   }
  ----------------------------------------------------------------------------------------
class Eye
{
  int diameter;
  int x;
  int y;
  int grey;
  
  Eye(int d, int xcor, int ycor, int g)
  {
    grey=g;
    diameter=d;
    x=xcor;
    y=ycor;
  }
  void display()
  {
    fill(grey);
    ellipse(x,y,diameter,diameter);
  }
  int getX()
  {
    return x;
  }
  void setX(int xx)
  {
    x=xx;
 }
   int getY()
  {
    return y;
  }
  void setY(int yy)
  {
    y=yy;
 }
 
 
 
 
}
-------------------------------------------------------------------------------------------
class Enemy
{
  int x;
  int y;
  int diameter;
  int step;
  int r;
  int g;
  int b;
  
  
  Enemy(int a,int b,int c,int d,int rc,int gc,int bc)
  {
    x=a;
    y=a;
    diameter=c;
    step=d;
    r=rc;
    g=gc;
    b=bc;
}
void display()
{ 
  fill(r, g, b);
  stroke(0,0,255);
  ellipse(x,y,diameter,diameter);
  
}

void move()
{
  y=y+step;
  if(y<diameter)
  step=-step;
  if(y>600)
  step=-step;
}
void moveUp()
{ 
  if(y>=5+diameter/2)
  {
    y=y-5;
  }
}
void moveDown()
{
  if(y<=height-5-diameter/2)
  {
    y=y+5;
  }
}
void changeColor()
{
  g=(g+20)%255;
}
}
------------------------------------------------------------

class Face
{
  int diameter;
  int x;
  int y;
  int r;
  int g;
  int b;
  Eye leftEye;
  Eye rightEye;
  
  Face(int d, int xcor, int ycor,int rc,int gc,int bc)
  {
    diameter=d;
    y=ycor;
    x=xcor;
    r=rc;
    g=gc;
    b=bc;
    leftEye=new Eye(10,x-10,y-10,255);
    rightEye=new Eye(10,x+10,y-10,250);
    
    
  }
  
 
  void display()
  {
    fill(255, g, b);
    stroke(255,0,0);
    ellipse(x,y,diameter,diameter);
    leftEye.display();
    rightEye.display();
    
  }
  void moveLeft()
  {
    if(x>diameter/2)
    {
      x=x-10;
      leftEye.setX(leftEye.getX()-10);
      rightEye.setX(rightEye.getX()-10);
   }
  }
  void moveRight()
  {
    if(x<width-diameter/2)
    {
    x=x+10;
      leftEye.setX(leftEye.getX()+10);
      rightEye.setX(rightEye.getX()+10);
    }
  }
  void moveUp()
{ 
  if(y>=5+diameter/2)
  {
    y=y-10;
    leftEye.setY(leftEye.getY()-10);
    rightEye.setY(rightEye.getY()-10);
  }
}
 
    void moveDown()
{
  if(y<=height-5-diameter/2)
  {
    y=y+10;
    leftEye.setY(leftEye.getY()+10);
    rightEye.setY(rightEye.getY()+10);
    
  }
}


  }
  -----------------------------------------------------------------------------
class Message
{
int startTime;
int stopTime;
String str;
int x;
int y;
boolean visible;

Message(int xcor, int ycor, int t1,int t2, String s)
{
  x = xcor;
  y = ycor;
  startTime = t1;
  stopTime = t2;
  str = s;
  visible = false;
}

void display()
{
  textSize(24);
  if(visible == true)
   {
     text(str, x, y);
   }
}
  
void setTime(int time)
{
    if(time>=startTime && time<= stopTime)
    {
      visible = true;   
    }
    else
    {
      visible = false;   
    }
}

}
-------------------------------------------------------------------

Sorry, I do not know why a part of my code is bold:( <3

Welcome. Please format your code using the </> button in the textbox.

you can press the pen button to edit your posts.

1 Like

Great, thanks a lot<3:)

YW. Collisions are kinda straight forward, however I’ll point you to this site as it will explain it better than I could. Hope you are comfortable with some math.
this site has everything you could require for collisions.

also here is an example to show how it works, I’ll leave it to you to adapt it to your purposes.


float radius = 50;
void setup(){
  size(400,400);
};

void draw(){
  
  background(0);
  fill(255);
  rectMode(CENTER);
  rect(width/2,height/2,200,200);
  fill(255,0,0);
  if((mouseX-radius/2<width/2+100
     &&mouseY+radius/2>height/2-100
     &&mouseY-radius/2<height/2+100
     &&mouseX+radius/2>width/2-100))
  fill(0,0,255);
  ellipse(mouseX,mouseY,radius,radius);
};
1 Like

Wow thanks with the advice, although I understand it, I can not understand how this collision is related to mine. It is a circle/rect collision but I do not want the circle to just go through the rectangle. Appreciate it<3

This means the circle hit the rect.

So prior to moving the circle make this test. When it gives you true, do not move the circle.

boolean collide() {
    if((playerX-radius/2<width/2+100
         &&playerX+radius/2>height/2-100
         &&PlayerY-radius/2<height/2+100
         &&PlayerX+radius/2>width/2-100))
      return true;
      else return false; 
}
1 Like

the if statement provides the condition you are looking for, I have simply added logic to change the color when this condition is true. If you want more functionality you will have to think outside the box.


float radius = 50;
void setup(){
  size(400,400);
};

void draw(){
  
  background(0);
  fill(255);
  rectMode(CENTER);
  rect(width/2,height/2,200,200);
  fill(255,0,0);
  if(!(mouseX-radius/2<width/2+100
     &&mouseY+radius/2>height/2-100
     &&mouseY-radius/2<height/2+100
     &&mouseX+radius/2>width/2-100))
  //fill(0,0,255);
  ellipse(mouseX,mouseY,radius,radius);
};
1 Like

Ah, because the walls are blue:

you can also check the color in the target direction of your movement.

When it’s blue, do not move

(You need to take the wall color exactly)

Here is your new moveUp() :

(remove else println("No"); when you clean up your code later)

  void moveUp()
  { 

    if (y>=5+diameter/2    && 
      get(x, y-10-diameter/2) != color(35, 20, 219) )
    {
      y=y-10;
      leftEye.setY(leftEye.getY()-10);
      rightEye.setY(rightEye.getY()-10);
    } else println("No");
  }
1 Like

Guys, I really appreciate the help!! Everything turned out beautifully, and thanks for creating a mindset to me. Many many thanks!!! You guys are awesome, I am really proud of you( idk thats wrong with me:) Yes, basically many thanks to both of you!<3

2 Likes

hi, my FINAL question ever and I will never bother you again about this code. So I’ve putted the moveup addition and I have also putted the movedown addition. Now I have been trying to make the left and right correct code and it doesn’t go, you know, through it. Sorry<3

can you post your entire code please


Face f1;
Enemy e1,e2,e3,e4;
Message m1, m2, m3, m4;
int currentTime;

void setup()
{
size (600,600);
f1=new Face(50,200,300,0,255,0);
e1 = new Enemy(200,300,30,-5,255,0,0);
e2=new Enemy(400,200,40,-8,255,0,0);
e3=new Enemy(100,500,50,-7,255,0,0);
e4=new Enemy(550,400,30,-6,255,0,0);
m1 = new Message(width/2, height/2, 1000,4000, “wear a mask”);
m2 = new Message(width/2, height/2, 5000,9000, “avoid contacts”);
m3 = new Message(width/2, height/2, 10000,14000, “wash your hands”);
m4 = new Message(width/2, height/2, 15000,19000, “stay safe”);

}

void draw()
{
background (0);
currentTime = millis();

f1.display();
e1.move();
e1.display();
e2.display();
e2.move();
e3.display();
e3.move();
e4.display();
e4.move();
m1.setTime(currentTime);
m2.setTime(currentTime);
m3.setTime(currentTime);
m4.setTime(currentTime);
m1.display();
m2.display();
m3.display();
m4.display();

fill(35, 20, 219);
rect(200, 240, 180, 30);
rect(0, 0, 600, 10);
rect(0, 0, 10, 600);
rect(0, 590, 600, 10);
rect(590, 0, 10, 600);
rect(40, 50, 50, 20);
rect(140, 50, 50, 20);
rect(0, 220, 40, 140);
rect(0, 400, 40, 20);
rect(80, 580, 200, 20);
rect(340, 580, 200, 20);
rect(240, 500, 150, 20);
rect(300, 500, 20, 50);
rect(240, 380, 150, 20);
rect(300, 380, 20, 70);
rect(390, 450, 60, 15);
rect(500, 450, 90, 15);
rect(500, 480, 20, 50);
rect(120, 420, 20, 80);
rect(90, 500, 90, 20);
rect(280, 0, 20, 60);
rect(380, 50, 40, 20);
rect(480, 50, 40, 20);
rect(480, 280, 20, 100);
rect(450, 260, 80, 20);
rect(150, 120, 300, 20);
rect(275, 140, 25, 40);
rect(100, 270, 20, 100);fill(35, 20, 219);
rect(200, 240, 180, 30);
rect(0, 0, 600, 10);
rect(0, 0, 10, 600);
rect(0, 590, 600, 10);
rect(590, 0, 10, 600);
rect(40, 50, 50, 20);
rect(140, 50, 50, 20);
rect(0, 220, 40, 140);
rect(0, 400, 40, 20);
rect(80, 580, 200, 20);
rect(340, 580, 200, 20);
rect(240, 500, 150, 20);
rect(300, 500, 20, 50);
rect(240, 380, 150, 20);
rect(300, 380, 20, 70);
rect(390, 450, 60, 15);
rect(500, 450, 90, 15);
rect(500, 480, 20, 50);
rect(120, 420, 20, 80);
rect(90, 500, 90, 20);
rect(280, 0, 20, 60);
rect(380, 50, 40, 20);
rect(480, 50, 40, 20);
rect(480, 280, 20, 100);
rect(450, 260, 80, 20);
rect(150, 120, 300, 20);
rect(275, 140, 25, 40);
rect(100, 270, 20, 100);

if(dist(f1.x, f1.y, e1.x, e1.y) < ( (f1.diameter + e1.diameter) / 2) ){
noLoop();
}
if(dist(f1.x, f1.y, e2.x, e2.y) < ( (f1.diameter + e2.diameter) / 2) ){
noLoop();
}
if(dist(f1.x, f1.y, e3.x, e3.y) < ( (f1.diameter + e3.diameter) / 2) ){
noLoop();
}
if(dist(f1.x, f1.y, e4.x, e4.y) < ( (f1.diameter + e4.diameter) / 2) ){
noLoop();
}
if(dist(f1.x, f1.y, e4.x, e4.y) < ( (f1.diameter + e4.diameter) / 2) ){
noLoop();
}

}

void keyPressed()
{
if(key==CODED)
{
if(keyCode==LEFT)
{
f1.moveLeft();
}
if(keyCode==RIGHT)
{
f1.moveRight();
}
if(keyCode==UP)
{
f1.moveUp();
}
if(keyCode==DOWN)
{
f1.moveDown();
}
}

}

----------------------------------------------------------
class Eye
{
  int diameter;
  int x;
  int y;
  int grey;
  
  Eye(int d, int xcor, int ycor, int g)
  {
    grey=g;
    diameter=d;
    x=xcor;
    y=ycor;
  }
  void display()
  {
    fill(grey);
    ellipse(x,y,diameter,diameter);
  }
  int getX()
  {
    return x;
  }
  void setX(int xx)
  {
    x=xx;
 }
   int getY()
  {
    return y;
  }
  void setY(int yy)
  {
    y=yy;
 }
 
 
 
 
}
-----------------------------------------------------------
class Enemy
{
  int x;
  int y;
  int diameter;
  int step;
  int r;
  int g;
  int b;
  
  
  Enemy(int a,int b,int c,int d,int rc,int gc,int bc)
  {
    x=a;
    y=a;
    diameter=c;
    step=d;
    r=rc;
    g=gc;
    b=bc;
}
void display()
{ 
  fill(r, g, b);
  stroke(0,0,255);
  ellipse(x,y,diameter,diameter);
  
}

void move()
{
  y=y+step;
  if(y<diameter)
  step=-step;
  if(y>600)
  step=-step;
}
void moveUp()
{ 
  if(y>=5+diameter/2)
  {
    y=y-5;
  }
}
void moveDown()
{
  if(y<=height-5-diameter/2)
  {
    y=y+5;
  }
}
void changeColor()
{
  g=(g+20)%255;
}
}
--------------------------------------
class Face
{
  int diameter;
  int x;
  int y;
  int r;
  int g;
  int b;
  Eye leftEye;
  Eye rightEye;
  boolean collide;
  
  Face(int d, int xcor, int ycor,int rc,int gc,int bc)
  {
    diameter=d;
    y=ycor;
    x=xcor;
    r=rc;
    g=gc;
    b=bc;
    leftEye=new Eye(10,x-10,y-10,255);
    rightEye=new Eye(10,x+10,y-10,250);
    
    
  }
  
 
  void display()
  {
    fill(255, g, b);
    stroke(255,0,0);
    ellipse(x,y,diameter,diameter);
    leftEye.display();
    rightEye.display();
    
  }
  void moveLeft()
  {
    if(x>diameter/2)
       
    {
      x=x-10;
      leftEye.setX(leftEye.getX()-10);
      rightEye.setX(rightEye.getX()-10);
      
      
      }
      
   }
  
  void moveRight()
  {
    if(x<width-diameter/2)
    {
    x=x+10;
      leftEye.setX(leftEye.getX()+10);
      rightEye.setX(rightEye.getX()+10);
    }
  }
   void moveUp()
  { 

    if (y>=5+diameter/2    && 
      get(x, y-10-diameter/2) != color(35, 20, 219) )
    {
      y=y-10;
      leftEye.setY(leftEye.getY()-10);
      rightEye.setY(rightEye.getY()-10);
    }
    
  }
  
 
    void moveDown()
{
  if(y<=height-5-diameter/2 &&
   get(x, y+10+diameter/2) != color(35, 20, 219) )
  {
    y=y+10;
    leftEye.setY(leftEye.getY()+10);
    rightEye.setY(rightEye.getY()+10);
    
  }
  
}





  
  


  
  


}

--------------------------------------
class Message
{
int startTime;
int stopTime;
String str;
int x;
int y;
boolean visible;

Message(int xcor, int ycor, int t1,int t2, String s)
{
  x = xcor;
  y = ycor;
  startTime = t1;
  stopTime = t2;
  str = s;
  visible = false;
}

void display()
{
  textSize(24);
  if(visible == true)
   {
     text(str, x, y);
   }
}
  
void setTime(int time)
{
    if(time>=startTime && time<= stopTime)
    {
      visible = true;   
    }
    else
    {
      visible = false;   
    }
}

}
-------------------------------
1 Like

err…

you need the color check in moveLeft and moveRight?

Of course and thanks for not being sarcastic and serious!<3

1 Like

I tried putting again like the moveUp get and the color but nothing

okay,

this

 get(x, y-10-diameter/2) != color(35, 20, 219)

means if the new position (x,y-10) is not blue, then you are allowed to move. The expression color(35, 20, 219) must be the exact wall color.

The new position you test (x,y-10) must match with the new position you are attempting to move to!

it’s hard to tell without seeing your code

I have putted this code to my moveup and then I have changed it like this:

void moveDown()
{
  if(y<=height-5-diameter/2 &&
   get(x, y+10+diameter/2) != color(35, 20, 219) )
  {
    y=y+10;
    leftEye.setY(leftEye.getY()+10);
    rightEye.setY(rightEye.getY()+10);
    
  }

for my movedown. Now I have a problem with the left and right. Would you like for me to sent again the code?<3

You need to test into the direction you want to move

So when it’s x=x+10 you want to check where the new position would be:

  if( .....  &&
   get(x+10+diameter/2 , y) != color(35, 20, 219) )
  {
  

in this case, x changes, y does not

Next case

x=x-10

gives you:

  if( .... &&
   get(x-10-diameter/2, y) != color(35, 20, 219) )
  {
  

[EDITED: the y<=height-5-diameter/2 was wrong. Please use them like they were in your code]

Warm regards,

Chrisir

:wink:

1 Like

I am so sorry about yesterday, but apparently, I have a limited number of comments that I can do and I had reached it. Everything turned out perfect, yes you are correct! I feel privileged to have met you, many thanks<3

1 Like