简体   繁体   English

Java中两个对象之间的碰撞检测

[英]Collision detection between two objects in java

I'm really struggling detecting collision between two Car objects of Car class. 我真的很难检测到Car类的两个Car对象之间的碰撞。 The code is pretty long so its hard to include it all. 该代码很长,因此很难将其全部包含在内。 I've included my collision method. 我已经包括了碰撞方法。 If you can spot any logic errors with it which I'm thinking is my issue that'd be great. 如果您能发现任何逻辑错误,而我认为这是我的问题,那将是个好消息。 If you need more context I asked an earlier similar question which includes a lot more of the code here , but i still dont have a solution.. 如果您需要更多一点,我问一个较早类似的问题,其中包括了很多的代码在这里 ,但我仍然没有一个解决方案..

What i want it to do is tell me when two cars are touching. 我想要做的是告诉我两辆车何时接触。 What it's doing is nothing but printing 'no collision' even when there are collisions. 它所做的只是打印“无冲突”,即使有冲突也是如此。

collision method (inside environment class) - 碰撞方法(环境类内部)-

public boolean collision() {

    double MIN_DIS = 0.1;
    for (Car a : cars) {
        for (Car b : cars) {
            if (!(a.equals(b)) && (Math.abs(a.getPosition() - b.getPosition()) < MIN_DIS)) {
                return true;
            }
        }
    }
    return false;
}

Calling method (inside main where i add cars) - 呼叫方式(在我添加汽车的主要区域内)-

    if(e.collision() == true) {
        System.out.println("collision");
    } else {
        System.out.println("no collision");
    } 

Equals method (i decided to use !(a == b) for now though) - 等于方法(虽然我现在决定使用!(a == b))-

public boolean equals(Object obj) {
    if (obj instanceof Car){
        Car car = (Car) obj;
        return car.getPosition() == this.position && car.getLane() == this.lane && car.getColor().equals(this.color) && car.getSpeed() == this.speed; 
    }
    return false;
}

You may want to change the detection 'range'. 您可能需要更改检测“范围”。 If you look at the way you're detecting your cars, you're only looking for the co-ordinate that the sprite/vector is positioned at which is going to be relatively hard for them to collide if there are more than a few pixels. 如果您查看检测汽车的方式,那么您只在寻找精灵/矢量所处的坐标,如果像素多于几个,它们将相对难以碰撞。 Try finding the position of Car A from the width/length of Car B . 尝试从Car Bwidth/length查找Car A的位置。

I think that the equality method is not correct. 我认为平等方法是不正确的。 And it causes some collision detection problems. 并且它引起一些碰撞检测问题。 We can not assume that two Car with the same property are the same object. 我们不能假定具有相同属性的两个Car是同一对象。 For example: 例如:

e.add(new Car(  0, 63, 2, new Color(1.0f,1.0f,1.0f, 1.0)));
e.add(new Car(  0, 63, 2, new Color(1.0f,1.0f,1.0f, 1.0)));

In this case there is a collision but it would never be detected since the two objects, through the equals method implemented, are the same. 在这种情况下,存在冲突,但是由于通过实现的equals方法实现的两个对象相同,因此永远不会检测到冲突。

In contexts like this, game engine or modelling system, it is useful to differentiate each object in your world with an ID, as each instance is unique. 在这种情况下,例如游戏引擎或建模系统,使用ID区分世界中的每个对象非常有用,因为每个实例都是唯一的。

Moreover as @Jason say, to correctly capture a collision you have to compute the length or the width of the object. 此外,如@Jason所说,要正确捕获碰撞,您必须计算对象的长度或宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM