• Advertisement
Sign in to follow this  

tile based engine

This topic is 4432 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

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]

Share this post


Link to post
Share on other sites
Advertisement
Here is what I would do (didnt check this, so sorry if there are errors) :


// UP
if(map[player.x / TILESIZE][(player.y - player.p_speed) / TILESIZE] == 0
...
// DOWN
if(map[player.x / TILESIZE][(player.y / TILESIZE) + 1] == 0
...
// RIGHT
if(map[(player.x / TILESIZE) + 1][player.y / TILESIZE] == 0

...
// LEFT
if(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.

Share this post


Link to post
Share on other sites
Well in that case you only have to move your paranteses. You should add and subtract 1 after you divide :


// UP
if(map[player.x / TILESIZE][(player.y - / TILESIZE) - 1] == 0
...
// DOWN
if(map[player.x / TILESIZE][(player.y / TILESIZE) + 1] == 0
...
// RIGHT
if(map[(player.x / TILESIZE) + 1][player.y / TILESIZE] == 0
...
// LEFT
if(map[(player.x / TILESIZE) - 1][player.y / TILESIZE] == 0
...



Cheers.

PS! Never used SDL so no idea about the font, sorry.

Share this post


Link to post
Share on other sites
more time is commonly spent on optimization and corrections than figuring out the logic behind it.

Ur supposedly making a mistake somewhere.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Have you traced through the program through a debugger to figure out if the player's and tile's position are correctly set?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement