java在2d数组中的移动

java在2d数组中的移动,java,game-engine,game-physics,Java,Game Engine,Game Physics,在我正在构建的游戏中,我制作了一个基本的碰撞检测系统 我目前的方法解释如下: 我知道玩家将在游戏的下一步进行哪些训练: double checkforx = x+vx; double checkfory = y+vy; 然后检查是否与mapArray中的块(1)发生冲突 public static Boolean checkForMapCollisions(double character_x,double character_y){ //First find our positio

在我正在构建的游戏中,我制作了一个基本的碰撞检测系统

我目前的方法解释如下:

我知道玩家将在游戏的下一步进行哪些训练:

double checkforx = x+vx;
double checkfory = y+vy;
然后检查是否与mapArray中的块(1)发生冲突

public static Boolean checkForMapCollisions(double character_x,double character_y){

    //First find our position in the map so we can check for things...
    int map_x = (int) Math.round((character_x-10)/20);
    int map_y = (int) Math.round((character_y-10)/20);

    //Now find out where our bottom corner is on the map
    int map_ex = (int) Math.round((character_x+10)/20);
    int map_ey = (int) Math.round((character_y+10)/20);

    //Now check if there's anything in the way of our character being there...

    try{
        for(int y = map_y; y <= map_ey; y++){
            for(int x = map_x; x <= map_ex; x++){
                if (levelArray[y][x] == 1){
                    return true;
                }
            }
        }
    }catch (Exception e){
        System.out.println("Player outside the map");
    }
    return false;
}
公共静态布尔检查表单集合(双字符x,双字符y){
//首先在地图上找到我们的位置,这样我们就可以检查东西了。。。
int map_x=(int)数学圆((字符_x-10)/20);
int map_y=(int)数学圆((字符_y-10)/20);
//现在找出我们的底角在地图上的位置
int map_ex=(int)数学圆((字符x+10)/20);
int map_ey=(int)Math.round((字符y+10)/20);
//现在检查一下我们的角色在那里是否有任何阻碍。。。
试一试{

对于(int y=map_y;y我很抱歉给出了一个非常糟糕的解释,但这是我的github代码,它会有更好的帮助


只是为了解释我的工作。我有character object()它有向量和一个称为“站立”的布尔值。每次布尔值站立为false。然后我们将其传递给引擎以检查是否发生碰撞,如果发生碰撞,则站立为true,y向量为0。至于x向量,无论何时按下任何箭头键,都会将对象的x向量设置为所需的任何值。在更新循环中,显示根据速度大小对给定框进行评分。

此问题分为两部分。碰撞检测,即确定一个卷是否与另一个卷接触或相交。第二部分是碰撞响应。碰撞响应是物理部分

我将在这里介绍碰撞检测,因为这主要是您所问的问题

D为映射定义一个类,如下所示:

int emptyTile = 0;
//this assumes level is not a ragged array.
public boolean inBounds(int x, int y){
    return x>-1 && y>-1 && x<levelArray[0].length && y<levelArray.length;
}

public boolean checkForCollisions(Rectangle rectangle){
    boolean wasCollision = false;
    for(int x=0;x<rectangle.width && !wasCollision;x++){
        int x2 = x+rectangle.x;
        for(int y=0;y<rectangle.height && !wasCollision;y++){
             int y2 = y+rectangle.y;
             if(inBounds(x2,y2) && levelArray[y2][x2] != emptyTile){
                 //collision, notify listeners.
                 wasCollision=true;
             }
        }
    }
}
int-emptyTile=0;
//这假设级别不是参差不齐的数组。
公共布尔内边界(整数x,整数y){
返回x>-1&&y>-1&&x