Jump to content

View more

Image of the Day

雑魚は多めにして、爽快感重視にしつつ・・・(´・ω・`)
早いとこ、ベースを作って、完成にもっていかないとね。
タイトルもまだ迷ってるだよなぁ。 
#indiedev  #indiegame #screenshotsaturday https://t.co/IwVbswGrhe
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

2d tile collision advice

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
4 replies to this topic

#1 beginner07   Members   

100
Like
0Likes
Like

Posted 27 January 2012 - 10:39 PM

Hi I'm new to game programming and this forum. I've been searching all over the net for tutorials on how to do 2d tile collision between a sprite and tile using a 2d array. I just can't wrap my head around the logic to make it work properly. I've used primitive drawing routines to create a small map and sprite represented my boxes. Currently the sprite box will get stuck whenever I touch a solid tile and try to move.

red box = solid tile represented by a 1.
green = passibile tile reprented by a 0.

I'm using Visual C++ and the allegro sdk.

Thank you for your time :)

#include <allegro.h>
using namespace std;
#define ScreenWidth 800
#define ScreenHeight 600
volatile long timer = 0;
void Increment(){timer++;}
END_OF_FUNCTION(Increment);
int map[10][10] = {{1,1,1,1,1,1,1,1,1,1},
	   {1,0,0,0,0,0,0,0,0,1},
	   {1,0,0,1,1,0,1,0,0,1},
	   {1,0,0,0,0,0,0,0,0,1},
	   {1,0,0,1,1,1,1,0,0,1},
	   {1,0,0,1,0,0,1,0,0,1},
	   {1,0,0,1,1,1,1,0,0,1},
	   {1,0,0,0,0,0,0,0,0,1},
	   {1,0,0,0,0,0,0,0,0,1},
	   {1,1,1,1,1,1,1,1,1,1},
};

int main()
{
allegro_init();
install_keyboard();
install_timer();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
set_window_title("Collision detection");
LOCK_FUNCTION(Increment);
LOCK_VARIABLE(counter);
install_int_ex(Increment, BPS_TO_TIMER(60));
BITMAP *Buffer = create_bitmap(800, 600);
bool done = false;
int x = 64, y = 64;
//Game Loop
while(!done)
{
int px = x / 32;
int py = y / 32;
int px2 = (x+32)/ 32;
int py2 = (y+32)/ 32;
//Update Loop
while(timer > 0)
{
  if(key[KEY_ESC])
   done = true;
  if(key[KEY_UP])
  {
   if(map[px][py]==0 && map[px2][py]==0)
    y -= 1;
  }
  if(key[KEY_RIGHT])
  {
   if(map[px2][py]==0 && map[px2][py2]==0)
    x += 1;
  }
  if(key[KEY_LEFT])
  {
   if(map[px][py]==0 && map[px][py2]==0)
    x -= 1;
  }
  if(key[KEY_DOWN])
  {
   if(map[px][py2]==0 && map[px2][py2]==0)
    y += 1;
  }
 
   timer--;
}
 
  
//Draw tiles
for(int a = 0; a < 10; a++)
{
  for(int b = 0; b < 10; b++)
  {
   if(map[a][b]== 0)
    rectfill(Buffer, a*32,b*32, a*32+32, b*32+32, makecol(0,255,0));
   else
    rectfill(Buffer, a*32,b*32, a*32+32, b*32+32, makecol(200,0,0));
 
  }
}

textprintf(Buffer, font, 400, 530, 255, "X: %i Y: %i", x, y);
textprintf(Buffer, font, 400, 550, 255, "Integer: %i, %i", px, py);
textprintf(Buffer, font, 400, 570, 255, "Map tile: %i", map[px][py]);
rect(Buffer, x,y,x+32,y+32, 255);
blit(Buffer, screen, 0,0,0,0,800, 600);
clear_bitmap(Buffer);
}
destroy_bitmap(Buffer);
allegro_exit();
return 0;
}
END_OF_MAIN()



#2 Tom Sloper   Moderators   

15855
Like
0Likes
Like

Posted 27 January 2012 - 11:52 PM

Programming questions don't belong in the Game Design forum. Moving this to For Beginners (a technical programming forum).
-- Tom Sloper
Sloperama Productions
Making games fun and getting them done.
www.sloperama.com

Please do not PM me. My email address is easy to find, but note that I do not give private advice.

#3 aeroz   Members   

171
Like
0Likes
Like

Posted 28 January 2012 - 11:57 AM

How do you want exactly the collision to be? And what is colliding with what?
If the objects are all rectanges you could try AABB Collision Detection.

#4 wildbunny   Members   

550
Like
1Likes
Like

Posted 28 January 2012 - 05:16 PM

I wrote this article on tile collision detection which should give you some pointers:

http://www.wildbunny.co.uk/blog/2011/12/14/how-to-make-a-2d-platform-game-part-2-collision-detection/

:)

Cheers, Paul.

#5 Marvel Magnum   Members   

333
Like
0Likes
Like

Posted 29 January 2012 - 11:32 AM

I wrote this article on tile collision detection which should give you some pointers:

http://www.wildbunny...sion-detection/


That is a amazing resource. Thanks man.




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.