views:

1738

answers:

4

Hey, I'm trying to make (figure out how to..) a 2d platformer in XNA.

I know how to create a tile grid and how to perform collision detection. I perform collision detection on the 9 bounding tiles of the player, but I'd like to know if there's a better way to perform collision detection.

I've read Braid doesn't use tiles but pieced images or something, how do you perform collision detection on those? and is that better than using tiles?

+1  A: 

Assuming you don't mind using a third party tool to do the majority of the work for you, you may be better off using something like Box2D for C#.

http://www.box2d.org/ http://code.google.com/p/box2dx/

This library will give you collision detection and physics capabilities.

xgalaxy
using a physics library for simple collision detection for your main character in a platform game is generally a bad idea. It takes an enormous effort to tweak it and make the movement and feel of it right. Games like Mario are not physically correct, and are therefore much more enjoyable to play. That doesn't mean that it appears to behave and comply to some rules (like the main character falls down due to gravity) it's just that it's programmed to feel just right, instead of physically correct.
Toad
Box2D and Farseer Physics, as mentioned below, are extremely simple physics API's. In fact they are so simple I would argue that setting up a physically based world with them would involve less lines of code than rolling your own collision detection. Even for a simple 2d platformer.
xgalaxy
the problem with physics (again) is that it takes along time to get right. Anything is possible, which is fun for some games, but typically you don't want the main character to be able to do interact and use everything. Physics based games quickly become sort off simulation style games, while plqtform games are much more direct arcade style games. It doesn't mean they use physics things for certain elements, but it most definately doesn't mean everything is simulated.
Toad
That's true, and one person i know which put together a platformer out of Farseer had to do quite a bit of shoehorning of the engine to get reasonable behavior. It worked, but it takes a long time.
RCIX
@reinier and RCIXWhat's better to use? a physics engine or tile collision?and what do platformers now a days like "New Super Mario Bros. Wii" for example use? I know the old platformers used tile collision right?
Restart
@restart: If you are a beginner, just start with tile collision. See how far it will get you. Everything before 3D became mainstream used 'simple' tile based collision. (So games like turrican, the old mario, superfrog, etc). So you can get a long way.
Toad
ok I planned on making a tile game, but when I found out there were other ways to make a platformer I didn't know what to use anymore. I will continue to build my tile game now, though I'm still curious what Braid used if it wasn't a tile game.
Restart
@restart: instead of doing tiles in a regular grid you can use 'tiles' which are positioned anywhere (so they basically have an X, Y pos). Collision will be harder since it's not grid based anymore. You'll need to find out (in a fast way) which graphics you are standing on. One way of doing this is using another space subdivision scheme like a Quadtree http://en.wikipedia.org/wiki/Quadtree You'll also be needing this to quickly determine which 'tiles' to blit to the screen (Something which is also much easier when using a grid).
Toad
@restart: It's easy to get lost in all the options. Thats why it's recommendable to just start 'easy' and used a tilemap. By the time your first game has finished you'll have plenty of experience to move up to other schemes.
Toad
A: 

Have you checked the examples in the collision serie on the XNA creators club, in particular the pixel-collision sample?

Paolo Tedesco
With a platform game you really don't want pixel perfect collision detection. It would mean that if the main character animates, that sometimes (due to his hand sticking out for instance) he bumps into a wall, and then he animates again, and he can still walk 2 pixels. What you normally do is define either a few collision boxes, or an invisible elipsoid shape around the character and use this as your collision area.
Toad
@reinier: I've written 2D platform games where the collision between hero and tiles were not pixel perfect but collision between objects (like balls) and tiles were pixel perfect and between objects and hero were pixel perfect too. So it may be more complex than "no pixel perfect collision". :)
Webinator
@reinier: good point, I agree.
Paolo Tedesco
@wizardofodds: This can clearly be a choice. I myself like it better if a game is slightly forgiving. So my main character is not instantaneously dead if his left nostril happens to collide with the fingernail of the enemy. Although computers can be pixel perfect, the vision of players is typically not... that's why typically you allow some overlap of sprites.
Toad
+1  A: 

If you're going for a retro style then it's certainly fine to use tiles! However, if you want something more "modern", then you can go with a more conventional physics engine. Farseer Physics is a great engine, and several people have used it to make platformers. It's based on Box2D and similar engines, but offers a simpler API and several unique features (like texture to geometry) and i myself can testify to it's niceness having played around with it a bit.

RCIX
+1  A: 

You probably need a bit more than collision detection for a game like Braid. I would assume that you also need a physics engine. I would recommend that you take a look at Farseer Physics engine, which is 2D engine that works very well with XNA. It supports a number of different techniques for collision detection.

Brian Rasmussen
Thanks, I'll take a look at farseer.
Restart
I've been looking at farseer now, from the samples it has it looks gooed but I can't get the collision detection between 2 geoms to work.I created a PhysicsSimulator, 2 Bodys, 2 Geoms, and now 1 body falls because of gravity. problem is that it falls straight through the other body.
Restart
@Restart: Did you look at all the demos? There are plenty of examples on collision detection.
Brian Rasmussen