Collision detection in 2D game without tiles XNA

user3313308

I'm working on a XNA platform game and would need some help with the collision. The game takes place in a cave and the problem is that the art style will be sketchy and therefore the terrain( the caves) will differ a LOT, so I can't use tiles. But I need to check pixel perfect collision on the character and the cave but I can't figure out how to do so when I can't place rectangles around every tiles since there are none.

I have thought about it a lot came up with some ideas:

-One big rectangle around the whole level and one around the character and use pixel perfect collision. But I don't think that will work since the rectangle will include the background as well.

-Place out rectangles manually. Very ugly code and may cause a lot of bugs and errors.

-Use tiles anyway and have hundreds of tile types. Again, very ugly code and it just seems wrong.

-Use a collision engine. I would preferably make the game from scratch.

I'm sorry if I've explained badly, but it's a rather complicated problem(for me at least) and I can't find any solution around the net. Would be very glad for any ideas, thank you.

davidsbro

I think you should use per-pixel collision detection to do what you're trying to. You could have your character, and make his texture transparent (using the image's alpha) except for the actual character. Then you could have the terrain texture which would be your cave. Make the parts of the cave the character should be able to move transparent, so that you can have another texture which will be the background of the whole level. Here's a rough example:

Character (pink BG is transparent:)

enter image description here

Cave (white is transparent)

enter image description here

Background:

enter image description here

All three added together:

enter image description here

That's just to give you a very rough idea (since i just googled the background and drew the cave in paint). Then you could use alpha pixel collision detection between the cave texture (NOT the cave background) and the character texture. See this for a tutorial on how you could easily use per-pixel collision. HTH. Here is some collision code you could use (rectangleA should be the character's rectangle, dataA the character's pixel Data, rectangleB the rectangle of the cave (which will probably be the whole screen), and dataB the pixel data of the cave (NOT THE BACKGROUND)) Since you aren't using the pixel data of the background image, the collision won't be checked with this data:

    /// <param name="rectangleA">Bounding rectangle of the first sprite</param>
    /// <param name="dataA">Pixel data of the first sprite</param>
    /// <param name="rectangleB">Bouding rectangle of the second sprite</param>
    /// <param name="dataB">Pixel data of the second sprite</param>
    /// <returns>True if non-transparent pixels overlap; false otherwise</returns>
    static bool IntersectPixels(Rectangle rectangleA, Color[] dataA,
                                Rectangle rectangleB, Color[] dataB)
    {
        // Find the bounds of the rectangle intersection
        int top = Math.Max(rectangleA.Top, rectangleB.Top);
        int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
        int left = Math.Max(rectangleA.Left, rectangleB.Left);
        int right = Math.Min(rectangleA.Right, rectangleB.Right);

        // Check every point within the intersection bounds
        for (int y = top; y < bottom; y++)
        {
            for (int x = left; x < right; x++)
            {
                // Get the color of both pixels at this point
                Color colorA = dataA[(x - rectangleA.Left) +
                                     (y - rectangleA.Top) * rectangleA.Width];
                Color colorB = dataB[(x - rectangleB.Left) +
                                     (y - rectangleB.Top) * rectangleB.Width];

                // If both pixels are not completely transparent,
                if (colorA.A != 0 && colorB.A != 0)
                {
                    // then an intersection has been found
                    return true;
                }
            }
        }

        // No intersection found
        return false;
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Collision detection in 2D game without tiles XNA

From Dev

XNA 2D object collision (without Tiles/Grid)

From Dev

Collision detection in java 2d game

From Dev

XNA Collision detection with Intersects()

From Dev

HTML Canvas game: 2D collision detection

From Dev

XNA 3D Bounding Box Collision Detection Method

From Dev

XNA 3D Bounding Box Collision Detection Method

From Dev

Collision detection in javascript game

From Dev

XNA - Farseer Physics 3.5 - Collision Detection Issues - No/Zero Gravity - Space Game

From Dev

XNA Tile based Game: Checking collision between 2 objects using their grid position inside a 2D array

From Dev

Collision Detection between two textures in XNA

From Dev

Javascript Canvas Game - Collision Detection

From Dev

2D Collision detection not working

From Dev

Collision detection not working in Unity 2D

From Dev

Unity 2d collision detection not registering

From Dev

Trouble with java 2D collision detection

From Dev

Unity 2D Collision Detection

From Dev

Solving 2d game collision (polygons)

From Dev

Collision Detection on 2 objects

From Dev

Collision detection in javascript canvas tanks game

From Dev

Java Game Physics - Determine intersection and collision detection

From Dev

Trying to use quadtrees for collision detection in a game

From Dev

Game score via collision detection increment

From Dev

Trying to use quadtrees for collision detection in a game

From Dev

Collision Detection for fast moving game object in Unity

From Dev

Problem with collision detection is JS canvas game

From Dev

Applying gravity to a 2D array in XNA game

From Dev

How to make 2D color tiles for the 2048 game

From Dev

What is the logic behind collision detection in 2d games?

Related Related

  1. 1

    Collision detection in 2D game without tiles XNA

  2. 2

    XNA 2D object collision (without Tiles/Grid)

  3. 3

    Collision detection in java 2d game

  4. 4

    XNA Collision detection with Intersects()

  5. 5

    HTML Canvas game: 2D collision detection

  6. 6

    XNA 3D Bounding Box Collision Detection Method

  7. 7

    XNA 3D Bounding Box Collision Detection Method

  8. 8

    Collision detection in javascript game

  9. 9

    XNA - Farseer Physics 3.5 - Collision Detection Issues - No/Zero Gravity - Space Game

  10. 10

    XNA Tile based Game: Checking collision between 2 objects using their grid position inside a 2D array

  11. 11

    Collision Detection between two textures in XNA

  12. 12

    Javascript Canvas Game - Collision Detection

  13. 13

    2D Collision detection not working

  14. 14

    Collision detection not working in Unity 2D

  15. 15

    Unity 2d collision detection not registering

  16. 16

    Trouble with java 2D collision detection

  17. 17

    Unity 2D Collision Detection

  18. 18

    Solving 2d game collision (polygons)

  19. 19

    Collision Detection on 2 objects

  20. 20

    Collision detection in javascript canvas tanks game

  21. 21

    Java Game Physics - Determine intersection and collision detection

  22. 22

    Trying to use quadtrees for collision detection in a game

  23. 23

    Game score via collision detection increment

  24. 24

    Trying to use quadtrees for collision detection in a game

  25. 25

    Collision Detection for fast moving game object in Unity

  26. 26

    Problem with collision detection is JS canvas game

  27. 27

    Applying gravity to a 2D array in XNA game

  28. 28

    How to make 2D color tiles for the 2048 game

  29. 29

    What is the logic behind collision detection in 2d games?

HotTag

Archive