0

円形のスプライトがあり、それらが他の円と衝突するかどうかを確認する必要があります。私は試した:

public boolean collision(){
    boolean collide=false;

    if(spriteNum>0)
        for(int x=0;x<spriteNum;x++)
            if(yourSprite[spriteNum].collidesWith(yourSprite[x]))
                collide=true;
    return collide;
}

しかし、それはその周りに長方形を作成し、それがそれを台無しにします。距離の式を使用して、2 つのスプライトが接触しているかどうかを手動で計算することもできますが、それは負担が大きいように思われ、各スプライトには円の物理体が取り付けられています。つまり、中心が常に移動していることを意味します (中心を見つける方法がわかりません)。 . 何か案は?

4

3 に答える 3

2

Alexandru が指摘しているように、これまで AndEngine でサポートされている円衝突検出はありません。最良の方法は、自分で実装することです。彼のソリューションは問題なく(高速に)機能しますが、もう少し精度が必要な場合に備えて、別の概算を投稿します。

// There is no need to use Sprites, we will use the superclass Entity
boolean collidesWith(Entity circle){
    final float x1 = this.getX();
    final float y1 = this.getY();
    final float x2 = circle.getX();
    final float y2 = circle.getY();
    final float xDifference = x2 - x1;
    final float yDifference = y2 - y1;

    // The ideal would be to provide a radius, but as
    // we assume they are perfect circles, half the
    // width will be just as good
    final float radius1 = this.getWidth()/2;
    final float radius2 = circle.getWidth()/2;

    // Note we are using inverseSqrt but not normal sqrt,
    // please look below to see a fast implementation of it.
    // Using normal sqrt would not need "1.0f/", is more precise
    // but less efficient
    final float euclideanDistance = 1.0f/inverseSqrt(
                                            xDifference*xDifference +
                                            yDifference*yDifference);
    return euclideanDistance < (radius1+radius2);
}

/**
* Gets an aproximation of the inverse square root with float precision.
* @param x float to be square-rooted
* @return an aproximation to sqrt(x)
*/
public static float inverseSqrt(float x) {
    float xhalf = 0.5f*x;
    int i = Float.floatToIntBits(x);
    i = 0x5f3759df - (i>>1);
    x = Float.intBitsToFloat(i);
    x = x*(1.5f - xhalf*x*x);
    return x;
}

注: 私は高速な inverseSqrt メソッドの作成者ではありません。これは、浮動小数点表現 ( IEEE 754 浮動小数点表現Java float to byte 表現を参照) により、Java (より正確には Android) で動作します。

詳細な調査については、以下を参照してください。

于 2012-11-22T14:40:58.200 に答える
1

Andengineには円の衝突検出がないため、唯一の方法はそれらの間の距離を計算することです

boolean collidesWithCircle(Sprite circle) {
    float x1 = this.getX();
    float y1 = this.getY();
    float x2 = circle.getX();
    float y2 = circle.getY();
    double a = x1 - x2;
    double b = y1 - y2;
    double c = (a * a) + (b * b);

    if (c <= this.getWidth()*this.getWidth())
        return true;
    else return false;
}
于 2012-06-21T08:54:48.220 に答える
0

物理ワールドを使用している場合は、PhysicsFactory.createCircularBody() メソッドを使用して円形のボディを作成できます。

于 2012-08-16T16:08:28.950 に答える