2012-05-01 38 views
0

ここで衝突検出を実装する必要があります。私がここに持っているオブジェクトは、ボール/円と長方形です。ボールは垂直に移動し、矩形は水平に移動します。ボールと矩形が互いに接触した場合、イベントが発生する必要があります。私は同僚としばらくはやっているが、成功していない。これはC#の私の最初のプログラムですので、私に同行してください。ここ は私のコードです:円と半径の衝突

public partial class Form1 : Form 
{ 
    bool collided = false; 


    Player player; 
    List<Ball> balls; 
    const int fps = 60; 
    public Form1() 
    { 
     InitializeComponent(); 

     balls = new List<Ball>(); 
     Random r = new Random(); 

     for(int i =0; i<1;i ++) 
     { 
      balls.Add(new Ball(Width, Height,r)); 
     } 

     var task = new Task(Run); 
     task.Start(); 

     player = new Player() 
     { 
      x = this.Width/2, 
      y = (Height*9/10), 

      xvel = 10, 
      brush = Brushes.Black, 
     }; 

    } 

    protected void Run() 
    { 
     while (true) 
     { 
      for(int i = 0; i < balls.Count; i++) 
      { 
       balls[i].Move(this.Width);      
      } 
      this.Invalidate(); 
      Thread.Sleep(1000/fps); 

     } 
    } 


    private void Form1_Paint(object sender, PaintEventArgs e) 
    { 
     Graphics g = e.Graphics; 
     g.Clear(Color.White); 
     for(int i = 0; i < balls.Count; i++) 
     {    
      balls[i].Draw(g);    
     }    
     player.DrawPlayer(g);  

    } 

//This is the part where i was trying to check collision between the circle and a ball 
    private void CheckCollision(PaintEventArgs e) 
    { 

     if (player.IntersectsWith(balls)) 
     { 
      player.Intersect(balls); 
      if (!player.IsEmpty) 
      { 
       collided = true; 
       MessageBox.Show("collision detected"); 
      } 
     } 
    }   
} 



public class Player 
{   
    public float x, y, xvel; 
    public Brush brush; 

    public Player() 
    { 

    } 

    public void DrawPlayer(Graphics g) 
    { 
     g.FillRectangle(brush, new RectangleF(x, y, 30,30));  
    } 

    public void MovePlayerLeft(int gameWidth) 
    { 
     if (x > 0) 
     { 
      x -= xvel; 
     } 
    } 
    public void MovePlayerRight(int gameWidth) 
    {    
     if (x < gameWidth-47) 
     { 
      x += xvel; 
     } 
    }  
} 

public class Ball 
{ 

    public float x, y, yvel, radius; 
    public Brush brush; 
    public Ball(int gamewidth,int gameHeight,Random r)   

    { 

     x = r.Next(gamewidth); 
     y = r.Next(gameHeight); 

     yvel = r.Next(2) + 5; 

     radius = r.Next(10) + 5; 
     brush = new SolidBrush(Color.Blue); 

    } 

    public void Move(int gameHeight) 

    {     
     if (y + radius >= gameHeight) 
     { 
      y = 0;    
     } 
     y += yvel; 


    } 


    public void Draw(Graphics g) 
    { 
     g.FillEllipse(brush, new RectangleF(x-radius,y - radius, radius * 2, radius * 2)); 
    } 


} 
+1

ようこそスタックオーバーフロー。私はちょうど質問することができます - 質問は何ですか?そして私は実際の交差点のコードを見逃しましたか?それが機能していない場合は、それを確認する必要があります。同様に、我々はおそらく他のものを見る必要はありません - あなたは私たちにそこにいくつかの壁の壁を与えました:) –

+0

ボールのxが<= 0 or x > = maxWidthOfScreenで、yが<= 0 or y > = maxHeightOfScreenの場合はチェックできませんか? –

+0

あなたはあなたのコードを書くいくつかのボディを作ることがないようにあなた自身の共謀を試みるべきです:) –

答えて

1

あなたは、長方形や円が交差しているかどうかを把握しようとしている場合は、四辺のそれぞれについて、このアルゴリズムを試してみてください。

Circle line-segment collision detection algorithm?

あなたはコーナーがサークルの内側にあるかどうかをチェックすることで、これを高速化できます。

また、矩形内の完全な円とその逆の場合も、おそらく衝突としてカウントされるはずです。