user10355389
user10355389

Reputation:

Using collision to pick up items

I am currently working on trying to make a version of capture of flag in processing but I am running into an issue with collisions. In all honesty I don't know much about collisions and I am trying to pickup the enemies blocks. I can pick it up from the y axis, however if I go below or above the object it still will pick the object up which is not what I want. There's that and I cannot figure out how to set up the collision for the top and bottom to pick the item up.

(I hope this make sense, and the code to collide with and grab the blocks is under //move chickens)

Goal: Item can be collected from the side, bottom, or top. When collected the item follows the x and y coordinates of the player who picked it up

int[] player1;
int[] player2;
int[] player1chicken1;
int[] player1chicken2;
int[] player1chicken3;
int[] player2chicken1;
int[] player2chicken2;
int[] player2chicken3;
int p1chicken1width = 50;
int p1chicken1height = 50;
int p1chicken2width = 50;
int p1chicken2height = 50;
int p1chicken3width = 50;
int p1chicken3height = 50;
int p2chicken1width = 50;
int p2chicken1height = 50;
int p2chicken2width = 50;
int p2chicken2height = 50;
int p2chicken3width = 50;
int p2chicken3height = 50;
int p1width = 50;
int p1height = 50;
int p2width = 50;
int p2height = 50;
void setup(){
  size(1200, 600);
  player1= new int[] {250, 250};
  player2= new int[] {950, 250};
  player1chicken1= new int[] {40, 250};
  player1chicken2= new int[] {40, 50};
  player1chicken3= new int[] {40, 500};
  player2chicken1= new int[] {1110, 250};
  player2chicken2= new int[] {1110, 50};
  player2chicken3= new int[] {1110, 500};
}

void draw(){
  //draw players
  fill(0);
  rect(0,0,width/2,height);
  fill(255,0,0);
  rect(600,0,width/2,height);
  //move players
  fill(255);
  rect(player1[0], player1[1], p1width, p1height);
  if (keyPressed) {
    switch(keyCode) {
    case LEFT:
      player1[0] -=10;
      break;
    case RIGHT:
      player1[0] +=10;
      break;
    case UP:
      player1[1] -=10;
      break;
    case DOWN:
      player1[1] +=10;
      break;
    }
  }

  fill(255,255,0);
  rect(player2[0], player2[1], p2width, p2height);
  if (keyPressed) {
    if (key=='a'){
      player2[0] -=10;
      }
    if (key=='s'){
      player2[0] +=10;
      }
    if (key=='w'){
      player2[1] -=10;
      }
    if (key=='d'){
      player2[1] +=10;
      }
    }

   if(player1[0]+25>1150){
     player1[0]-=25;
   }
    if(player1[0]-25<0){
     player1[0]+=50;
   }

    if(player2[0]+25>1150){
      player2[0]-=25;
   }
    if(player2[0]-25<0){
      player2[0]+=50;
   }

   if(player1[1]+25>550){
     player1[1]-=25;
   }
   if(player1[1]-25<0){
     player1[1]+=50;
   }

   if(player2[1]+25>550){
     player2[1]-=25;
   }
   if(player2[1]-25<0){
     player2[1]+=50;
   }
   ////////////////////////////////chickens
   fill(0,0,255);
   rect(player1chicken1[0],player1chicken1[1],
   p1chicken1width,p1chicken1height);

   fill(0,0,255);
   rect(player1chicken2[0],player1chicken2[1],
   p1chicken2width, p1chicken2height);

   fill(0,0,255);
   rect(player1chicken3[0],player1chicken3[1],
   p1chicken3width,p1chicken3width);


   fill(0,0,255);
   rect(player2chicken1[0],player2chicken1[1],
   p2chicken1width,p2chicken1width);

   fill(0,0,255);
   rect(player2chicken2[0],player2chicken2[1],
   p2chicken2width,p2chicken2width);

   fill(0,0,255);
   rect(player2chicken3[0],player2chicken3[1],
   p2chicken3width,p2chicken3width);

   //move chickens
   if(player1[0] < player2chicken1[0] 
   +p2chicken1width && 
   player1[0] + p1width > player2chicken1[0]){
   player2chicken1[0]=player1[0];
 }


}

Upvotes: 0

Views: 38

Answers (1)

user10355389
user10355389

Reputation:

I just figured out what I was doing wrong, sorry. I was not including the width of player 1. including the width of player 1 also made the collision from the top and bottom work too. A simple mistake on my part that I did not catch despite spending hours trying to fix it. Here would be the full result:

if(player1[0] + p1width/2 > 
   player2chicken1[0] - p2chicken1width/2
   && player1[0] - p1width/2 <    
   player2chicken1[0] + p2chicken1width/2
   && player1[1] + p1width/2 > 
   player2chicken1[1] - p2chicken1height/2
   && player1[1] - p1width/2 < 
   player2chicken1[1] + p2chicken1height/2){
       player2chicken1[0]=player1[0];
       player2chicken1[1]=player1[1];
}

Upvotes: 1

Related Questions