2013-10-17 39 views
0

如何檢測與我的地圖的碰撞?我想檢測與我的地圖和我的車相撞。掃描顏色。如何檢測與顏色的碰撞?

在我的底圖上:軌道是灰色的,草地是白色的,牆壁是黃色的。

在我的上圖:軌道更現實,這是他們看到的。

我試過http://www.xnadevelopment.com/tutorials/theroadnottaken/theroadnottaken.shtml但是當我遇到碰撞時,代碼將無法工作。很多這些在XNA 4.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; 
} 

第2部分

//Create the Collision Texture that contains the rotated Track image for determing 
//the pixels beneath the Car srite. 
private Texture2D CreateCollisionTexture(float theXPosition, float theYPosition) 
{ 
    //Grab a square of the Track image that is around the Car 
    graphics.GraphicsDevice.SetRenderTarget(0, mTrackRender); 
    graphics.GraphicsDevice.Clear(ClearOptions.Target, Color.Red, 0, 0); 

    mSpriteBatch.Begin(); 
    mSpriteBatch.Draw(mTrack, new Rectangle(0, 0, mCarWidth + 100, mCarHeight + 100), 
     new Rectangle((int)(theXPosition - 50), 
     (int)(theYPosition - 50), mCarWidth + 100, mCarHeight + 100), Color.White); 
    mSpriteBatch.End(); 

    graphics.GraphicsDevice.ResolveRenderTarget(0); 
    graphics.GraphicsDevice.SetRenderTarget(0, null); 

    Texture2D aPicture = mTrackRender.GetTexture(); 

    //Rotate the snapshot of the area Around the car sprite and return that 
    graphics.GraphicsDevice.SetRenderTarget(0, mTrackRenderRotated); 
    graphics.GraphicsDevice.Clear(ClearOptions.Target, Color.Red, 0, 0); 

    mSpriteBatch.Begin(); 
    mSpriteBatch.Draw(aPicture, new Rectangle((int)(aPicture.Width/2), (int)(aPicture.Height/2), 
     aPicture.Width, aPicture.Height), new Rectangle(0, 0, aPicture.Width, aPicture.Width), 
     Color.White, -mCarRotation, new Vector2((int)(aPicture.Width/2), (int)(aPicture.Height/2)), SpriteEffects.None, 0); 
    mSpriteBatch.End(); 

    graphics.GraphicsDevice.ResolveRenderTarget(0); 
    graphics.GraphicsDevice.SetRenderTarget(0, null); 

    return mTrackRenderRotated.GetTexture(); 
} 
+0

你看過任何的MS遊戲教程嗎?碰撞通常是通過在對象周圍放置一個矩形並查看它是否與另一個對象(它周圍也有一個矩形)相交來完成的。 – gleng

+0

您正在尋找'每像素碰撞檢測'。 – user1306322

+0

不使用像素衝突,它是FPS和CPU殺手。使用矩形,圓形,多邊形碰撞。 –

回答

1

我不建議對像素碰撞,因爲它贅述CPU和FPS殺手。 這裏有一些collsions,可以幫助你。但如果你真的需要像素碰撞,首先檢查rectange或任何其他碰撞檢測,然後如果相交,然後使用像素collsion預警...但我不建議,尤其是在快速遊戲。

這裏有一些衝突的功能,可以幫助您

POLYGON

http://www.codeproject.com/Articles/15573/2D-Polygon-Collision-Detection

CIRCLE:

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; 
    } 
} 

圓矩形

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)); 
} 
1

我不知道如何解決你的具體情況,但我只是要推薦Riemers

我敢肯定,他的教程的一個有顏色檢測,我認爲它是他的2D坦克遊戲。

蒙娜麗莎