不同的对象发生碰撞 [英] Different objects collide

查看:95
本文介绍了不同的对象发生碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚要检查,如果两个对象发生碰撞,在这种情况下,它是一个圆和第二个是一个正方形。在code我使用的作品完美,但它只是检查下/ BUTTOM 方碰撞的一侧,请帮我纠正它,我可以能够检查各方面的冲突。

I just want to check if two objects collide, in this case it's a circle and the second one is a square shape. The code i'm using works perfectly, but it only check if the right and down/buttom side of the square collide, please help me to correct it so i can be able to check all sides collision.

问题是,我想只是为了检查广场的四面八方,如果它与圆碰撞,但只检查双方具有了以下功能下降:

The problem is that i want just to check all sides of the square if it collide with the circle, but it only check two sides with the function down below:

 bool Collision(int circleX, int circleY, int radius, 
                   int squareX, int squareY, int width, int height)
    {

        double distance = 0;

        //get circle of square
        double center_square_x = (double)(squareX + squareX + width)/2;
        double center_square_y = (double)(squareY + squareY + height)/2;

        //check for whether circle fully located inside square
        if (circleX >= squareX && circleX <= squareX + width
            && circleY >= squareY && circleY <= squareY + height)
            return true;

        distance =  pow (circleX - center_square_x,2.0) 
                  + pow(circleY - center_square_y,2.0);

        if( distance <= pow(radius, 2.0))
           return true; 
       else    
           return false;
     }

显示的自己做错了什么照片:

在圆顶左侧,仍然没有触及到广场:

When the circle goes against the left and still didn't touch the square:

现在,当它触及因为希望它返回true方:

and now when it touch the square it return true as wanted:

在圆违背了权利,仍然没有触及到广场就返回false:

When the circle goes against the right and still didn't touch the square it return false:

现在,当它接触到广场它仍然返回false,这是错误的:

and now when it touch the square it return still false, which is wrong:

在圈上升到广场BUTTOM和触摸它返回true,这是正确的:

When the circle goes up to the buttom of the square and touch- it return true, which is right:

但是,当圆下降到广场的顶部,触摸它返回false,这是错误的:

But when the circle goes down to the top of the square and touch- it return false, which is wrong:

推荐答案

试试这个

bool Collision(int circleX, int circleY, int radius, 
               int squareX, int squareY, int width, int height)
{

    double distance = 0;

    //get circle of square (the center of the rectangle or square is
    // (squareX + width)/2 and (squareY+height)/2

    double center_square_x = (double)(squareX + width)/2;
    double center_square_y = (double)(squareY + height)/2;

    // check for each segment the circle position

    // check if the circle is between the bottom and upper square segments
    if (circleY >= squareY && circleY <= squareY + height) 
           // check for left segment
           if (circleX >= squareX && circleX < centerX )
           return true;
           // check for right segment
       else if (circleX <= squareX+width && circleX > centerX)
           return true;
     // check if the circle is between left and right square segments
    else if (circleX >= squareX && circleX <= squareX + width)
         // check for upper segment
         if (circleY >= squareY && circleY < centerY)
           return true;
         // check for bottom segment
        else if (circleY <= squareY + height && circleY > centerY)
           return true;

我不知道你想实现与距离计算什么,但它不会得到有..因为你检查碰撞的功能将退出它的范围后,当你返回true ..

I don't know what you're trying to achieve with the distance calculation but it won't get there.. because when you're returning true after you check the collision the function will exit from it's scope..

这篇关于不同的对象发生碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆