【问题标题】:Collision between circle and a radius圆和半径之间的碰撞
【发布时间】:2012-05-01 18:12:27
【问题描述】:

我需要在这里实现碰撞检测。我这里的对象是一个球/圆和一个矩形。球垂直移动,而矩形水平移动。条件是如果球和矩形相互接触,那么应该引发一个事件。我和我的同事一直在尝试这样做,但没有成功。这是我在 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));
    }


}

【问题讨论】:

  • 欢迎来到 Stack Overflow。我可以问 - 问题是什么?我错过了实际的交叉路口代码吗?如果那是行不通的,那么我们需要看到这一点。同样 - 我们可能不需要查看其他所有内容 - 你已经给了我们几堵代码墙 :)
  • 你不能检查球的 x = maxWidthOfScreen 和 y = maxHeightOfScreen 吗?
  • 您应该尝试自己的勾结,不要让其他人编写您的代码:)

标签: c#


【解决方案1】:

如果您想确定一个矩形和一个圆是否相交,请尝试对四个边中的每一个边使用此算法:

Circle line-segment collision detection algorithm?

您可以通过检查角是否在圆内来加快速度。

另外,请记住,完全在矩形内的圆(反之亦然)应该算作碰撞。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 2014-05-05
    • 2023-01-26
    相关资源
    最近更新 更多