Vector tile map, collision detection

Hello people, I have created a 2D Map for my game.
My Vector stores the following numbers:
1
2
3
4
5
6
7
8
9
10
11
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 3 0 4 2 2 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 4 2 2 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1


0 is transparent
1 is dirt
2 is grass
3 is grass block with left & right side ending
4 is left grass block
5 is right grass block.

I then have a gravity formula that looks like the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
if(key[LEFT])
{
	dx = -3;
	//x -= moveSpeed;
}
else if(key[RIGHT])
{
	dx = 3;
	//x += moveSpeed;
}
else
{
	dx = 0;
}

if(key[SPACE] && jumping)
{
	dy = -jumpSpeed;
	jumping = false;
}

if(!jumping)
	dy += gravity;
else
	dy = 0;

x += dx;
y += dy;

jumping = (y + 32 >= 288);

if(jumping)
	y = 288 - 32;


How would I instead of having the collision at y pos: 288 have it in a way, so it's collision with the following tiles:
1, 2, 3, 4 & 5?
Last edited on
Bump
I'd guess that x/y are the coordinates in the vector?
If so simply check the field wether it contains one of those numbers.

If you want it as a kind of barrier you need to determine what's the way is.
For instance
1
2
3
4
5
6
7
8
9
10
11
step = dx/dy;
current_y = y;
for(i = 0; i < dx; i += step)
{
  for(j = 0; j < step; ++j)
  {
    if(vector[x + i + j][current_y] != 0)
      ...
  }
  ++current_y;
}
No x/y is the coordinates of the player, it is just two integer variables holding the value of the player position.
The vector contains the whole map.txt, which is:
1
2
3
4
5
6
7
8
9
10
11
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 3 0 4 2 2 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 4 2 2 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1


The numbers represent which tile to use.
Here's the tilesheet:
http://i.imgur.com/UIzMVpy.png

Basically what I want to do, is to check is the player is currently colliding with tiles 1-5.
How would I approach this?
Last edited on
Just bumping this thread
The issue is how are the x/y coordinates are related to the map?
If it is pixel within the map you may calculate the index like so:

index_in_map = pixel / tile_size_in_pixel

for both x and y. So this is an example how you walk thru the map and finding the collisions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// calculate the indexes...
step_index = index_dx/index_dy;
for(i = 0; i < dx; i += step)
{
  for(j = 0; j < step; ++j)
  {
    switch(vector[index_x + i + j][index_y])
    {
    case 1:
      ...
    }
  }
  ++index_y;
}
within the switch you need to deal with the collision (like stopping the player)
I have managed to fix the problem, by rewriting the whole thing, doing arrays instead of vectors.
Topic archived. No new replies allowed.