1
\$\begingroup\$

I want to create a 2D side scrolling game in a similar style as the game "Castle Crashers". (Go to: http://store.steampowered.com/app/204360/ to see some pics of the game).

I have experience creating 2D games which use an array of tiles (a tilemap), to see if the player/enemies/bullets are moving to a blocked or unblocked tile by referencing the entities x and y coordinates with the tile in the corresponding location.

My question is: How do I implement collision with the "game map", in a game that doesn't use tiles, but instead shapes.

For instance: if the current "map" was a circle, the p[layer could move freely around the inside of the circle, but could not leave the circle.

Sorry if this question is long-winded and confusing, but I would really appreciate some help with this!

Thanks!

\$\endgroup\$
1
  • \$\begingroup\$ Why not use a psuedo-tilemap? Computers easily think in squares, so you can have arrays of objects in each tile, and then preform your collision detection with the "shapes" if the objects' tiles overlap. When a shape moves, simply update its tiles. \$\endgroup\$ Commented Jul 11, 2014 at 16:52

3 Answers 3

2
\$\begingroup\$

I'm assuming you're either using some sort of entity-component system, or else you have in-game classes that represent your objects.

Either way, you have some reference to your in-game objects (players, walls, enemies, etc.) At a minimum, these objects should have:

  • A position
  • A sprite (display)
  • A collision primitive (axis-aligned bounding box?)

All you need to do, then, for collisions, is check the collision primitives against each other. (Mentally, you can imagine this box that moves when your player/objects move.)

Of course, this solution has O(n^2) complexity (check every primitive against every other primitive), so you may need to use something like a quadtree for performance (to limit which objects are checked -- only against near neighbors).

\$\endgroup\$
1
\$\begingroup\$

Simply use shapes and AABB collisions.

Keep a tiled map. Create an approximation of a circle using tiles and create a rectangle for each of the unwalkable tiles.

Next, when you create a player or any movable entity, simply create a Rectangle around them too. Before moving, look whether or not the player's rectangle would collide with any of the wall's rectangle and allow the movement accordingly.

Here are some resources to read about AABB collisions:

https://stackoverflow.com/questions/22512319/what-is-aabb-collision-detection

http://www.gamedev.net/page/resources/_/technical/game-programming/swept-aabb-collision-detection-and-response-r3084

http://www.gamefromscratch.com/post/2012/11/26/GameDev-math-recipes-Collision-detection-using-an-axis-aligned-bounding-box.aspx

\$\endgroup\$
1
\$\begingroup\$

You can use a raycasting (or linecasting) based collision detection system. At a very high level, you cast rays from your player (preferably from the edges of it's collider box) into the game map. When the ray hits something, you can check to see what its hit, and if it's a collidable surface, resolve the collision and adjust player movement as appropriate. This method is pretty agnostic to how your game map is built (tilemap, models, geometric shapes, etc).

For more technical details, you can view my own question on a similar topic. While the code presented is C# (and for Unity), you could easily adapt it for any language.

Here's an image to help visualize the system

Source: Yoann Pignole

Some resources:

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .