Java Rect.intersects()有时无法正常工作

技术联盟

我目前遍历所有的精灵,检查它们是否彼此相交,如下所示:

for (Sprite s : sprites) {
        if (s.dead) {
            dead.add(s);
        }
        for (Sprite sprite : sprites) {
            if (!sprite.equals(s)) {
                s.collide(sprite, maxX, maxY);
            }
        }
        s.run();

}

精灵使用Rect.intersects()方法进行检查,如下所示:

if (getRect().intersects(s.getRect()))

但是有时它只是完全忽略了碰撞,而对象只是彼此通过。

有任何想法吗?

技术联盟

我通过使其为框架之间覆盖的区域创建一个矩形来修复它,如下所示:

private void checkForNextCollision() {
    double boundsWidth = width + dX ;
    if(dX < 0){
        boundsWidth= width - dX ;
    }

    double boundsHeight = height + dY ;
    if(dY < 0){
        boundsHeight = height - dY ;
    }
    double boundx = xWorld + dX ;
    double boundy = yWorld + dY ;
    betweenRect = new Rectangle((int)(boundx),(int)(boundy),(int)(boundsWidth), (int)(boundsHeight));


}

然后对照其他子画面中创建的矩形检查此矩形,以检查下一帧是否应该存在碰撞:

    public void collide(Sprite s, int maxX, int maxY) {
    maxWX = maxX;
    maxWY = maxY;


    //check for collision with borders
    if (xWorld <= 0) {
        dX = -dX;
        xWorld += 2;
        if(xWorld < -1000){
            dX = 0;
            xWorld += 10;
        }
    }
    if (yWorld <= 0) {
        dY = -dY;
        yWorld += 2;
        if(yWorld < -1000){
            dX = 0;
            yWorld += 10;
        }
    }
    if (xWorld + width >= maxX) {
        dX = -dX;
        xWorld -= 2;
        if(xWorld+width > maxX + 1000){
            dX = 0;
            xWorld -= 10;
        }
    }
    if (yWorld + height >= maxY) {
        dY = -dY;
        yWorld -= 2;
        if(yWorld+height > maxY + 1000){
            dY = 0;
            yWorld -= 10;
        }
    }



    //check for collision with borders
    if(betweenRect.intersects(s.betweenRect)){
        willIntersect = true;
    }else{
        willIntersect = false;
    }

    // Use all checks to see if they should collide
    if (getRect().intersects(s.getRect()) || s.getRect().intersects(getRect()) || willIntersect || (xWorld + width > s.xWorld && xWorld < s.xWorld + s.width && yWorld < s.yWorld+s.height && yWorld + height > s.yWorld) ) {

            double lastDy = dY;
            double lastsDy = s.dY;
            double lastDx = dX;
            double lastsDx = s.dX;

            dY = (((weight - s.weight) / (weight + s.weight)) * lastDy)
                    + (((2.0 * s.weight) / (weight + s.weight)) * lastsDy);
            s.dY = (((s.weight - weight) / (weight + s.weight)) * lastsDy)
                    + (((2.0 * weight) / (weight + s.weight)) * lastDy);

            dX = (((weight - s.weight) / (weight + s.weight)) * lastDx)
                    + (((2.0 * s.weight) / (weight + s.weight)) * lastsDx);
            s.dX = (((s.weight - weight) / (weight + s.weight)) * lastsDx)
                    + (((2.0 * weight) / (weight + s.weight)) * lastDx);



            if(willIntersect){
                willIntersect = false;
                //s.willIntersect = false;
            }


        }




}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

svg rect元素上的classList无法正常工作

来自分类Dev

具有Rect.fromLTRB()的Canvas.drawRect()无法正常工作

来自分类Dev

无法更新rect对象pygame

来自分类Dev

Pygame Rect,有什么争论?

来自分类Dev

有时无法解析JAVA SQLException错误消息

来自分类Dev

TestContext.AddResultFile有时无法正常工作

来自分类Dev

WordPress的排序查询有时无法正常工作

来自分类Dev

Rails和jQuery有时无法正常工作

来自分类Dev

HOLDLOCK XLOCK ROWLOCK有时无法正常工作

来自分类Dev

HTML链接有时无法正常工作

来自分类Dev

MariaDB复制有时无法正常工作

来自分类Dev

iOS Swift动画有时无法正常工作

来自分类Dev

脾气暴躁的种子有时无法正常工作

来自分类Dev

TestContext.AddResultFile有时无法正常工作

来自分类Dev

Thread.sleep()有时无法正常工作

来自分类Dev

ios中的YouTube视频有时无法正常工作

来自分类Dev

Wordpress Orderby查询有时无法正常工作

来自分类Dev

有时XCode Playground无法正常工作

来自分类Dev

Rails和jQuery有时无法正常工作

来自分类Dev

jQuery .map有时无法正常工作

来自分类Dev

休眠更新有时无法正常工作

来自分类Dev

外部jquery .click()事件有时无法正常工作

来自分类Dev

HTML链接有时无法正常工作

来自分类Dev

使用Java配置时,Camel的BridgePropertyPlaceholderConfigurer无法正常工作

来自分类Dev

Chrome成为焦点时,Java Robot无法正常工作

来自分类Dev

当 32 位工作正常时无法安装 java 64 位

来自分类Dev

无法获得<svg的<rect元素的宽度

来自分类Dev

无法在 pygame ffs 上绘制 RECT

来自分类Dev

使用SDL_RECT时表面消失