Java - 2D平铺游戏中的检测冲突

时间:2016-12-23 02:56:07

标签: java collision

我很高兴建立一个2D平铺游戏(没有libgdx的Slick2D),而且我正在进行超过15个小时的碰撞。我觉得是时候提问了。我搜索了很多,但没有人真正回应我搜索的内容。

所以,这就是它的样子:2D game capture(黑暗的牌是玩家无法进入的)和Another 2D game capture

矩形是碰撞可能发生的。在那里,当它们碰撞时,它仍然移动到足以锁定在瓷砖中。 (不能再做任何事了)

因此,我的类由输入处理程序,更新和绘图调用。论文三由“游戏”类。谁只是更新东西。 (Player.MoveUp(),Player.draw,...)

  • “WorldManager”是管理图块的位置(其中只有4个,如果被阻止,则查找getType()<0有效且1个被阻止

  • “LevelGeneration”是图块的级别,位置及其ID 映射(使用int [] [])(他没关系'它创建带ID的地图,当我用 然后表明他们都很好。

  • AssetManager是我剪切精灵表以进行变换然后在BufferedImage中的地方

  • tileize为50,玩家为41x36。

  • 我有一个问题,因为我想拥有自由的权利(4个方面可以制作8个方面)并且锚点似乎位于左上方

所以这是我的班级(没有导入和包):

public class Player {

public static double SPEED;
public static int WIDTH, HEIGHT;
public static float XPos, YPos;
public static float destXPos, destYPos;

public static BufferedImage presentImage;
public static int[] position; 
public static boolean moving;
public static boolean movementlock;
public static int side; // 0 up 1 right 2 down 3 left
public static Rectangle p = null;
public static Rectangle e = null;
private static boolean mouvementlock;
private static boolean collision;

public static void CreateHero(){
    // Valeurs de test
    position = new int[2];
    SPEED = 1;
    XPos = destXPos = 100;
    YPos = destYPos =  100;
    presentImage = AssetManager.Player[0][0];
    moving = false;
    movementlock = false;
    collision = false;

    position[0] = (int) (XPos/WorldManager.TileSize);
    position[1] = (int) (YPos/WorldManager.TileSize);

}

public static void MoveUp(){
    side = 0;
    destYPos -= SPEED;
    moving = ValidateNextPosition();
}
public static void MoveRight(){
    side = 1;
    destXPos += SPEED;
    moving = ValidateNextPosition();
}

public static void MoveDown(){
    side = 2;
    destYPos += SPEED;
    moving = ValidateNextPosition();
}

public static void MoveLeft(){
    side = 3;
    destXPos -= SPEED;
    moving = ValidateNextPosition();
}

static void updateMovement(){
    if(moving == true && mouvementlock == false){
        mouvementlock = true;
        System.out.println("Mouvement");
        System.out.println("X : " + XPos);
        System.out.println("Y : " + YPos);
        XPos = destXPos;
        YPos = destYPos;
        System.out.println("Xdest : " + destXPos);
        System.out.println("Ydest : " +  destYPos);
        System.out.println("" + moving);
        moving = false;
        mouvementlock = false;}
    else{
        destXPos = XPos;
        destYPos = YPos;
    }
}


public static boolean ValidateNextPosition(){
    if(moving) return false;
    if(collision) return false;

    p = new Rectangle((int) XPos, (int) YPos, presentImage.getWidth(null),presentImage.getHeight(null));
    int tileid = 0;
    boolean validate = true;
    int destpos[] = new int[2];
    double TileCollision[] =  new double[2];
    destpos[0] = (int) destXPos/WorldManager.TileSize;
    destpos[1] = (int) destYPos/WorldManager.TileSize;
    TileCollision[0] = 0;
    TileCollision[1] = 0;


    if(side == 0 && collision != true){
        if(destpos[1] - 1 > 0){
            tileid = LevelGenerator.getIdmap()[destpos[0]][destpos[1] - 1];
            if(destYPos + 1 < 0 || WorldManager.Tuiles[tileid].getType() == 1 ){
                destpos[1] -= 1;collision = true;}}
    }
    if(side == 1 && collision != true){
        tileid = LevelGenerator.getIdmap()[destpos[0] + 1][destpos[1]];
        if(destXPos + 1 < 0 || WorldManager.Tuiles[tileid].getType() == 1 ){
            destpos[0] += 1;collision = true;}
    }
    if(side == 2 && collision != true){
        tileid = LevelGenerator.getIdmap()[destpos[0]][destpos[1] + 1];
        if(destYPos - 1 < 0 || WorldManager.Tuiles[tileid].getType() == 1 ){
            destpos[1] += 1;collision = true;}
    }
    if(side == 3 && collision != true){
        tileid = LevelGenerator.getIdmap()[destpos[0]][destpos[1]];
        if(destXPos - 1 < 0 || WorldManager.Tuiles[tileid].getType() == 1 ){
            /*destpos[0] -= 1*/;collision = true;}
    }
    if (collision == true){
        TileCollision[0] = LevelGenerator.getPosmap()[destpos[0]][destpos[1]][0];
        TileCollision[1] = LevelGenerator.getPosmap()[destpos[0]][destpos[1]][1];
        e = new Rectangle((int) TileCollision[0], (int) TileCollision[1],WorldManager.TileSize,WorldManager.TileSize);}

    if(e != null && p.intersects(e)){
        validate = false;
        System.out.println("collision");}

    return validate;
}


public static void update() {
    updateMovement();
}

public static void draw(Graphics2D g) {
    g.drawImage(presentImage,(int) XPos,(int) YPos,presentImage.getWidth(null),presentImage.getWidth(null),null);
    if(p != null){
        g.setColor(Color.BLACK);
        g.drawRect(p.x, p.y, p.width, p.height);}
    if(e != null){
        g.setColor(Color.BLACK);
        g.drawRect(e.x, e.y, e.width, e.height);}
    }

}

要恢复,

  1. 碰撞发生得太晚,玩家陷入被阻挡的区域。
  2. 有时它会通过它,因为(我猜)左上角没有碰到阻挡。
  3. 它可以毫无问题地离开地图然后崩溃,但我尝试的一切都会让游戏崩溃
  4. 非常感谢你,祝你有个美好的一天

    Hell'no

    (P.S。:英语是我的第三语言,所以请问我是否完全不懂)

1 个答案:

答案 0 :(得分:0)

collision设置为true且您的ValidateNextPosition方法返回false时,collision(以及扩展名为moving变量)永远不能再次切换,因为{ {1}}声明在顶部。

相关问题