tile based engine

Started by
8 comments, last by BrasiLokau 18 years, 3 months ago
Hello everyone, this is my first post here I've been working on my tile based engine and i'm having problem with collision detection... my map is an array char map[10][10] and i'm using 32x32 tiles. My map is like 111111111 100011001 111000011 1 is the wall and 0 are the places that i can walk Code:

switch(event.key.keysym.sym){ 
                    case SDLK_UP: 
                        if(map[player.x / TILESIZE][player.y / TILESIZE - 1] == 0) 
                            player.y = player.y - player.p_speed; 
                        break; 
                    case SDLK_DOWN: 
                        if(map[player.x / TILESIZE][player.y / TILESIZE + 1] == 0) 
                            player.y = player.y + player.p_speed; 
                        break; 
                    case SDLK_RIGHT: 
                        if(map[player.x / TILESIZE + 1][player.y / TILESIZE ] == 0) 
                            player.x = player.x + player.p_speed; 
                        break; 
                    case SDLK_LEFT: 
                        if(map[player.x / TILESIZE - 1][player.y / TILESIZE ] == 0) 
                            player.x = player.x - player.p_speed; 
                        break; 
                    default: 
                        break; 
                }
My other question is, are there any ways to print a text onto the screen? I'm using sfont but i'm having problems... i check everything and it keeps saying that couldn't open the font file Code:

int Write_Text(int x, int y, char *string){ 

    SFont_Font* Font; 

   Font = SFont_InitFont(IMG_Load("font\24P_Copperplate_Blue.png")); 
   if(!Font) { 
      fprintf(stderr, "An error occured while loading the font."); 
      exit(1); 
   } 

   // a simple text blit to (0/0) 
   SFont_Write (screen, Font, x,y,string); 

   // Update the screen 
   SDL_UpdateRect(screen, 0, 0, 0, 0); 
   // Let the user time to look at the font 

   // Don't forget to free our font 
   SFont_FreeFont(Font); 
} 

Thanks everyone for your attention best regards [Edited by - BrasiLokau on January 3, 2006 11:44:42 AM]
blog: www.brasilokau.com/games/blog
Advertisement
Please reformat your post using the [ source ] tags.
Here is what I would do (didnt check this, so sorry if there are errors) :

// UPif(map[player.x / TILESIZE][(player.y - player.p_speed) / TILESIZE] == 0...// DOWNif(map[player.x / TILESIZE][(player.y / TILESIZE) + 1] == 0...// RIGHTif(map[(player.x / TILESIZE) + 1][player.y / TILESIZE] == 0...// LEFTif(map[(player.x - player.p_speed) / TILESIZE][player.y / TILESIZE] == 0...


This is assuming your players starting positiong and speed allows him to exactly touch walls (if not I think it might give some strange results at times). With that I mean, for instance, if your TILESIZE is 32, your players speed should be 1, 2, 4, 8, 16, or 32, and the starting position should be accordingly. TILESIZE = 32, speed = 4, startx = 32, starty = 32 for instance should work with this method. You would probably want that in most cases anyway, so the player sprite wount stop like 5 pixels away from the wall, because its supposed to walk 6.

Cheers.
Yeah, but my player's speed is 32, Its tilebased movement.
blog: www.brasilokau.com/games/blog
Well in that case you only have to move your paranteses. You should add and subtract 1 after you divide :

// UPif(map[player.x / TILESIZE][(player.y -  / TILESIZE) - 1] == 0...// DOWNif(map[player.x / TILESIZE][(player.y / TILESIZE) + 1] == 0...// RIGHTif(map[(player.x / TILESIZE) + 1][player.y / TILESIZE] == 0...// LEFTif(map[(player.x / TILESIZE) - 1][player.y / TILESIZE] == 0...


Cheers.

PS! Never used SDL so no idea about the font, sorry.
it doesn't work...
i don't know why because the logic seems to be fine...
blog: www.brasilokau.com/games/blog
more time is commonly spent on optimization and corrections than figuring out the logic behind it.

Ur supposedly making a mistake somewhere.
----------------------------

http://djoubert.co.uk
Is the intention that X be the row and Y be the column? Just wondering if perhaps you have them transposed from what you intended?

if(map[player.y / TILESIZE][player.x / TILESIZE - 1] == 0) 


Hope that helps.
Have you traced through the program through a debugger to figure out if the player's and tile's position are correctly set?
Quote:Original post by Apoxol
Is the intention that X be the row and Y be the column? Just wondering if perhaps you have them transposed from what you intended?

*** Source Snippet Removed ***

Hope that helps.


OMG!!!!
THANK YOU SO MUCH!!! that was the problem :( i usually do this kind of shit, like mistyping something or lack of attention
but thanks everyone for helping me
best regards
blog: www.brasilokau.com/games/blog

This topic is closed to new replies.

Advertisement