2017-01-12 8 views
0

パドルの四角形を分割する方法を理解しようとしています。ヒット。ボールがパドルの左右いずれかに当たった場合、毎回まったく同じパスをたどるのではなく、ボールの方向がわずかに変化するようにしたい。ここでパドルの位置をパドルで定義してより良いボールコリジョンフィジックスを作成しようとしました

は私のパドルクラスは、(私はisboxcolliding方法で必要となるコードを書き始めたが立ち往生し、混乱しました)です:

class Ball 
{ 
    Texture2D texture; 
    Vector2 position; 
    Vector2 speed; 
    Rectangle bounds; 
    Rectangle screenBounds; 
    Random rnd = new Random(); 
    bool collisionWithBrick; // Prevents rapid brick removal. 



    public Ball(Texture2D texture, Rectangle screenbounds) // Constructor 
    { 
     this.texture = texture; 
     this.screenBounds = screenbounds; 
    } 

    public int Width 
    { 
     get { return texture.Width; } 
    } 

    public int Height 
    { 
     get { return texture.Height; } 
    } 

    public Rectangle Bounds 
    { 
     get 
     { 
      bounds = new Rectangle((int)position.X, (int)position.Y, 
       Width, Height); 

      return bounds; 
     } 
    } 

    public void SetStartBallPosition(Rectangle paddle) 
    { 
     position.X = paddle.X + (paddle.Width - Width)/2; 
     position.Y = paddle.Y = paddle.Y - Height; 
     bounds = Bounds; 

     if (rnd.Next(0, 2) < 1) 
      speed = new Vector2(-200.0f, 200.0f); // Move ball left. 
     else 
      speed = new Vector2(200.0f, -200.0f); // Move Ball Right. 
    } 

    internal void Deflection(MultiBallBrick multiBallBrick) 
    { 
     if (!collisionWithBrick) 
     { 
      speed.Y *= -1; 
      collisionWithBrick = true; 
     } 
    } 

    internal void Deflection(ReinforcedBrick reinforcedBrick) 
    { 
     if (!collisionWithBrick) 
     { 
      speed.Y *= -1; 
      collisionWithBrick = true; 
     } 
    } 


    public void Draw(SpriteBatch spriteBatch) 
    { 
     spriteBatch.Draw(texture, position, Color.Silver); 
    } 


    // Check if our ball collides with the right paddle. 
    public void IsBoxColliding(Rectangle paddle) 
    { 

     if (bounds.Intersects(paddle)) 
     { 
      int center = paddle.Center.X - bounds.Center.X; 


      if (center >) 
      { 
       speed.Y *= -1; 



       speed.X = speed.X * 1.5f; 
      } 
     } 

     /*if (paddle.Intersects(bounds)) // Bounds = our ball 
     { 
      position.Y = paddle.Y - Height; 
      speed.Y *= - 1; // Reverse the direction of the ball. 
     }*/ 
    } 

    public void Update(GameTime gameTime) 
    { 
     collisionWithBrick = false; 
     position += speed * (float)gameTime.ElapsedGameTime.TotalSeconds; 


     // Check to see if the ball goes of the screen. 
     if (position.X + Width > screenBounds.Width) // Right side 
     { 
      speed.X *= -1; 
      position.X = screenBounds.Width - Width; 
     } 

     if (position.X < 0) // Left side 
     { 
      speed.X *= -1; 
      position.X = 0; 
     } 

     if (position.Y < 0) // Top 
     { 
      speed.Y *= -1; 
      position.Y = 0; 
     } 
    } 

    public bool OffBottom() 
    { 
     if (position.Y > screenBounds.Height) 
      return true; 

     return false; 
    } 

    public void Deflection(Brick brick) 
    { 
     if (!collisionWithBrick) 
     { 
      speed.Y *= -1; 
      collisionWithBrick = true; 
     } 
    } 
} 

}

任意のヘルプは大幅にapreciatedされるだろう!

答えて

0

必ずしも衝突矩形を分割する必要はありません。次の情報を持つことで、衝突が左側か右側にあるかどうかを確認できます。

  • 衝突点やボールの中心位置衝突が発生し、パドルのVector2 collisionPoint
  • センター位置にそれを呼び出すことができ、場合のみ機能Vector2 paddleCentrePosition

それを呼び出すことができますパドルは回転しません。

Vector2 difference = collisionPoint - paddleCentrePosition; 
if(difference.x > 0) { 
    /*Right side of the paddle*/ 
} 
else if(difference.x < 0) { 
    /*Left side of the paddle*/ 
} 
else{ 
    /*The exact same position*/ 
} 
+0

本当に助かりました。それは私が考えていたものの、私の頭を丸めることはできませんでした。しかし、私は2番目のレベルにあるマルチブロックレンガを持っていて、何らかの理由で2番目のボールが生成され、もはやパドルと実際にはやり取りしません(実際にレンガとやり取りすることもありません)。それがどうして起こったのだろうか?なぜなら、第2のボールが第1のボールのように衝突するように設定されているからです(これは前に衝突していたものです)。 – ESuth

+0

@ESuthいいえ。 isboxcollidingがまったく呼び出されていないか確認してください。コンソール印刷を行います。 – Vermacian55

+0

これは間違いなく呼び出されています。私は、基本的には、マルチクラスが打たれたときに、私のボールクラスの別のバージョンを生成しています。私はマルチボールのためにまったく新しいクラスを作る必要はないのですか? – ESuth

関連する問題