然后碰撞检测让玩家停下来

时间:2015-10-29 03:47:19

标签: 2d processing collision-detection game-physics

我正在使用处理,我想创建一个游戏,玩家在一个房间里,如果他们撞墙,他们就像普通墙一样停止移动。但是,我真的不知道如何做到这一点。使用我当前的方法,一旦他们撞到墙壁,他们就不能再在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;
  }
}

2 个答案:

答案 0 :(得分:0)

首先简单回答;你的checkForCollide方法应该返回一个布尔值而不是void,然后你可以将这个方法与movePlayerPosition方法一起使用,并且只在没有碰撞时执行X轴移动。

现在,在一个更完整的答案;您应该更多地了解这个主题,有很多模式和库可以更好,更轻松地处理这个主题。 我推荐你Nature of code,你可以免费获得这个pdf,你有很多关于使用第5章和第5章的物理库,还有很多关于编程游戏的样本。

希望这会有所帮助并祝你好运。 问候何塞

答案 1 :(得分:0)

You're so very close! After this line: println("COLLIDED"); you set the new coordinates like so: playerPosition.x = barrierPosition.x + 10; This means after the first collision takes places, the x position will always be set to barrierPosition.x + 10; What you probably want to do is move the player by 10 pixels, but relative to it's present position: playerPosition.x = playerPosition.x + 10; or: playerPosition.x += 10; Have fun! For reference, your full code with a few characters modified: 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; } }