6 Replies - 1056 Views - Last Post: 22 December 2016 - 10:20 AM

#1 AnonymousUser1508  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-April 16

Determine which side collision occurs xna c# 2d

Posted 21 December 2016 - 09:09 PM

Hi everyone,

I am currently takling collision detection and I have ran into this issue where the player sprite will only notice it has collided with the tile when the top left corner is hitting the thing it is meant to be colliding with. I have been searching all day and the code below is my interpretation of how to fix the issue as well as searching on forums relating to the topic. However, this doesn't seem to be working as I would like and it is still only recognising the collision in the top left part of the rectangle. I am at a loss with this so I wondered if you guys would be able to provide some insight and point me in the right direction.

I want it so when one of the sides of the player collision box hits the tile it Consol.Writelines what side was hit. Then once this has been fixed eventually stop the player once a specific tile has been hit.

Any help would be greatly appreciated.
Thank you in advance.

// calculating AABB center.
            int tx = (tileBounds.Left + tileBounds.Width / 2) - (player.playerBounds.Left + player.playerBounds.Width / 2);
            int ty = (tileBounds.Top + tileBounds.Height / 2) - (player.playerBounds.Top + player.playerBounds.Height / 2);



                  if (mapCell[tileY, tileX].TileID == 4)
                        {
                            // determining which side has been hit
                            if(tx > ty && tx > 0)
                            {
                                Console.WriteLine("left side hit");
                            }
                            if (tx > ty && tx < 0)
                            {
                                Console.WriteLine("right side hit");
                            }
                            if (tx < ty && tx > 0)
                            {
                                Console.WriteLine("top side hit");
                            }
                            if (tx > ty && tx < 0)
                            {
                                Console.WriteLine("bottom side hit");
                            }
                         }




Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Determine which side collision occurs xna c# 2d

#2 AnonymousUser1508  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-April 16

Re: Determine which side collision occurs xna c# 2d

Posted 22 December 2016 - 06:25 AM

Is the error within
if (mapCell[tileY, tileX].TileID == 4) 
here? I have no idea how to proceed from here, so any help is greatly appreciated.
Was This Post Helpful? 0
  • +
  • -

#3 AnonymousUser1508  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-April 16

Re: Determine which side collision occurs xna c# 2d

Posted 22 December 2016 - 06:41 AM

I have been running some tests and as far as I'm aware the error is this. if (mapCell[tileY, tileX].TileID == 4). I did colission tests on a rectangle with hardcoded positions rectangle it appears to work better than just detecting the top left corner. So the issue appears to be when detecting collision for a specific tile( as far as I'm aware)
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13390
  • View blog
  • Posts: 53,435
  • Joined: 12-June 08

Re: Determine which side collision occurs xna c# 2d

Posted 22 December 2016 - 07:39 AM

Yeah, top left corner is 0,0.. so if you extrapolate edges x + width is 'right'.. x is left.. y is top.. y + height is bottom.
Was This Post Helpful? 0
  • +
  • -

#5 AnonymousUser1508  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-April 16

Re: Determine which side collision occurs xna c# 2d

Posted 22 December 2016 - 09:13 AM

         
   float Xpos = player.Position.X;
   float yPos = player.Position.Y;

   for (int y = top; y <= bottom; ++y)
            {
                for (int x = left; x <= right; ++x)
                {

                    if (mapCell[tileY, tileX].TileID ==1)
                    {
                        if(Xpos + 50 > tileBounds.Left)
                        {
                            Console.WriteLine("player hit the left side");
                        }
                        if(Xpos < tileBounds.Right)
                        {
                            Console.WriteLine("player hit right side");
                        }
                        if(yPos + 50 < tileBounds.Bottom)
                        {
                            Console.WriteLine("player hit from bottom");
                        }
                        if(yPos > tileBounds.Top)
                        {
                            Console.WriteLine("player hit from tom");
                        }


Did you mean like this?? if so, collision is still only registered on the top left corner of the player.

sorry I meant this
  float Xpos = player.playerBounds.X;
  float yPos = player.playerBounds.Y;


Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13390
  • View blog
  • Posts: 53,435
  • Joined: 12-June 08

Re: Determine which side collision occurs xna c# 2d

Posted 22 December 2016 - 09:29 AM

I don't know what you got going on there.

Does your character only move one tile at a time? If so then it should be even more simple to check "hey.. this guy's walking over here and going right.. check the right tile for move or not.. if not cancel the move.. if so update their location to the new tile and refresh'.
Was This Post Helpful? 0
  • +
  • -

#7 AnonymousUser1508  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 29-April 16

Re: Determine which side collision occurs xna c# 2d

Posted 22 December 2016 - 10:20 AM

Hey again,

The character can move freely around the map, it isn't restricted to simply one tile at a time.The collision for the player works fine when I use a rectangle and give it hard coded positions and width and height like this
 Rectangle greenBtn = new Rectangle(40, 60, 50, 50); 


The player is then able to recognise which side of the greenBtn Rectangle it hit using the code below this works perfectly for the greenBtn when I change the
 tileBounds.Width to greenBtn.Width
in the code below.
            double w = 0.5 * (tileBounds.Width + playerBounds.Width);
            double h = 0.5 * (tileBounds.Height + playerBounds.Height);
            
            for (int y = top; y <= bottom; ++y)
            {
                for (int x = left; x <= right; ++x)
                {
                    if (playerBounds.Intersects(greenBtn)
                    {
                        if (Math.Abs(tx) <= w && Math.Abs(ty) <= h)
                        {
                            double wy = w * ty;
                            double hx = h * tx;

                            if (wy > hx)
                            {
                                if (wy > -hx)
                                {
                                    Console.WriteLine("player hits tile from top");
                                }
                                else
                                {
                                    Console.WriteLine("player hits tile from left");
                                }
                            }
                            else if (wy > -hx)
                            {
                                Console.WriteLine("player hits tile on right");
                            }
                            else
                            {
                                Console.WriteLine("collision on bottom");
                            }

                        }

:code:
Howevever, when I try to check collision for a specific tile such as tile 1, like this

  if (playerBounds.Intersects(tileBounds) && mapCell[tileY, tileX].TileID ==1)
the player will only recognise collision in the top left corner.

the tile map is drawn like this in the game1 class.
TileMap tileMap = new TileMap(new int[,]
        {
            { 2,2,2,2,2,2,2,2,2,2 },
            { 2,2,2,2,2,2,2,2,2,2 },
            { 0,0,0,0,0,0,4,4,4,4 },
            { 0,0,0,0,0,0,1,1,4,4 },
            { 1,1,1,1,1,1,1,1,1,1,},
            { 1,1,1,1,1,1,1,1,1,1,}
        });




I hope that answers all of your questions. Thank you for the reply by the way and taking the time to try and help me.

This post has been edited by modi123_1: 22 December 2016 - 10:36 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1