使用碰撞来拾取物品

时间:2018-12-10 18:56:21

标签: processing collision-detection

我目前正在尝试在处理过程中制作捕获标记的版本,但遇到冲突问题。老实说,我对碰撞了解不多,我正在尝试捡起敌人的障碍物。我可以从y轴拾取它,但是,如果我在对象上方或下方移动,它仍然会拾取不是我想要的对象。就是这样,我无法弄清楚如何为顶部和底部设置碰撞以拾取物品。

(我希望这是有道理的,与之碰撞并抓取块的代码位于//移动鸡的下方)

目标:可以从侧面,底部或顶部收集物品。收集物品时,它会遵循捡起它的玩家的x和y坐标

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];
 }


}

1 个答案:

答案 0 :(得分:1)

对不起,我只是想出我做错了什么。我不包括播放器1的宽度。包括播放器1的宽度也使顶部和底部的碰撞也起作用。我的一个简单错误是,尽管花费了数小时进行修复,但还是没有发现。这将是完整的结果:

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];
}