Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Collision with Mappy

This thread is locked; no one can reply to it. rss feed Print
Collision with Mappy
evantage
Member #15,489
February 2014

Hello,

I am using Mappy with Allegro 5 to create and render my map however I am having trouble creating a working pixel perfect collision check for the walls.

The walls are flagged in mappy and I have a function set up to check:

#SelectExpand
1int Map::collided(int x, int y) 2{ 3 BLKSTR *blockdata; 4 blockdata = MapGetBlockInPixels(x,y); 5 6 return (blockdata->user1); 7}

And I have checks for all directions, the up and down functions seem to be working perfectly however the left and right ones are not:

#SelectExpand
2bool Character::collideLeft() 3{ 4 int highX = 0; 5 int highY = 0; 6 7 if(p_Map->collided(m_xpos, m_ypos) || p_Map->collided(m_xpos, m_ypos+m_height-1)) 8 { 9 al_lock_bitmap(m_sys_bitmap, al_get_bitmap_format(m_sys_bitmap), ALLEGRO_LOCK_READONLY); 10 11 ALLEGRO_COLOR pixel; 12 13 for(int x = m_frame_column*m_width; x < (m_frame_column*m_width)+m_width; x++) 14 for(int y = m_frame_row; y < (m_frame_row*m_height)+m_height+1; y++) 15 { 16 pixel = al_get_pixel(m_sys_bitmap, x, y); 17 18 if(pixel.a > 0) 19 { 20 if(y < highY) 21 highY = y; 22 if(x < highY) 23 highX = x; 24 } 25 } 26 27 if(p_Map->collided(m_xpos+highX,m_ypos+highY)) 28 { 29 cout << highX << ", " << highY << endl; 30 al_unlock_bitmap(m_sys_bitmap); 31 return true; 32 } 33 34 else 35 { 36 al_unlock_bitmap(m_sys_bitmap); 37 return true; 38 } 39 } 40}

Now I have tried many methods and can see the flaws in the one above however this is the attempt I tried last, could anyone advise me on a simpler method, perhaps checking pixels on system bitmaps isn't the right way. It does work fine for collision between two other bitmaps though.

Go to: