Sign in to follow this  

collision detection with layers

This topic is 4221 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently trying to impliment collision detection in a tiled 2D game where, for example: everything on the first layer is able to be walked over (ground), everything on the 2nd layer can NOT be walked over (i.e. building walls, random props etc) currently using the following code
...

if(KEY_DOWN(VK_RIGHT))
{
    if(g_TileIDs[1][(nCurRow * 20) + (nCurCol - 1)] == -1) //if no tile
    {         
          Move(fPositionX += (g_fMoveSpeed * g_fDelta), fPositionY, 0.0f);
          g_Dir = 1;
          g_Dir2 = 0;
          nFrame = g_Dir2;
    }
}

...

and while trying it out, my sprite hardly moves at all before it stops and then also while trying to move it after a few seconds the app crashes and I'm stuck. Error: First-chance exception at 0x00497df2 in tileset_engine.exe: 0xC0000005: Access violation reading location 0x004e857f. Unhandled exception at 0x00497df2 in tileset_engine.exe: 0xC0000005: Access violation reading location 0x004e857f. the error message points to the " if(g_TileIDs[1][(nCurRow * 20) + (nCurCol - 1)] == -1) //if no tile " line. No idea what's causing the problem or why this isn't working. If anyone has any suggestions, ideas or anything I would GREATLY appreciate it.

Share this post


Link to post
Share on other sites
You might post where and how g_TileIDs is declared and/or allocated. Also, try putting an assertion before the line in question to check and make sure the index is actually >= 0 and < the size of the array (perhaps something's going wrong and you're accessing outside of the array bounds).

Share this post


Link to post
Share on other sites
char g_TileIDs[2][20*13];

I have it read from a .LVL file (simple text file) where the tiles are stored stored like

20 20 20 0 . . . .
.
.
.
etc

20 20 20 0 . . . .
.
.
.
etc

for each of the layers

Share this post


Link to post
Share on other sites
Well, I would still put assertions in anywhere you're accessing the array to see if you're reading or writing outside the bounds somewhere. Beyond that, I don't know if the cause of the problem can be determined from the code you posted.

Share this post


Link to post
Share on other sites

This topic is 4221 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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