C# 如何检测与颜色的碰撞?

C# 如何检测与颜色的碰撞?,c#,xna,collision-detection,C#,Xna,Collision Detection,如何检测与地图的碰撞?我想检测我的地图和我的车发生碰撞。扫描颜色 在我的底图上:跑道是灰色的,草地是白色的,墙壁是黄色的 在我上面的地图上:轨迹更真实,这是他们看到的 我试过了,但当我遇到冲突时,代码无法工作。很多东西在XNA4.0中不起作用,但这正是我可以使用的 //This method checks to see if the Sprite is going to move into an area that does //not contain all Gray pixels. If t

如何检测与地图的碰撞?我想检测我的地图和我的车发生碰撞。扫描颜色

在我的底图上:跑道是灰色的,草地是白色的,墙壁是黄色的

在我上面的地图上:轨迹更真实,这是他们看到的

我试过了,但当我遇到冲突时,代码无法工作。很多东西在XNA4.0中不起作用,但这正是我可以使用的

//This method checks to see if the Sprite is going to move into an area that does
//not contain all Gray pixels. If the move amount would cause a movement into a non-gray
//pixel, then a collision has occurred.
private bool CollisionOccurred(int aMove)
{

    //Calculate the Position of the Car and create the collision Texture. This texture will contain
    //all of the pixels that are directly underneath the sprite currently on the Track image.
    float aXPosition = (float)(-mCarWidth / 2 + mCarPosition.X + aMove * Math.Cos(mCarRotation));
    float aYPosition = (float)(-mCarHeight / 2 + mCarPosition.Y + aMove * Math.Sin(mCarRotation));
    Texture2D aCollisionCheck = CreateCollisionTexture(aXPosition, aYPosition);

    //Use GetData to fill in an array with all of the Colors of the Pixels in the area of the Collision Texture
    int aPixels = mCarWidth * mCarHeight;
    Color[] myColors = new Color[aPixels];
    aCollisionCheck.GetData<Color>(0, new Rectangle((int)(aCollisionCheck.Width / 2 - mCarWidth / 2), (int)(aCollisionCheck.Height / 2 - mCarHeight / 2), mCarWidth, mCarHeight), myColors, 0, aPixels);

    //Cycle through all of the colors in the Array and see if any of them
    //are not Gray. If one of them isn't Gray, then the Car is heading off the road
    //and a Collision has occurred
    bool aCollision = false;
    foreach (Color aColor in myColors)
    {
        //If one of the pixels in that area is not Gray, then the sprite is moving
        //off the allowed movement area
        if (aColor != Color.Gray)
        {
            aCollision = true;
            break;
        }
    }
    return aCollision;
}

我不知道如何解决你的特殊情况,但我只想推荐你

我相信他的教程中有一个有颜色检测功能,我想是他的2d坦克游戏


Mona

我不建议使用像素冲突,因为它是不必要的CPU和FPS杀手。 下面是一些可以帮助你的口语。但如果你真的需要像素冲突,首先检查rectange或任何其他冲突检测,然后如果intersect,则使用像素冲突来精确。。。但我不建议这样做,尤其是在快速游戏中

下面是一些可以帮助您的碰撞函数

多边形

圆圈:

int circlesColliding(int x1,int y1,int radius1,int x2,int y2,int radius2)
{
    //compare the distance to combined radii
    int dx = x2 - x1;
    int dy = y2 - y1;
    int radii = radius1 + radius2;
    if ( ( dx * dx )  + ( dy * dy ) < radii * radii ) 
    {
        return true;
    }
    else
    {
        return false;
    }
}
int圈碰撞(int-x1,int-y1,int-radius1,int-x2,int-y2,int-radius2)
{
//将距离与组合半径进行比较
int dx=x2-x1;
int dy=y2-y1;
内半径=半径1+半径2;
如果((dx*dx)+(dy*dy)<半径*半径)
{
返回true;
}
其他的
{
返回false;
}
}
圆到矩形

bool intersects(CircleType circle, RectType rect)
{
    circleDistance.x = abs(circle.x - rect.x);
    circleDistance.y = abs(circle.y - rect.y);

    if (circleDistance.x > (rect.width/2 + circle.r)) { return false; }
    if (circleDistance.y > (rect.height/2 + circle.r)) { return false; }

    if (circleDistance.x <= (rect.width/2)) { return true; } 
    if (circleDistance.y <= (rect.height/2)) { return true; }

    cornerDistance_sq = (circleDistance.x - rect.width/2)^2 +
                         (circleDistance.y - rect.height/2)^2;

    return (cornerDistance_sq <= (circle.r^2));
}
bool相交(圆形、圆形、矩形)
{
圆距离x=绝对值(圆x-矩形x);
圆距离y=绝对值(圆y-直线y);
if(circleDistance.x>(rect.width/2+circle.r)){return false;}
if(circleDistance.y>(rect.height/2+circle.r)){return false;}

if(circleDistance.x)你看过微软游戏教程吗?碰撞通常是通过在你的对象周围放置一个矩形,看看它是否与另一个对象相交(该对象周围也有一个矩形)来完成的。您正在查找每像素碰撞检测。
。不要使用像素碰撞,这是FPS和CPU杀手。请使用矩形、圆形和多边形碰撞。
bool intersects(CircleType circle, RectType rect)
{
    circleDistance.x = abs(circle.x - rect.x);
    circleDistance.y = abs(circle.y - rect.y);

    if (circleDistance.x > (rect.width/2 + circle.r)) { return false; }
    if (circleDistance.y > (rect.height/2 + circle.r)) { return false; }

    if (circleDistance.x <= (rect.width/2)) { return true; } 
    if (circleDistance.y <= (rect.height/2)) { return true; }

    cornerDistance_sq = (circleDistance.x - rect.width/2)^2 +
                         (circleDistance.y - rect.height/2)^2;

    return (cornerDistance_sq <= (circle.r^2));
}