N
N
noob3332016-08-05 07:05:11
Game development
noob333, 2016-08-05 07:05:11

(SDL2 + C) Creating a game level with tiles in a 2D platformer game and collision detect, ways?

I'm trying to make a 2D platformer in C with SDL2, I settled on creating a level and checking for collisions, in general, how to do this is clear, I can draw one object and make it collide with another, but I need to draw a large level, and here is the complexity.
At first there was a question of how, in fact, to create a level, platforms on which the character will run. My level consists of many tiles of 20x20 pixels in size, I chose the method from Google with a two-dimensional array, in which there will be p and b, p - empty, b - block. Next, two loops are made with a pass through each element of the array, and if the element is equal to b, then a block is drawn there, something like this:

for (int y = 0; y < arrow_size; y++){
 for (int x = 0; x < arrow_size; x++){
  if(my_level[y][x] == 'b'){
   SDL_Rect block_info = {(x * 20), (y * 20), 20, 20)
   SDL_RenderCopy(render,block_texture, NULL, &block_info);
  }
 }
}

And everything seems to be fine, but how to detect collisions in this way? I couldn't find a proper answer. Please help with this method or advise another, thanks.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
RedHairOnMyHead, 2016-08-05
@ThePyzhov

When pressing the movement key, check if there is a wall block there. If not, move the character; if there is, don't move.

N
napa3um, 2016-08-05
@napa3um

jonathanwhiting.com/tutorial/collision

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question