【问题标题】:android collision detection with bounds带有边界的android碰撞检测
【发布时间】:2018-02-13 09:35:02
【问题描述】:

我们正在尝试检测在屏幕上移动的图像视图上的碰撞。问题是当brickRect 的底部或左侧与ballRect 的顶部或右侧碰撞时,我们只想要一个负面反应。如果 ballRect 想要降落在brickRect 的顶部,则不需要负面反应。我们的问题是如何为ballRect 与brickRect 的这三个交互编写if 语句?我们了解如何获取 top bottom 和 left 值我们将发布该代码不确定它是否会添加到问题中

        Rect brickRect = new Rect();
    brickFOUR.getHitRect(brickRect);

    int BRICKbottom = brickRect.bottom;
    int BRICKtop = brickRect.top;
    int BRICKleft = brickRect.left;
    int BRICKright = brickRect.right;

这里的对象是让ballRect跳起来落在brickRect上 如果 ballRect 跳跃并撞到brickRect 的底部,那么做任何事情 如果 ballRect.right 拦截了brickRect.left 然后做任何事情 那么我们如何把这个逻辑变成代码呢? 我们看过很多帖子,但它们都处理了所有检测

【问题讨论】:

    标签: android collision-detection


    【解决方案1】:

    假设你有一对 position 或者 Rect 模型,对于球的前后运动,并且 position 是向上和向右增加的,那么你可以编写如下代码:

    Rect ballBefore = ...
    Rect ballAfter = ...
    Rect brickRect = new Rect();
    brickFOUR.getHitRect(brickRect);
    
    if(ballBefore.top > ballAfter.top
        && ballBefore.top < brickRect.bottom
        && ballAfter.top >= brickRect.bottom) {
        //movement in top direction
        //and position before is below
        //and position after is not
    }
    if(ballBefore.right > ballAfter.right
        && ballBefore.right < brickRect.left
        && ballAfter.right >= brickRect.left) {
       //if movement is in right direction
       //and position before is left
       //and position after is not
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-01
      • 2014-02-04
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多