2012-01-14 10 views
0

私はゲームエンジンを作成していますが、私はアクタークラスに問題があります。私は矩形(上)と矩形の一辺との衝突を判断したいと思います。私はそれらのために2つの方法を書いていました。Javaゲームの衝突検出(サイドコリジョン)が長方形で

public boolean isLeftCollision(Actor actor) { 
    boolean bool = false; 
    Rectangle LeftBounds = new Rectangle(x, y, x-velocity, image.getHeight(null)); 
    bool = LeftBounds.intersects(actor.getBounds()); 
    return bool; 
} 

public boolean isRightCollision(Actor actor) { 
    boolean bool = false; 
    Rectangle RightBounds = new Rectangle(x+image.getWidth(null), y, image.getWidth(null)+velocity, image.getHeight(null)); 
    bool = RightBounds.intersects(actor.getBounds()); 
    return bool; 
} 

ここで、速度は次のステップの動きです。

しかし、彼らはどちらも私にエラー(すなわち、誤った判断)を与えます。アクタークラスでこれをどのように解決できますか?

+0

(と仮定すると、速度が呼び出されます、あなたが移動する方向に対して左または右に移動する(正)までの距離、および唯一の方法です)エラーログを追加する –

+0

@stasエラーログを追加する方法 –

+0

lol。プログラムを実行し、エラーをコピーして貼り付けます。 –

答えて

1

私はあなたのコードをほとんど読んでいないと私は申し訳ありませんが、私の答えが役に立たない場合はごめんなさい。私の推測では、衝突の速度が誤差を生むということです。どのくらいの頻度でベロシティが保たれているかによって、まだ発生していない衝突を登録するかもしれません...

私は2つのステップで衝突検出を行います。それは上記または片側にかどう衝突

  • ため

    1. テスト決定します。他の俳優があなたに関連して配置されているあなたが見ることができ

      Rectangle self_shape=this.getBounds(); 
      Rectangle other_shape=actor.getBounds(); 
      bool collision = self_shape.intersects(other_shape); 
      if(collision){ 
          //create two new variables self_centerx and self_centery 
          //and two new variables other_centerx and other_centery 
          //let them point to the coordinates of the center of the 
          //corresponding rectangle 
      
          bool left=self_centerx - other_centerx<0 
          bool up=self_centery - other_centery<0 
      } 
      

      その方法:

    はここにいくつかの擬似コードです。もしそれが上または片側にあるならば。

  • +0

    あなたの答えをありがとう、それは働いた –

    1

    Rectangleの3番目のパラメータは、もう一方の側のxの幅ではなく、幅であることに注意してください。だから、あなたが本当にしたいことは、このように考えられます:

    public boolean isLeftCollision(Actor actor) { 
        return new Rectangle(x - velocity, y, velocity, image.getHeight(null)) 
         .intersects(actor.getBounds()); 
    } 
    
    public boolean isRightCollision(Actor actor) { 
        return new Rectangle(x + image.getWidth(null), y, velocity, image.getHeight(null)) 
         .intersects(actor.getBounds()); 
    }