碰撞检测然后让玩家停止

Collision detection then get player to stop

我正在使用处理,我想创建一个游戏,其中玩家在一个房间里,如果他们撞到墙,他们会像普通墙一样停止移动。但是我真的不知道该怎么做。使用我目前的方法,一旦它们撞到墙上,它们就不能再在 x 轴上移动。任何帮助将不胜感激。

PVector playerPosition;
PVector barrierPosition;

float velocity = 4;

boolean goLeft=false;
boolean goRight=false;
boolean goUp=false;
boolean goDown=false;

float playerWidth = 10;
float playerHeight = 10;

float barrierWidth = 10;
float barrierHeight = 600;



void setup() {
  size(800,600);
  rectMode(CORNER);
  playerPosition = new PVector(200,200);
  barrierPosition = new PVector(0,0);
}

void draw() {
 background(255); 
 drawPlayer();
 movePlayerPosition();
 drawBarrier();
 checkCollide();



}
  //check for collision with barrier then stop player moving
  void checkCollide() {
   if (playerPosition.y <= barrierHeight + playerHeight && playerPosition.x <= barrierWidth + playerWidth)
   {
     println("COLLIDED");
     playerPosition.x = barrierPosition.x + 10;
   }

  }

void drawPlayer() {
  fill(255,0,0);
  rect(playerPosition.x,playerPosition.y,playerWidth,playerHeight);  
}
void drawBarrier() {
  fill(0);
  rect(barrierPosition.x,barrierPosition.y,barrierWidth,barrierHeight);  
}

void movePlayerPosition() {

    //moves up and down
    if (goUp && playerPosition.y > 0)
    {
      playerPosition.y = playerPosition.y - velocity;
    } else if (goDown && playerPosition.y < 598-playerHeight)
    {
      playerPosition.y = playerPosition.y + velocity;
    }

    //moves right and left
    if (goLeft && playerPosition.x > 0)
    {
      playerPosition.x = playerPosition.x -velocity;
    } else if (goRight && playerPosition.x <800-playerWidth)
    {
      playerPosition.x = playerPosition.x + velocity;
    }
  }

  //if the keys are pressed move in that direction
void keyPressed() {
  if (key == 'w') {
    goUp=true;
  } else if (key == 'a') {
    goLeft=true;
  } else if (key == 's') {
    goDown=true;
  } else if (key == 'd') {
    goRight=true;
  }
}
//if the keys are released stop moving
void keyReleased() {
  if (key == 'w') {
    goUp=false;
  } else if (key == 'a') {
    goLeft=false;
  } else if (key == 's') {
    goDown=false;
  } else if (key == 'd') {
    goRight=false;
  }
}

先简单回答;您的 checkForCollide 方法应该 return 布尔值而不是空值,然后您可以将此方法与您的 movePlayerPosition 方法一起使用,并且仅在没有碰撞时执行 X 轴移动。

现在,在一个更完整的答案中;你应该多研究一下这个主题,有很多模式和库可以更好、更容易地处理这个主题。 我向您推荐 Nature of code,您可以免费获得此 pdf,并且您有很多关于第 5 章及以后的物理库的使用,还有很多关于编程游戏的示例。

希望这对您有所帮助,祝您好运。 问候何塞

你太接近了!

这一行之后:

println("COLLIDED");

你像这样设置新坐标:

playerPosition.x = barrierPosition.x + 10;

这意味着在第一次碰撞发生后,x 位置将始终设置为 barrierPosition.x + 10;

您可能想要做的是将播放器移动 10 个像素,但相对于它的当前位置:

playerPosition.x = playerPosition.x + 10;

或:

playerPosition.x += 10;

玩得开心!

作为参考,您的完整代码修改了几个字符:

PVector playerPosition;
PVector barrierPosition;

float velocity = 4;

boolean goLeft=false;
boolean goRight=false;
boolean goUp=false;
boolean goDown=false;

float playerWidth = 10;
float playerHeight = 10;

float barrierWidth = 10;
float barrierHeight = 600;



void setup() {
  size(800,600);
  rectMode(CORNER);
  playerPosition = new PVector(200,200);
  barrierPosition = new PVector(0,0);
}

void draw() {
 background(255); 
 drawPlayer();
 movePlayerPosition();
 drawBarrier();
 checkCollide();



}
  //check for collision with barrier then stop player moving
  void checkCollide() {
   if (playerPosition.y <= barrierHeight + playerHeight && playerPosition.x <= barrierWidth + playerWidth)
   {
     println("COLLIDED");
     playerPosition.x += 10;
   }

  }

void drawPlayer() {
  fill(255,0,0);
  rect(playerPosition.x,playerPosition.y,playerWidth,playerHeight);  
}
void drawBarrier() {
  fill(0);
  rect(barrierPosition.x,barrierPosition.y,barrierWidth,barrierHeight);  
}

void movePlayerPosition() {

    //moves up and down
    if (goUp && playerPosition.y > 0)
    {
      playerPosition.y = playerPosition.y - velocity;
    } else if (goDown && playerPosition.y < 598-playerHeight)
    {
      playerPosition.y = playerPosition.y + velocity;
    }

    //moves right and left
    if (goLeft && playerPosition.x > 0)
    {
      playerPosition.x = playerPosition.x -velocity;
    } else if (goRight && playerPosition.x <800-playerWidth)
    {
      playerPosition.x = playerPosition.x + velocity;
    }
  }

  //if the keys are pressed move in that direction
void keyPressed() {
  if (key == 'w') {
    goUp=true;
  } else if (key == 'a') {
    goLeft=true;
  } else if (key == 's') {
    goDown=true;
  } else if (key == 'd') {
    goRight=true;
  }
}
//if the keys are released stop moving
void keyReleased() {
  if (key == 'w') {
    goUp=false;
  } else if (key == 'a') {
    goLeft=false;
  } else if (key == 's') {
    goDown=false;
  } else if (key == 'd') {
    goRight=false;
  }
}