2D Tile Collisions

Started by
1 comment, last by Tracy 21 years, 10 months ago
Don't laugh too hard, the following code does work for doing tile collision checks between a player structures x and y coords and tiles. But it checks for collisions every time it draws a wall tile (1's are walls, 0's are clear), awfully inefficient. So far it's the only method I've gotten to work. Looking for other articles/tutorials on the subject when time permits. //map array char map[]= { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, }; //function to draw tiles void drawTiles(void) { RECT rcRect; HRESULT ddrval; int startX,startY; int renderX,renderY; int whichCell; int xOffset,yOffset; startX=0; startY=0; rcRect.top=0; rcRect.bottom=16; whichCell=0; xOffset=240; yOffset=100; for(renderY=0;renderY<20;renderY++) { xOffset=240; for(renderX=0;renderX<20;renderX++) { if(map[whichCell]==1) { rcRect.left=0; rcRect.right=16; detHits(xOffset,yOffset); /**/ } if(map[whichCell]==0) { rcRect.left=16; rcRect.right=32; } ddrval=lpDDSBack->BltFast(xOffset,yOffset,lpDDSTextures, &rcRect,DDBLTFAST_SRCCOLORKEY); xOffset=xOffset+16; ++whichCell; } yOffset=yOffset+16; } } Better methods? "A man can't just sit around." 'Lawn Chair' Larry Walters (1982) [edited by - Tracy on June 7, 2002 5:24:49 PM]
"A man can't just sit around." 'Lawn Chair' Larry Walters (1982)
Advertisement
Drawing a map is not the same thing as checking for collisions. I don''t see why you''re putting them in the same part. There''s no need to - whenever you need to check for collisions, you just take the player''s location, translate that into tile coordinates, and see if the tile (or tiles) at that location is passable or not.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]
Got it!

"A man can''t just sit around." ''Lawn Chair'' Larry Walters (1982)
"A man can't just sit around." 'Lawn Chair' Larry Walters (1982)

This topic is closed to new replies.

Advertisement