• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
skuzzbag

2D tile map collision detection

7 posts in this topic

I've got a problem with my tilemap based collision detection. The problem is getting stuck on walls and I was wondering what the method is to tackle this issue.

 

For example moving left/right works ok and also checking for solid tiles above and below. However diagonal movement causes the issue where the player sprite sticks to the wall.

 

Currently if a check is made on a point around the sprite for example -1 pixel and this tile is found to be solid then the player x position is set to be to the right on the solid tile (player.position.x = (tile.X / 16)+ player.width) - so in effect the player just stops moving left.

 

Should you first check for and correct X positioning before moving onto checking and correcting Y positioning? Currently I am checking X/Y then correcting X/Y.

 

Thanks.

0

Share this post


Link to post
Share on other sites

the way i would do it is to just do a bounding box collision check, then if there was a collision i would correct on the axis with the lowest penetration value. the penetration value is how far one bounding box has penetrated another . so you get the penetration value of both the x axis and the y axis. and correct for the smallest penetration value. you can get the penetration value by taking the distance from the center of the bounding box to the edge of the bounding box "width/2 or height /2". for both bounding boxes, and then subtract it from the distance from the center of box 1 to  the center of box 2:

 


int Xpenetration=( ( Boundingbox1.width/2 + Boundingbox2.width/2 ) - (Boundingbox2.centerX-Boundingbox1.centerX) );

int Ypenetration=( ( Boundingbox1.height/2 + Boundingbox2.height/2 ) - (Boundingbox2.centerY-Boundingbox1.centerY) );

 

 

then you compare the absolute value of both the Xpenetration and the Ypenetration, and just correct for the smallest value:

 

if( abs(Xpenetration) > abs(Ypenetration) )
{
   // correct on the y axis using Ypenetration value.
}
else 
{
   // correct on the x axis using Xpenetration value.
} 
Edited by xinfinite33
1

Share this post


Link to post
Share on other sites

So are you suggesting that once I have detected a collision based upon a tilemap check, I then perform a bounding box collision using the tilemap tile coords and the player bounding box coords?

0

Share this post


Link to post
Share on other sites

Can you show the collision handling code?

 

I'm thinking I know the problem (someone else recently posted the same problem) but it helps to be sure.

 

In short, allowing diagonal motion means you need to segregate collision reactions per-axis. It's easier to express in code, or to point out the reason that the code is failing.

Edited by Khatharr
1

Share this post


Link to post
Share on other sites

Its a bit unclear how you are handling the generic collision testing. Could you elaborate a little bit more?

for example, since it is tile-based, can we assume you have a multi-tier grid to partition the space? 

 

the general algorithm I've seen over and over is something like this:

assume:

- each character has a bounding-box that is either calculated at run time or pre-calculated on a per-frame basis. In other words, you have the rectangular "space" your guy occupies

- each character also has its x,y coordinates stored somewhere

 

1) for each character, do a bounding-box vs space partition test. For example, if you are using multi-leveled grids then you would "query" the grid for the possible tiles that collide with the character's bounding box. If you get no results, then you know that there are no collisions

 

2) for each possible tile you collide with in (1), re-check the character's bounding box against the tile, removing all the tiles that you DON'T collide with

 

3) now for each remaining tile, you can offset the character's x and y location using a similar algorithm to what xinifinite33 mentioned to "stop" the character from going thru the tile. 

 

There are other algorithms i'm sure, and your information provided is a bit vague so I apologize if this is more info / different info than you wanted

1

Share this post


Link to post
Share on other sites

    I would suggest using the velocity to create an up vector of sorts, you could use this to "bounce" the sprite away from the collision point by a small but safe distance.  This would prevent the blocking when walking against a wall, the basic idea is a few simple steps.

 

Use the X & Y Velocities to create an up vector (+1/-1 X & Y Values indicating direction sprite is traveling).

Invert the up vector based on collision surface facing (or orientation if you will)

Use this new inverted up vector to apply a minimal position shift (back to last non blocking tile in that direction maybe?)

 

    The important part is that you are sure to only affect the axis corresponding to the facing of the collision surface.  (If you make collision traveling along the X axis you should only affect a change on the X axis).  I would assume that right now you are doing something that completely stops or shifts both X and Y axis independent of the collision axis.  Shoot me an email (found on my profile) if you need some more information.

2

Share this post


Link to post
Share on other sites

Thanks for all the replies they are very helpfull!

 

I woke up a but early this morning (6:00 am now) and had another go and it turned out the points I was checking for collisions were a bit close or overlapping. So when my code was checking for left right collisions the points that checked for downward collisions were also triggered meaning that hitting a wall in the X direction also triggered code for landing on a platform so the player character just stuck to the wall.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0