11 Replies - 3780 Views - Last Post: 13 April 2013 - 09:07 AM

#1 stevenson74   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-April 13

XNA 2D Tile Collision

Posted 08 April 2013 - 06:35 PM

(I tried posting this on Xbox's create MSDN forums, didn't get much help)

I had an idea of doing a Super Smash Bros type 2D game with platforms (I thought it wouldn't really require tiles)

Anyway, I have decided to stick with good old tiles. Problem; collision. I have yet to find a successful way of collision, I've tried so many methods (checking what side the collision is taking place, checking the overlap and moving the player back that amount, directional movement bools) and I can't wrap my head around the Starter Kit's collision method.

Here's some mini-code I was working on(link below). It loads the map from a 2D array and draws them perfectly. What's included with the project is the smoothest collision coding I could do, but there still HAS GOT to be a better way. I see all sorts of games written in XNA with perfectly smooth collision, and I just can't seem to grab consistency with it.

Thanks!


http://www.mediafire...9996371lyjfbp7d

Is This A Good Question/Topic? 0
  • +

Replies To: XNA 2D Tile Collision

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6814
  • View blog
  • Posts: 23,188
  • Joined: 05-May 12

Re: XNA 2D Tile Collision

Posted 08 April 2013 - 08:31 PM

Moving to the XNA forum where you'll likely get more appropriate responses.
Was This Post Helpful? 1
  • +
  • -

#3 stevenson74   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-April 13

Re: XNA 2D Tile Collision

Posted 08 April 2013 - 08:35 PM

View PostSkydiver, on 08 April 2013 - 08:31 PM, said:

Moving to the XNA forum where you'll likely get more appropriate responses.



Thanks! :stupid:
Was This Post Helpful? 0
  • +
  • -

#4 oyyou   User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 196
  • Joined: 26-April 10

Re: XNA 2D Tile Collision

Posted 10 April 2013 - 07:04 AM

I would like an answer to this question too!
Was This Post Helpful? 0
  • +
  • -

#5 stevenson74   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-April 13

Re: XNA 2D Tile Collision

Posted 10 April 2013 - 11:39 AM

View Postoyyou, on 10 April 2013 - 07:04 AM, said:

I would like an answer to this question too!



You have got to be kidding me. :unsure:
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14924
  • View blog
  • Posts: 59,571
  • Joined: 12-June 08

Re: XNA 2D Tile Collision

Posted 10 April 2013 - 11:47 AM

No, I am pretty certain he would like to know too.

Though, typically, the XNA rectangle's "intersect" works pretty darn well.

http://msdn.microsof...intersects.aspx


1. Make a temp rectangle of the current character's rectangle.
2. Apply the movement to the temp rectangle
3. See if that temp rectangle intersects with what ever is near.
4. If it does intersect then stop the movement
5. If not, continue on per normal.
Was This Post Helpful? 1
  • +
  • -

#7 oyyou   User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 196
  • Joined: 26-April 10

Re: XNA 2D Tile Collision

Posted 11 April 2013 - 01:23 AM

View Postmodi123_1, on 10 April 2013 - 11:47 AM, said:

Though, typically, the XNA rectangle's "intersect" works pretty darn well.


The thing with intersect is detecting each side of the rectangle, and then making the collision apply depending on the side.
What has Steven has done has used a RectangleHelper class that I created and tried using that.
The problem with that is if your sprite moves too quickly, it will go straight the rectangle and cause all sorts of problems.

I think Separating Axis Theorem (SAT) would be an excellent collision. But it's very difficult.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14924
  • View blog
  • Posts: 59,571
  • Joined: 12-June 08

Re: XNA 2D Tile Collision

Posted 11 April 2013 - 08:40 AM

I disagree... think about breaking down a collision in a 2d game.

Your objects are your player character, and host of other blocks, coins, objects, enemies, etc.

You don't need to track _EVERYTHING_ for a collision, right? You have a bubble of things that are near-ish. Anything not near-ish don't check. Boom - that cuts down your search space dramatically.

Then of those near-ish objects your first concern is to even see if the next potential move (temp rectangle of current box plus potential move) intersects with those near-ish objects. A simple and quick check.

If the potential move intersects don't allow the player to move there, and notify the object the pc collided with that there was a collision and what general direction the pc was going when the collision happened. Then, if the object is so inclined, it act on the collision and with the player's direct you can determine which side it happened.

There's no need to get all nitty gritty and match up pixels edges - just chunk up objects, potential moves, and movement directions.. logic the rest out.



Quote

What has Steven has done has used a RectangleHelper class that I created and tried using that.
The problem with that is if your sprite moves too quickly, it will go straight the rectangle and cause all sorts of problems.

I am not sure if I follow what you are saying.
Was This Post Helpful? 0
  • +
  • -

#9 oyyou   User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 196
  • Joined: 26-April 10

Re: XNA 2D Tile Collision

Posted 11 April 2013 - 12:39 PM

View Postmodi123_1, on 11 April 2013 - 08:40 AM, said:

I am not sure if I follow what you are saying.


What I was saying there was, if the players velocity is 15, and the rectangles (The rectangle it is going to collide with) width is only 5, the playing has the potential to just ignore the rectangle completely and just be on it's merry way.

But I do agree that it doesn't need to be perfect. The only problem I ever really can across was telling the player (Or whatever) when collision is detected).

For example, you can tell the player to stop, you can tell the player position to equal to an appropriate place. And it can get really frustrating trying to make simple things work.
Was This Post Helpful? 0
  • +
  • -

#10 stevenson74   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-April 13

Re: XNA 2D Tile Collision

Posted 11 April 2013 - 10:08 PM

I've tried doing what you stated; add rectangles to each side of my sprite plus it's potential move and when they meet that condition, I have a collision. I've tried this type of collision many times before and I just can't seem to grab consistency with it. Maybe an example or demo to help me out? (Might be a lot to ask)
Was This Post Helpful? 0
  • +
  • -

#11 oyyou   User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 196
  • Joined: 26-April 10

Re: XNA 2D Tile Collision

Posted 12 April 2013 - 08:00 AM

Hey man, so I just gave it another quick look-over and came up with something for you
You can try something like this on your collision detection
if(rectangle.TouchLeftOf(newRectangle) && 
Keyboard.GetState().IsKeyDown(Keys.D)) // or IsKeyDown(rightKey) - Keys rightKey = Keys.D
{
position.X = newRectangle.X - rectangle.Width;
velocity.X = 0;
}



I was on a break at work and decided to play with it. If you do something like that, you should be sorted on side collision.

This post has been edited by oyyou: 12 April 2013 - 08:00 AM

Was This Post Helpful? 1
  • +
  • -

#12 stevenson74   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-April 13

Re: XNA 2D Tile Collision

Posted 13 April 2013 - 09:07 AM

View Postoyyou, on 12 April 2013 - 08:00 AM, said:

Hey man, so I just gave it another quick look-over and came up with something for you
You can try something like this on your collision detection
if(rectangle.TouchLeftOf(newRectangle) && 
Keyboard.GetState().IsKeyDown(Keys.D)) // or IsKeyDown(rightKey) - Keys rightKey = Keys.D
{
position.X = newRectangle.X - rectangle.Width;
velocity.X = 0;
}



I was on a break at work and decided to play with it. If you do something like that, you should be sorted on side collision.



Wow, that was simple fix to a rather complicated problem I was having. Thanks so much, Oyyou! You are seriously awesome.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1