首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java:命中检测不恰当地检测两个对象之间的重叠

Java:命中检测不恰当地检测两个对象之间的重叠
EN

Stack Overflow用户
提问于 2016-06-02 00:57:12
回答 1查看 164关注 0票数 0

所以我有一个赛艇游戏,在这个游戏中,一个小船物体必须避开随机放置的障碍物,使用箭头键。如果船撞上其中一个障碍物,船就会受到任意程度的伤害。这艘船是一张27x52的图片(两边都是2个像素,所以代码显示2-25),障碍物是矩形。

我已经击中探测工作的大部分,然而,每当船是在一个长方形的右侧,比它是宽,它将受到损害,即使船是大约5-10像素离右边缘的矩形。要更好地理解问题:http://imgur.com/pqDLMrl,请参阅此图像

在我的代码中,我创建了一个由15个障碍组成的数组。障碍采取参数(颜色,int damageDealt,int xPos,int yPos,int宽度,int高度,布尔hasHit)。

代码语言:javascript
复制
//Create array of obstacles
for(int x = 0; x < 15; x++) {
        obstacles[x] = new Obstacle((new Color(rand.nextInt(81), rand.nextInt(51), rand.nextInt(51))), rand.nextInt(21) + 10, rand.nextInt(601), 
            (-x * (rand.nextInt(51) + 31)), (rand.nextInt(31) + 5), (rand.nextInt(31) + 5), false); 
    }

下面是命中检测代码(在一个for循环中,遍历障碍对象数组):

代码语言:javascript
复制
for(int y = 2; y <= 25; y++) {
                //Obstacles hit detection
                for(int z = 0; z <= obstacles[x].getw(); z++) {           
                      if(boat.getx() + y == obstacles[x].getx() + z && boat.gety() == obstacles[x].gety()) {
                            if(!obstacles[x].getDamaged()) {
                                 boat.setHealth(boat.getHealth() - obstacles[x].getdmg());
                                 obstacles[x].setDamaged(true);
                            }
                      }             
                }

现在它循环通过船的x值,从2到25,而不是0到27,因为两边没有任何东西的两个像素。然后,它循环通过障碍的x值(xPos到xPos+width),并查看其中任何一个值是否匹配。如果有匹配,并且y值匹配,那么船就会受到伤害。记住,只有当船在障碍物的右边,障碍物比宽时才会发生这种情况。我认为我的代码没有问题,但我无法找到解决我的错误的解决方案。谢谢。

编辑:这是船和障碍类的代码。

代码语言:javascript
复制
import java.awt.Color;
import java.awt.Rectangle;

public class Obstacle {
    private int dmg, xPos, yPos, height, width;
    private Color color;
    private boolean hasDamaged;

    public Obstacle(Color hue, int damage, int x, int y, int w, int h, boolean damaged) {
        dmg = damage;
        xPos = x;
        yPos = y;
        width = w;
        height = h;
        color = hue;
        hasDamaged = damaged;
    }

    public boolean getDamaged() {
        return hasDamaged;
    }
    public void setDamaged(boolean damaged) {
        hasDamaged = damaged;
    }

    public Color getColor() {
        return color;
    }

    public int getdmg() {
        return dmg;
    }
    public void setdmg(int damage) {
        dmg = damage;
    }

    public int getx() {
        return xPos;
    }
    public void setx(int x) {
        xPos = x;
    }

    public int gety() {
        return yPos;
    }
    public void sety(int y) {
        yPos = y;
    }

    public int getw() {
        return width;
    }
    public void setw(int w) {
        width = w;
    }

    public int geth() {
        return height;
    }
    public void seth(int h) {
        height = h;
    }

    public Rectangle getBounds() {
        return new Rectangle(xPos, yPos, width, height);
    }
}

还有船舱:

代码语言:javascript
复制
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;

public class Boat {

private int hp, xPos, yPos, dx;
private BufferedImage boatPic;

public Boat(BufferedImage img, int health, int x, int y, int velX) {
    boatPic = img;
    hp = health;
    xPos = x;
    yPos = y;
    dx = velX;
}


public BufferedImage getImage() {
    return boatPic;
}
public void setImage(BufferedImage img) {
    boatPic = img;
}

public int getHealth() {
    return hp;
}
public void setHealth(int health) {
    hp = health;
}

public int getx() {
    return xPos;
}
public void setx(int x) {
    xPos = x;
}

public int gety() {
    return yPos;
}
public void sety(int y) {
    yPos = y;
}

public int getdx() {
    return dx;
}
public void setdx(int velX) {
    dx = velX;
}

public Rectangle getBounds() {
    return new Rectangle(xPos, yPos, 25, 49);
}    
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-02 07:01:10

如果你的船总是面对同一方向,你可以更容易地检查碰撞。

通过为您的飞船和障碍物创建一个方法,返回一个与您的对象大小相同的矩形。

代码语言:javascript
复制
public Rectangle getBounds() {
    return new Rectangle(shipPosX, shipPosY, widht, height);
}

就像这样。这样,您就可以在一行中测试碰撞:

代码语言:javascript
复制
getBounds().intersects(obstacle.getBounds());

如果它们相交,这将返回真。

如果你想要更精确,或者想要不同的情况从不同的方向碰撞,你可以为你的船的不同部分创建一个矩形,就像它的身体和翅膀。

代码语言:javascript
复制
public Rectangle getRightWingBound(){}
public Rectangle getLeftWindBound(){}

诸若此类。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37581053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档