Make two characters change color after colliding

I want to make an animation to persuade people to get vaccinated for a school project. I want when a “person” that has red head which indicates that he is not vaccinated, gets in touch with the vaccine change his color to green

class Vaccine
{
float x, y, size, speedX, speedY;
PImage imageVaccine;

Vaccine(float xx, float yy, float vs, float spX, float spY)
{
x = xx;
y = yy;
speedX = spX;
speedY =spY;
imageVaccine = loadImage(“vaccine.png”);
size = 50;
}
void display()
{

image(imageVaccine, x, y, size, size);

}
void moveLeft()
{
x = x - speedX;

}

void moveRight()
{
x = x + speedX;

}

void moveUp()
{
y = y - speedY;
}

void moveDown()
{
y = y + speedY;
}
float getX()
{
return x;
}
float getY()
{
return y;
}
float getSize()
{
return size;
}
void setRed(int r)
{
r = 0;
}
void setGreen(int g)

{

g=255;
}
void setBlue(int b)
{
b=0;
}

class People
{
  color c;
  float xpos;
  float ypos;
  float size;
  boolean vstatus;
  int red, green, blue;
  People ( boolean vs, float x, float y, int s, int r, int g, int b)
  {
    vstatus=vs;
    xpos = x;
    ypos = y;
    size = s;
    green = g;
    red = r;
    blue = b;
   

 
 
  if (vs==true) {c=color (0,240,0);}
    else {c=color (250,0,0);}
    
    xpos=x;
    ypos=y;
    size=s;
  }
  
  void display()
{
  fill(red,green,blue);
ellipse(xpos,ypos,size,size);
ellipse(xpos,ypos,size,size);
}

 float getX()
  {
    return xpos; 
  }
  float getY()
  {
    return ypos; 
  } 
  float getSize()
  {
    return size; 
  }
  
}
   void setRed(int r)
  {
    r = 0;
  }
  void setGreen(int g)
 
 {
 
  g=255;
  }
  void setBlue(int b)
{
b=0;  
}
Vaccine v;
People p1, p2;

void setup()
  {
  size(800,800);
  v= new Vaccine(100,300,400,15,5);
   p1 = new People(false,255,200,150,0,240,0);
  p2 = new People(true,585,200,150,250,0,0);
  }


void draw()
  {
  background(255);
 v.display();
  p1.display();

  p2.display();
   if(dist(v.getX(),v.getY(),p2.getX(),p2.getY()) <= v.getSize()/2+p2.getSize()/2 )
   {
     p2.setRed(0);
     p2.setGreen(255);
     p2.setBlue(0);
  }
  }

void keyPressed() 
{
  if (key == CODED) 
  {
    if (keyCode == LEFT) 
    {
      v.moveLeft();    
    } 
    else if (keyCode == RIGHT) 
    {
      v.moveRight();
    } 
  }

  if (key == CODED) 
  {
    if (keyCode == UP)
    {
      v.moveUp();   
    } 
    else if (keyCode == DOWN)
    {
      v.moveDown();
    } 
  }

Hi @mpsch,

Welcome to the forum! :wink:

Please format your code by using the </> button when editing a message or putting three backticks around your code like this: ``` code ``` → code.

In your post, there are three main things to solve:

  • Storing the color information for each person (or if the person is vaccinated or not…)
  • Detecting a collision between the vaccine and a non vaccinated person
  • Change the color of that person

Could you tell us what you have so far and what is not working?

1 Like

class Vaccine
{
float x, y, size, speedX, speedY;
PImage imageVaccine;

Vaccine(float xx, float yy, float vs, float spX, float spY)
{
x = xx;
y = yy;
speedX = spX;
speedY =spY;
imageVaccine = loadImage(“vaccine.png”);
size = 50;
}
void display()
{

image(imageVaccine, x, y, size, size);
}
void moveLeft()
{
x = x - speedX;

}

void moveRight()
{
x = x + speedX;

}

void moveUp()
{
y = y - speedY;
}

void moveDown()
{
y = y + speedY;
}
float getX()
{
return x;
}
float getY()
{
return y;
}
float getSize()
{
return size;
}
void setRed(int r)
{
r = 0;
}
void setGreen(int g)

{

g=255;
}
void setBlue(int b)
{
b=0;
}

class People
{
  color c;
  float xpos;
  float ypos;
  float size;
  boolean vstatus;
  int red, green, blue;
  People ( boolean vs, float x, float y, int s, int r, int g, int b)
  {
    vstatus=vs;
    xpos = x;
    ypos = y;
    size = s;
    green = g;
    red = r;
    blue = b;
   

 
 
  if (vs==true) {c=color (0,240,0);}
    else {c=color (250,0,0);}
    
    xpos=x;
    ypos=y;
    size=s;
  }
  
  void display()
{
  fill(red,green,blue);
ellipse(xpos,ypos,size,size);
ellipse(xpos,ypos,size,size);
}

 float getX()
  {
    return xpos; 
  }
  float getY()
  {
    return ypos; 
  } 
  float getSize()
  {
    return size; 
  }
  
}
   void setRed(int r)
  {
    r = 0;
  }
  void setGreen(int g)
 
 {
 
  g=255;
  }
  void setBlue(int b)
{
b=0;  
}
Vaccine v;
People p1, p2;

void setup()
  {
  size(800,800);
  v= new Vaccine(100,300,400,15,5);
   p1 = new People(false,255,200,150,0,240,0);
  p2 = new People(true,585,200,150,250,0,0);
  }


void draw()
  {
  background(255);
 v.display();
  p1.display();

  p2.display();
   if(dist(v.getX(),v.getY(),p2.getX(),p2.getY()) <= v.getSize()/2+p2.getSize()/2 )
   {
     p2.setRed(0);
     p2.setGreen(255);
     p2.setBlue(0);
  }
  }

void keyPressed() 
{
  if (key == CODED) 
  {
    if (keyCode == LEFT) 
    {
      v.moveLeft();    
    } 
    else if (keyCode == RIGHT) 
    {
      v.moveRight();
    } 
  }

  if (key == CODED) 
  {
    if (keyCode == UP)
    {
      v.moveUp();   
    } 
    else if (keyCode == DOWN)
    {
      v.moveDown();
    } 
  }

Hello and thanks for trying to help me! I am trying to make a person that is not vaccinated with a red head change his Color. I have used the dist method to make that possible. More specifically I made a vaccine that the user is able to move with the arrow keys. When the vaccine collides with the non vaccinated person I want his Color to change from red to green which indicates that he got vaccinated. I tried to do this but when I put the p2.setRed(0), p2.setGreen (255) etc it doesn’t work and I don’t know how to fix that

1 Like

You named the variables something else! The variables that handle colors in the People class are called red, green, and blue!

Please note that r, g, and b are destroyed once the class’ constructor is done working.

Also, red(), green() and blue() are functions in Processing. Please try to name your variables something else, if possible - to avoid ‘confusion’ (though it isn’t confusing, this is probably a good programming practice…).

1 Like

I got really confused how should I name these variables and where is the mistake in my code?How can I correct it?

The People method inside the People class is a constructor - constructors have the same name as the class.

People ( boolean vs, float x, float y, int s, int r, int g, int b)
  {
    vstatus=vs;
    xpos = x;
    ypos = y;
    size = s;
    green = g;
    red = r;
    blue = b;
   

 
 
  if (vs==true) {c=color (0,240,0);}
    else {c=color (250,0,0);}
    
    xpos=x;
    ypos=y;
    size=s;

Here, you made the variables g, r and b as arguments to the method. However, these variables go “out of scope” later, so you cannot use them in the class’ methods.

You instead saved their values in the variables green, red and blue, which are a part of the class. Use them instead!

About the naming part - name them whatever you like - how about p_Color or just pColor? You decide that as the programmer!
It’s not necessary to do so, though. I told you to do this only because Processing already has something (There are methods called that. Also, you have a variable called ‘size’, which is another Processing method!) called the same, which could get confusing later - therefore the suggestion. It will still work with the same names since there is of course, difference between a variable and a method, and so you may ignore this suggestion.

(The problem occurs when you get to languages such as JavaScript, where you often need to write a function’s ‘identifier’/name without the brackets, and if you were to name a variable the same name as another function', it would change the function to a variable instead! Which makes it impossible to call it again, without redefining it.)

2 Likes

Yeah, instead of r,g,b you can also just use data type
color

One of the purposes of the constructor is to copy the parameters into the class’s own variable so that their scope is the entire class (and not only the constructor)

The constructor instantiates one object from the class.

1 Like

For example here: r is the parameter

The name that is used in the entire class is red, not r.

So change to red=r;

Copying the parameter r into the class’s variable red which is used in the entire class.