4 Replies - 828 Views - Last Post: 09 September 2012 - 04:30 AM

#1 gamecampaign  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 06-September 12

Collision Detection With Map Not Working!

Posted 08 September 2012 - 09:25 PM

I have a map generator and it just generates tiles. For the collision system I'm using bounding boxes. Currently I have it search through every sprite's rectangle and see if it collides with players every time i press a key and if collided it will push the player back. But the player sometimes gets stuck inside even though its pushing back and i use a different key and it pushes him more into the rock because hes stuck. Is there a way around this, or even a better way to check collisions other then cycling through every rectangle in the whole game?

Is This A Good Question/Topic? 0
  • +

Replies To: Collision Detection With Map Not Working!

#2 LiberLogic969  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 85
  • Joined: 03-September 12

Re: Collision Detection With Map Not Working!

Posted 09 September 2012 - 12:30 AM

Can you post your collision code?
Was This Post Helpful? 0
  • +
  • -

#3 gamecampaign  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 06-September 12

Re: Collision Detection With Map Not Working!

Posted 09 September 2012 - 12:51 AM

View PostLiberLogic969, on 09 September 2012 - 12:30 AM, said:

Can you post your collision code?



  public int CheckCollision()
        {
            float a2 = 123;
            playerbox = new GameObject(playertexture2, position);
            int ps;
            Vector2 pos = playerbox.Position;
            Vector2 left = Vector2.Add(pos, new Vector2(-1, 0));
            Vector2 right = Vector2.Add(pos, new Vector2(1, 0)); 
            Vector2 up = Vector2.Add(pos, new Vector2(0, -1));
            Vector2 down = Vector2.Add(pos, new Vector2(0, -1));
            KeyboardState ks1 = Keyboard.GetState();
            if (ks1.GetPressedKeys().Length == 0)
            {
                foreach (KeyValuePair<Vector2, Rectangle> pair in grassblock)
                {
                    Rectangle a = pair.Value;
                    Vector2 c = pair.Key;
                    playerbox.BoundingBox1.Offset(0, 48);
                    if (a.Intersects(playerbox.BoundingBox1))
                    {
                        col++;
                    }
                    else
                    {
                    }
                }
                foreach (KeyValuePair<Vector2, Rectangle> pair in grassblock)
                {
                    Rectangle a = pair.Value;
                    Vector2 b = pair.Key;
                    playerbox.BoundingBox1.Offset(0, 48);
                    if (a.Intersects(playerbox.BoundingBox1))
                    {
                        col++;
                    }
                    else
                    {
                    }
                }
                if (col == 0)
                {
                    return 0;
                }

                else
                    col = 0;
                return 1;
            }
            else
                  if (ks1.IsKeyDown(Keys.Left))
                   {
                       foreach (KeyValuePair<Vector2, Rectangle> pair in grassblock)
                       {
                           Rectangle a = pair.Value;
                           Vector2 b = pair.Key;
                           if (a.Intersects(playerbox.BoundingBox1))
                           {
                               col++;
                           }
                           else
                           {
                           }
                       }
                       foreach (KeyValuePair<Vector2, Rectangle> pair in grassblock)
                       {
                           Rectangle a = pair.Value;
                           Vector2 b = pair.Key;
                           if (a.Intersects(playerbox.BoundingBox1))
                           {
                               col++;
                           }
                           else
                           {
                           }
                       }
                       if (col == 0)
                       {
                           return 0;
                       }

                       else
                           col = 0;
                       return 1;
                   }
                  else
                   if (ks1.IsKeyDown(Keys.Right))
                   {
                       foreach (KeyValuePair<Vector2, Rectangle> pair in grassblock)
                       {
                           Rectangle a = pair.Value;
                           Vector2 b = pair.Key;
                           if (a.Intersects(playerbox.BoundingBox1))
                           {
                               col++;
                           }
                           else
                           {
                           }
                       }
                       foreach (KeyValuePair<Vector2, Rectangle> pair in grassblock)
                       {
                           Rectangle a = pair.Value;
                           Vector2 b = pair.Key;
                           if (a.Intersects(playerbox.BoundingBox1))
                           {
                               col++;
                           }
                           else
                           {
                           }
                       }
                       if (col == 0)
                       {
                           return 0;
                       }

                       else
                           col = 0;
                       return 1;
                   }
        else
            
                   if (ks1.IsKeyDown(Keys.Up))
                   {
                       foreach (KeyValuePair<Vector2, Rectangle> pair in grassblock)
                       {
                           Rectangle a = pair.Value;
                           Vector2 b = pair.Key;
                           if (a.Intersects(playerbox.BoundingBox1))
                           {
                               col++;
                           }
                           else
                           {
                           }
                       }
                       foreach (KeyValuePair<Vector2, Rectangle> pair in grassblock)
                       {
                           Rectangle a = pair.Value;
                           Vector2 b = pair.Key;
                           if (a.Intersects(playerbox.BoundingBox1))
                           {
                               col++;
                           }
                           else
                           {
                           }
                       }
                       if (col == 0)
                       {
                           return 0;
                       }

                       else
                           col = 0;
                       return 1;
                   }
else
                       if (ks1.IsKeyDown(Keys.Down))
                       {
                           foreach (KeyValuePair<Vector2, Rectangle> pair in grassblock)
                           {
                               Rectangle a = pair.Value;
                               Vector2 b = pair.Key;
                               if (a.Intersects(playerbox.BoundingBox1))
                               {
                                   col++;
                               }
                               else
                               {
                               }
                           }
                           foreach (KeyValuePair<Vector2, Rectangle> pair in grassblock)
                           {
                                                              Rectangle a = pair.Value;
                               Vector2 b = pair.Key;
                               if (a.Intersects(playerbox.BoundingBox1))
                               {
                                   col++;
                               }
                               else
                               {
                               }
                           }
                           if (col == 0)
                           {
                               return 0;
                           }

                           else
                               col = 0;
                           return 1;
                       }
                       else
                           return 0;








        }

its a bit messy, sorry :P
Was This Post Helpful? 0
  • +
  • -

#4 LiberLogic969  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 85
  • Joined: 03-September 12

Re: Collision Detection With Map Not Working!

Posted 09 September 2012 - 02:44 AM

Hello,

So if this method returns 1 you stop the player from moving? What is the Vector2 b for?

There are a few other types of collision detection you could implement for tiles. If you are using a 2D grid you could give each cell an isSolid flag and only check the tiles that are directly around the player. If the player wants to move into another cell, check if that cell is solid. This method might change the way you handle moving your player around though since you would only be able to move within cells. Another thing you should look into is Seperating Axis Thereom. SAT will give you a Minimum Translation Vector that you could use to seperate the rectangles from eachother if they collide. Its very fast and the concept is intuitive.

Oh, I forgot to ask and i dont know how to edit my post lol...
How do you actually handle a collision? As in, when the method you posted returns 1, what do you do to the player?
Was This Post Helpful? 0
  • +
  • -

#5 gamecampaign  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 06-September 12

Re: Collision Detection With Map Not Working!

Posted 09 September 2012 - 04:30 AM

I got it all working. Just had to make the player stop 2 pixels before the actual collision happens when being influenced by gravity.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1