拖放益智游戏的碰撞问题

发布于 2024-10-21 14:27:26 字数 6368 浏览 2 评论 0原文

我正在开发一款类似于高峰时间/交通堵塞/堵塞益智游戏的 Android 游戏。棋盘是一个正方形,包含矩形棋子。长的棋子只能水平移动,高的棋子只能垂直移动。目标是释放红色棋子并将其移出棋盘。这个游戏只是我用任何语言进行的第二个编程项目,因此任何提示或最佳实践以及您的答案将不胜感激。

我有一个名为 Pieces 的游戏块类,它描述了它们的大小和绘制到屏幕上的方式,为它们提供了拖放功能,并检测和处理碰撞。

然后,我有一个名为 GameView 的活动类,它创建我的布局并创建 Pieces 对象以添加到名为 Board 的相对布局中。我曾考虑过将 Board 创建为自己的类,但目前还不需要。

这是我正在进行的工作的样子:
screen_cap1

我的问题:
除了我的碰撞处理之外,大部分工作都很好。它似乎很好地检测到碰撞,但当发生碰撞时,它不会将碎片推到彼此之外,而是在碎片被拖动到的位置和应该在的位置之间疯狂地来回移动。它看起来像这样:
screencapgif1
另一个奇怪的地方是:当拖动的棋子与其左侧的棋子碰撞时,碰撞处理似乎工作得很好。只有上面、下面和右边的部分会导致问题。
这是碰撞代码:

    @Override
public boolean onTouchEvent(MotionEvent event){

    float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        //check if touch is on piece
        if (eventX > x && eventX < (x+width) && eventY > y && eventY < (y+height)){
            initialX=x;
            initialY=y;
            break;
        }else{
            return false;
        }
    case MotionEvent.ACTION_MOVE:
        //determine if piece should move horizontally or vertically
        if(width>height){
            for (Pieces piece : aPieces) {
                //if object equals itself in array, skip to next object
                if(piece==this){
                    continue;
                }
                //if next to another piece, 
                //do not allow to move any further towards said piece
                if(eventX<x&&(x==piece.right+1)){
                    return false;
                }else if(eventX>x&&(x==piece.x-width-1)){
                    return false;
                }
                //move normally if no collision
                //if collision, do not allow to move through other piece
                if(collides(this,piece)==false){
                    x = (eventX-(width/2));
                }else if(collidesLeft(this,piece)){
                    x = piece.right+1;
                    break;
                }else if(collidesRight(this,piece)){
                    x = piece.x-width-1;
                    break;
                }
            }
            break;
        }else if(height>width){
            for (Pieces piece : aPieces) {
                if(piece==this){
                    continue;
                }else if(collides(this,piece)==false){
                    y = (eventY-(height/2));
                }else if(collidesUp(this,piece)){
                    y = piece.bottom+1;
                    break;
                }else if(collidesDown(this,piece)){
                    y = piece.y-height-1;
                    break;
                }
            }
        }
        invalidate();
        break;

    case MotionEvent.ACTION_UP:
        // end move
        if(this.moves()){
        GameView.counter++;
        }
        initialX=x;
        initialY=y;
        break;
        }
    // parse puzzle
    invalidate();
    return true;
    }

这发生在 onDraw 期间:

width = sizedBitmap.getWidth();
height = sizedBitmap.getHeight();
right = x+width;
bottom = y+height;

我的碰撞测试方法看起来像这样,每个方法都有不同的数学:

    private boolean collidesDown(Pieces piece1, Pieces piece2){
    float x1 = piece1.x;
    float y1 = piece1.y;
    float r1 = piece1.right;
    float b1 = piece1.bottom;
    float x2 = piece2.x;
    float y2 = piece2.y;
    float r2 = piece2.right;
    float b2 = piece2.bottom;

    if((y1<y2)&&(y1<b2)&&(b1>=y2)&&(b1<b2)&&((x1>=x2&&x1<=r2)||(r1>=x2&&x1<=r2))){
        return true;
    }else{
        return false;
    }
}

private boolean collides(Pieces piece1, Pieces piece2){
    if(collidesLeft(piece1,piece2)){
        return true;
    }else if(collidesRight(piece1,piece2)){
        return true;
    }else if(collidesUp(piece1,piece2)){
        return true;
    }else if(collidesDown(piece1,piece2)){
        return true;
    }else{
        return false;
    }
}

作为第二个问题,我的 x,y,right,bottom,width,height 应该变量是整数而不是像现在这样的浮点数? 此外,任何有关如何更好地实施事情的建议将不胜感激,即使与问题无关!预先感谢您的帮助并耐心解答这么长的问题!

更新:
我已经让它与以下代码几乎完美地工作(这不包括垂直件的代码):

@Override
public boolean onTouchEvent(MotionEvent event){

    float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        //check if touch is on piece
        if (eventX > x && eventX < (x+width) && eventY > y && eventY < (y+height)){
            initialX=x;
            initialY=y;
            break;
        }else{
            return false;
        }
    case MotionEvent.ACTION_MOVE:
        //determine if piece should move horizontally or vertically
        if(width>height){
            for (Pieces piece : aPieces) {
                //if object equals itself in array, skip to next object
                if(piece==this){
                    continue;
                }
                //check if there the possibility for a horizontal collision
                if(this.isAllignedHorizontallyWith(piece)){
                    //check for and handle collisions while moving left
                    if(this.isRightOf(piece)){
                        if(eventX>piece.right+(width/2)){
                            x = (int)(eventX-(width/2)); //move normally
                        }else{
                            x = piece.right+1;
                        }
                    }
                    //check for and handle collisions while moving right
                    if(this.isLeftOf(piece)){
                        if(eventX<piece.x-(width/2)){
                            x = (int)(eventX-(width/2));
                        }else{
                            x = piece.x-width-1;
                        }
                    }
                    break;
                }else{
                    x = (int)(eventX-(width/2));
                }

该代码的唯一问题是它只检测移动件与另一个件之间的碰撞(优先于垂直件上的碰撞)左边)。如果左侧有一个块要碰撞,右侧有另一个块要碰撞,则它只会检测与左侧块的碰撞。我认为这是因为一旦它发现可能的碰撞,它就会处理它,而不会完成对包含所有部分的数组的循环。如何让它同时检查多个可能的碰撞?

I am working on an Android game similar to the Rush Hour/Traffic Jam/Blocked puzzle games. The board is a square containing rectangular pieces. Long pieces may only move horizontally, and tall pieces may only move vertically. The object is to free the red piece and move it out of the board. This game is only my second ever programming project in any language, so any tips or best practices would be appreciated along with your answer.

I have a class for the game pieces called Pieces that describes how they are sized and drawn to the screen, gives them drag-and-drop functionality, and detects and handles collisions.

I then have an activity class called GameView which creates my layout and creates Pieces objects to add to a RelativeLayout called Board. I have considered making Board its own class, but haven't needed to yet.

Here's what my work in progress looks like:
screen_cap1

My Question:
Most of this works perfectly fine except for my collision handling. It seems to be detecting collisions well but instead of pushing the pieces outside of each other when there is a collision, it frantically snaps back and forth between (what seems to be) where the piece is being dragged to and where it should be. It looks something like this:
screencapgif1
Another oddity: when the dragged piece collides with a piece to its left, the collision handling seems to work perfectly. Only piece above, below, and to the right cause problems.
Here's the collision code:

    @Override
public boolean onTouchEvent(MotionEvent event){

    float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        //check if touch is on piece
        if (eventX > x && eventX < (x+width) && eventY > y && eventY < (y+height)){
            initialX=x;
            initialY=y;
            break;
        }else{
            return false;
        }
    case MotionEvent.ACTION_MOVE:
        //determine if piece should move horizontally or vertically
        if(width>height){
            for (Pieces piece : aPieces) {
                //if object equals itself in array, skip to next object
                if(piece==this){
                    continue;
                }
                //if next to another piece, 
                //do not allow to move any further towards said piece
                if(eventX<x&&(x==piece.right+1)){
                    return false;
                }else if(eventX>x&&(x==piece.x-width-1)){
                    return false;
                }
                //move normally if no collision
                //if collision, do not allow to move through other piece
                if(collides(this,piece)==false){
                    x = (eventX-(width/2));
                }else if(collidesLeft(this,piece)){
                    x = piece.right+1;
                    break;
                }else if(collidesRight(this,piece)){
                    x = piece.x-width-1;
                    break;
                }
            }
            break;
        }else if(height>width){
            for (Pieces piece : aPieces) {
                if(piece==this){
                    continue;
                }else if(collides(this,piece)==false){
                    y = (eventY-(height/2));
                }else if(collidesUp(this,piece)){
                    y = piece.bottom+1;
                    break;
                }else if(collidesDown(this,piece)){
                    y = piece.y-height-1;
                    break;
                }
            }
        }
        invalidate();
        break;

    case MotionEvent.ACTION_UP:
        // end move
        if(this.moves()){
        GameView.counter++;
        }
        initialX=x;
        initialY=y;
        break;
        }
    // parse puzzle
    invalidate();
    return true;
    }

This takes place during onDraw:

width = sizedBitmap.getWidth();
height = sizedBitmap.getHeight();
right = x+width;
bottom = y+height;

My collision-test methods look like this with different math for each:

    private boolean collidesDown(Pieces piece1, Pieces piece2){
    float x1 = piece1.x;
    float y1 = piece1.y;
    float r1 = piece1.right;
    float b1 = piece1.bottom;
    float x2 = piece2.x;
    float y2 = piece2.y;
    float r2 = piece2.right;
    float b2 = piece2.bottom;

    if((y1<y2)&&(y1<b2)&&(b1>=y2)&&(b1<b2)&&((x1>=x2&&x1<=r2)||(r1>=x2&&x1<=r2))){
        return true;
    }else{
        return false;
    }
}

private boolean collides(Pieces piece1, Pieces piece2){
    if(collidesLeft(piece1,piece2)){
        return true;
    }else if(collidesRight(piece1,piece2)){
        return true;
    }else if(collidesUp(piece1,piece2)){
        return true;
    }else if(collidesDown(piece1,piece2)){
        return true;
    }else{
        return false;
    }
}

As a second question, should my x,y,right,bottom,width,height variables be ints instead of floats like they are now?
Also, any suggestions on how to implement things better would be greatly appreciated, even if not relevant to the question! Thanks in advance for the help and for sitting through such a long question!

Update:
I have gotten it working almost perfectly with the following code (this doesn't include the code for vertical pieces):

@Override
public boolean onTouchEvent(MotionEvent event){

    float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        //check if touch is on piece
        if (eventX > x && eventX < (x+width) && eventY > y && eventY < (y+height)){
            initialX=x;
            initialY=y;
            break;
        }else{
            return false;
        }
    case MotionEvent.ACTION_MOVE:
        //determine if piece should move horizontally or vertically
        if(width>height){
            for (Pieces piece : aPieces) {
                //if object equals itself in array, skip to next object
                if(piece==this){
                    continue;
                }
                //check if there the possibility for a horizontal collision
                if(this.isAllignedHorizontallyWith(piece)){
                    //check for and handle collisions while moving left
                    if(this.isRightOf(piece)){
                        if(eventX>piece.right+(width/2)){
                            x = (int)(eventX-(width/2)); //move normally
                        }else{
                            x = piece.right+1;
                        }
                    }
                    //check for and handle collisions while moving right
                    if(this.isLeftOf(piece)){
                        if(eventX<piece.x-(width/2)){
                            x = (int)(eventX-(width/2));
                        }else{
                            x = piece.x-width-1;
                        }
                    }
                    break;
                }else{
                    x = (int)(eventX-(width/2));
                }

The only problem with this code is that it only detects collisions between the moving piece and one other (with preference to one on the left). If there is a piece to collide with on the left and another on the right, it will only detect collisions with the one on the left. I think this is because once it finds a possible collision, it handles it without finishing looping through the array holding all the pieces. How do I get it to check for multiple possible collisions at the same time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半衾梦 2024-10-28 14:27:26

我最好的猜测是,被拖动的棋子被拖到另一棋子中,然后向后移动,这样它就不再碰撞,然后再次被拖动到另一棋子中。至少这是我所期望的,如果绘制后发生碰撞响应。

顺便说一句,下面的代码让我有点想知道:

            //if next to another piece, 
            //do not allow to move any further towards said piece
            if(eventX<x&&(x==piece.right+1)){
                return false;
            }else if(eventX>x&&(x==piece.x-width-1)){
                return false;
            }

检查浮点数是否相等(==)通常是一个非常糟糕的主意,请参阅无数的“浮点精度”主题。替代方案 a) 使用整数而不是浮点数。 b) 使用范围比较(>= 等)。但不要使用 (a),微小的(时间)步长等有很多缺点。

尝试使用与触摸检测相同的方法:

    //check if touch is on piece
    if (eventX > x && eventX < (x+width) && eventY > y && eventY < (y+height))

My best guess is that the dragged piece is dragged into the other piece, then moved back so it is no longer colliding, then dragged into the other piece again. At least that is what I would expect if collision response happens after drawing.

On a side note, the following code makes me wonder a bit:

            //if next to another piece, 
            //do not allow to move any further towards said piece
            if(eventX<x&&(x==piece.right+1)){
                return false;
            }else if(eventX>x&&(x==piece.x-width-1)){
                return false;
            }

Checking for equality (==) on floats is generally a very bad idea, see zillions of "floating point precision" topics. Alternative a) use ints instead of floats. b) use ranged comparisions (>=, etc.). Don't use (a) though, there are many drawbacks with tiny (time) steps and such.

Try using the same approach as with the touch detection instead:

    //check if touch is on piece
    if (eventX > x && eventX < (x+width) && eventY > y && eventY < (y+height))
知足的幸福 2024-10-28 14:27:26

您应该能够更简单地做到这一点:)

请注意,我对如何存储运动方向做了一些假设,您可能需要根据自己的喜好进行调整。

另请注意,我将piece1视为我们关心的移动部件,而piece2则简单地视为与其碰撞的对象。因此,piece1 被重新定位,piece2 则没有。

// Horizontal:
if( piece1.isMovingLeft )
    piece1.x += Math.max( 0, piece2.right - piece1.x );
else if( piece1.isMovingRight )
    piece1.x -= Math.max( 0, piece1.right - piece2.x );

// Vertical:
if( piece1.isMovingUp )
    piece1.y -= Math.max( 0, piece2.bottom - piece1.y );
else if( piece1.isMovingDown )
    piece1.y += Math.max( 0, piece1.bottom - piece2.y )

我在这里所做的如下:

  • 如果该棋子朝某个方向移动,我们将其朝相反方向向后移动(一点点)以补偿(可能的)碰撞。我们需要将其向后移动重叠像素的数量。
  • 例如,向左移动时的重叠量是第二个对象的右侧减去第一个对象的左侧。 (负值表示没有重叠。)
  • Math.max( 0, Overlap ) 确保所述负值变为 0,即没有碰撞不会导致补偿。
  • 然后在与运动相反的方向上应用补偿,有效地将piece1从piece2中取出。 (然后你可以选择反转它的运动方向或以任何进一步的方式做出反应。)

你可以将这个简单的例程应用于两个部分的每种可能的组合,并且它们将不再穿透。

我希望这对你有帮助!

You should be able to do this more simply :)

Note that I made some assumptions on how you store the direction of movement, which you may have to adjust to your likings.

Also note that I treat piece1 as the moving piece we are concerned with, and piece2 simply as an object it collides with. So piece1 is repositioned, piece2 is not.

// Horizontal:
if( piece1.isMovingLeft )
    piece1.x += Math.max( 0, piece2.right - piece1.x );
else if( piece1.isMovingRight )
    piece1.x -= Math.max( 0, piece1.right - piece2.x );

// Vertical:
if( piece1.isMovingUp )
    piece1.y -= Math.max( 0, piece2.bottom - piece1.y );
else if( piece1.isMovingDown )
    piece1.y += Math.max( 0, piece1.bottom - piece2.y )

What I do here is as follows:

  • If the piece is moving in a certain direction, we move it back (a little bit) in the opposite direction to compensate for (possible) collision. We need to move it back by exactly the amount of overlapping pixels.
  • The amount of overlap, when moving left, for instance, is the right side of the second object minus the left side of the first object. (Negative means no overlap.)
  • The Math.max( 0, overlap ) ensures that said negative values become 0, i.e. no collision leads to no compensation.
  • The compensation is then applied in the direction opposite of movement, effectively taking piece1 out of piece2. (You can then choose to invert its movement direction or respond in any further way.)

You can apply this simple routine to every possible combination of two pieces, and they will no longer penetrate.

I hope this helps you out!

万劫不复 2024-10-28 14:27:26

回答你的第二个问题:

这段代码的唯一问题是它只检测移动块与另一个块之间的碰撞(优先于左侧的块)。如果左侧有一个块要碰撞,右侧有一个块要碰撞,则它只会检测与左侧块的碰撞。

循环中有一个break语句 - 具体来说,在碰撞检查中。 ;)

In reply to your second question:

The only problem with this code is that it only detects collisions between the moving piece and one other (with preference to one on the left). If there is a piece to collide with on the left and another on the right, it will only detect collisions with the one on the left.

There is a break statement within your loop - specifically, in the left collision check. ;)

与君绝 2024-10-28 14:27:26

我做了一些更改,到目前为止,它运行得很好。这是我的新代码:

case MotionEvent.ACTION_MOVE:
        //determine if piece should move horizontally or vertically
        if(width>height){
            for (Pieces piece : aPieces) {
                //if object equals itself in array, skip to next object
                if(piece==this){
                    continue;
                }
                //check if there the possibility for a horizontal collision
                if(this.isAllignedHorizontallyWith(piece)){
                    //check for and handle collisions while moving left
                    if(this.isRightOf(piece)){
                        if(eventX>piece.right+(width/2)){
                            x = (int)(eventX-(width/2)); //move normally
                            continue;
                        }else{
                            x = piece.right+1;
                        }
                    }else if(this.isLeftOf(piece)){ //check for and handle collisions while moving right
                        if(eventX<piece.x-(width/2)){
                            x = (int)(eventX-(width/2));
                            continue;
                        }else{
                            x = piece.x-width-1;
                        }
                    }else{
                        continue;
                    }
                    break;
                }else{
                    x = (int)(eventX-(width/2));
                }
            }
        }
        if(height>width){
            for (Pieces piece : aPieces) {
                //if object equals itself in array, skip to next object
                if(piece==this){
                    continue;
                }
                //check if there the possibility for a vertical collision
                if(this.isAllignedVerticallyWith(piece)){
                    //check for and handle collisions while moving up
                    if(this.isBelow(piece)){
                        if(eventY>piece.bottom+(height/2)){
                            y = (int)(eventY-(height/2)); //move normally
                            continue;
                        }else{
                            y = piece.bottom+1;
                        }
                    }else if(this.isAbove(piece)){ //check for and handle collisions while moving down
                        if(eventY<piece.y-(height/2)){
                            y = (int)(eventY-(height/2));
                            continue;
                        }else{
                            y = piece.y-height-1;
                        }
                    }else{
                        continue;
                    }
                    break;
                }else{
                    y = (int)(eventY-(height/2));
                }
            }
        }
        invalidate();
        break;

这些是我使用的方法:

private boolean isLeftOf(Pieces piece) {
    int r1 = this.right;
    int x2 = piece.initialX;
    boolean bool = false;
    if (r1<=x2){
        for(Pieces piece2: aPieces){
            if (piece2==piece || piece2==this){
                continue;
            }
            if (this.isAllignedHorizontallyWith(piece2) && r1<=piece2.initialX){
                if (piece.x>piece2.x){
                    bool = false;
                    break;
                }else{
                    bool = true;
                    continue;
                }
            }else{
                bool = true;
            }
        }
    }else{
        bool = false;
    }
    return bool;
}

private boolean isRightOf(Pieces piece) {
    //True means that "this" is right of "piece" and "piece" is farther right than other possible pieces
    int x1 = this.initialX;
    int r2 = piece.right;
    boolean bool = false;

    if (x1>=r2){
        for(Pieces piece2: aPieces){
            if (piece2==piece || piece2==this){
                continue;
            }
            if (this.isAllignedHorizontallyWith(piece2) && x1>=piece2.right){
                if (piece.x<piece2.x){
                    bool = false;
                    break;
                }else{
                    bool = true;
                    continue;
                }
            }else{
                bool = true;
            }
        }
    }else{
        bool = false;
    }
    return bool;
}

private boolean isBelow(Pieces piece){
    int y1 = this.initialY;
    int b2 = piece.bottom;
    boolean bool = false;
    if (y1>=b2){
        for(Pieces piece2: aPieces){
            if (piece2==piece || piece2==this){
                continue;
            }
            if (this.isAllignedVerticallyWith(piece2) && y1>=piece2.bottom){
                if (piece.y<piece2.y){
                    bool = false;
                    break;
                }else{
                    bool = true;
                    continue;
                }
            }else{
                bool = true;
            }
        }
    }else{
        bool = false;
    }
    return bool;
}

private boolean isAbove(Pieces piece){
    int y2 = piece.initialY;
    int b1 = this.bottom;
    boolean bool = false;
    if (b1<=y2){
        for(Pieces piece2: aPieces){
            if (piece2==piece || piece2==this){
                continue;
            }
            if (this.isAllignedVerticallyWith(piece2) && b1<=piece2.y){
                if (piece.y>piece2.y){
                    bool = false;
                    break;
                }else{
                    bool = true;
                    continue;
                }
            }else{
                bool = true;
            }
        }
    }else{
        bool = false;
    }
    return bool;
}

private boolean isAllignedHorizontallyWith(Pieces piece) {

    int y1 = this.y;
    int b1 = this.bottom;
    int y2 = piece.y;
    int b2 = piece.bottom;

    if((y1>=y2&&y1<=b2)||(b1>=y2&&b1<=b2)){
        return true;
    }else{
        return false;   
    }
}

private boolean isAllignedVerticallyWith(Pieces piece) {

    int x1 = this.x;
    int r1 = this.right;
    int x2 = piece.x;
    int r2 = piece.right;

    if((x1>=x2&&x1<=r2)||(r1>=x2&&r1<=r2)){
        return true;
    }else{
        return false;   
    }
}

这可以更好地注释并且可能做得更简单,但我将其留在这里供参考。

I made a few changes and, so far, it has been working perfectly. Here is my new code:

case MotionEvent.ACTION_MOVE:
        //determine if piece should move horizontally or vertically
        if(width>height){
            for (Pieces piece : aPieces) {
                //if object equals itself in array, skip to next object
                if(piece==this){
                    continue;
                }
                //check if there the possibility for a horizontal collision
                if(this.isAllignedHorizontallyWith(piece)){
                    //check for and handle collisions while moving left
                    if(this.isRightOf(piece)){
                        if(eventX>piece.right+(width/2)){
                            x = (int)(eventX-(width/2)); //move normally
                            continue;
                        }else{
                            x = piece.right+1;
                        }
                    }else if(this.isLeftOf(piece)){ //check for and handle collisions while moving right
                        if(eventX<piece.x-(width/2)){
                            x = (int)(eventX-(width/2));
                            continue;
                        }else{
                            x = piece.x-width-1;
                        }
                    }else{
                        continue;
                    }
                    break;
                }else{
                    x = (int)(eventX-(width/2));
                }
            }
        }
        if(height>width){
            for (Pieces piece : aPieces) {
                //if object equals itself in array, skip to next object
                if(piece==this){
                    continue;
                }
                //check if there the possibility for a vertical collision
                if(this.isAllignedVerticallyWith(piece)){
                    //check for and handle collisions while moving up
                    if(this.isBelow(piece)){
                        if(eventY>piece.bottom+(height/2)){
                            y = (int)(eventY-(height/2)); //move normally
                            continue;
                        }else{
                            y = piece.bottom+1;
                        }
                    }else if(this.isAbove(piece)){ //check for and handle collisions while moving down
                        if(eventY<piece.y-(height/2)){
                            y = (int)(eventY-(height/2));
                            continue;
                        }else{
                            y = piece.y-height-1;
                        }
                    }else{
                        continue;
                    }
                    break;
                }else{
                    y = (int)(eventY-(height/2));
                }
            }
        }
        invalidate();
        break;

And these are the methods I use:

private boolean isLeftOf(Pieces piece) {
    int r1 = this.right;
    int x2 = piece.initialX;
    boolean bool = false;
    if (r1<=x2){
        for(Pieces piece2: aPieces){
            if (piece2==piece || piece2==this){
                continue;
            }
            if (this.isAllignedHorizontallyWith(piece2) && r1<=piece2.initialX){
                if (piece.x>piece2.x){
                    bool = false;
                    break;
                }else{
                    bool = true;
                    continue;
                }
            }else{
                bool = true;
            }
        }
    }else{
        bool = false;
    }
    return bool;
}

private boolean isRightOf(Pieces piece) {
    //True means that "this" is right of "piece" and "piece" is farther right than other possible pieces
    int x1 = this.initialX;
    int r2 = piece.right;
    boolean bool = false;

    if (x1>=r2){
        for(Pieces piece2: aPieces){
            if (piece2==piece || piece2==this){
                continue;
            }
            if (this.isAllignedHorizontallyWith(piece2) && x1>=piece2.right){
                if (piece.x<piece2.x){
                    bool = false;
                    break;
                }else{
                    bool = true;
                    continue;
                }
            }else{
                bool = true;
            }
        }
    }else{
        bool = false;
    }
    return bool;
}

private boolean isBelow(Pieces piece){
    int y1 = this.initialY;
    int b2 = piece.bottom;
    boolean bool = false;
    if (y1>=b2){
        for(Pieces piece2: aPieces){
            if (piece2==piece || piece2==this){
                continue;
            }
            if (this.isAllignedVerticallyWith(piece2) && y1>=piece2.bottom){
                if (piece.y<piece2.y){
                    bool = false;
                    break;
                }else{
                    bool = true;
                    continue;
                }
            }else{
                bool = true;
            }
        }
    }else{
        bool = false;
    }
    return bool;
}

private boolean isAbove(Pieces piece){
    int y2 = piece.initialY;
    int b1 = this.bottom;
    boolean bool = false;
    if (b1<=y2){
        for(Pieces piece2: aPieces){
            if (piece2==piece || piece2==this){
                continue;
            }
            if (this.isAllignedVerticallyWith(piece2) && b1<=piece2.y){
                if (piece.y>piece2.y){
                    bool = false;
                    break;
                }else{
                    bool = true;
                    continue;
                }
            }else{
                bool = true;
            }
        }
    }else{
        bool = false;
    }
    return bool;
}

private boolean isAllignedHorizontallyWith(Pieces piece) {

    int y1 = this.y;
    int b1 = this.bottom;
    int y2 = piece.y;
    int b2 = piece.bottom;

    if((y1>=y2&&y1<=b2)||(b1>=y2&&b1<=b2)){
        return true;
    }else{
        return false;   
    }
}

private boolean isAllignedVerticallyWith(Pieces piece) {

    int x1 = this.x;
    int r1 = this.right;
    int x2 = piece.x;
    int r2 = piece.right;

    if((x1>=x2&&x1<=r2)||(r1>=x2&&r1<=r2)){
        return true;
    }else{
        return false;   
    }
}

This could be commented better and probably done much simpler, but I am leaving it here for reference.

脸赞 2024-10-28 14:27:26

没有什么只是得到你的图像视图的左,右,上,下,并与当前的x和y相交,并找到这个解决方案更多关于这个转到这个链接它也有帮助我希望你有一个权利在此链接上回答

/technical/game-programming/collision-detection-algorithm-r754">http://www.gamedev.net/page/resources//technical/game-programming/collision-detection-algorithm-r754

Nothing just get the your imageviews Left,right,top,bottom, and intersect with current x and y and find this solution more about this go to this link its also help i hop you got a right answer on this link

/technical/game-programming/collision-detection-algorithm-r754">http://www.gamedev.net/page/resources//technical/game-programming/collision-detection-algorithm-r754

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文