Vector tile map, collision detection

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 945 Views - Last Post: 10 December 2014 - 10:41 AM Rate Topic: -----

#1 MatiasMunk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-March 14

Vector tile map, collision detection

Posted 10 December 2014 - 09:12 AM

Hello people, I have created a 2D Map for my game.
My Vector stores the contents of map.txt which contains the following:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 draw.
Here's the tilesheet:
Posted Image

I then have a gravity formula that looks like the following:
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 collision is with the following tiles:
1, 2, 3, 4 & 5?

Is This A Good Question/Topic? 0
  • +

Replies To: Vector tile map, collision detection

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14045
  • View blog
  • Posts: 56,201
  • Joined: 12-June 08

Re: Vector tile map, collision detection

Posted 10 December 2014 - 09:16 AM

Make a method that translates screen pixel locations to a single number and vise versa. They are squares, right? So you can have an easy time doing the math to say if you are between this point and that point then you must be in square x,y.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14045
  • View blog
  • Posts: 56,201
  • Joined: 12-June 08

Re: Vector tile map, collision detection

Posted 10 December 2014 - 09:22 AM

Example.. your grid is three squares by three squares. Visually each square is 10 pixels big. I know which square I am on the X axis because values 0 to 9 are square 0 in the array, 10 to 19 is square 1.. 20 to 29 is square 2.. Vertically it is the same too. The total grid is 30 pixels (three squares time 10 pixels).

That means, with some minor bit of math, if I say throw out an x value of 15 how would I know what square I am in? Easy! You divide 15 by the number of pixels in each square.

15/10 = 1.5

We know we do not have a 1.5 square, but we do know that from 10 to 19 square one.. so we round down (or use the floor).

15/10 = 1.5
round down 1.5 = 1

we are in square 1!

If I am in x value of 8 where am I?

8/10 = 0.8
the floor of 0.8 is 0 so I know I am square 0!

With a bit of math we can map pixel coordinates to an array whole number!
Was This Post Helpful? 0
  • +
  • -

#4 MatiasMunk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-March 14

Re: Vector tile map, collision detection

Posted 10 December 2014 - 09:26 AM

I don't quite get where you want me to go.
But from what I understand, and do not know how to approach, I need some method, that translates each number so it has an x & y position.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14045
  • View blog
  • Posts: 56,201
  • Joined: 12-June 08

Re: Vector tile map, collision detection

Posted 10 December 2014 - 09:28 AM

I gave you the math. Basic division with a floor function.
Was This Post Helpful? 0
  • +
  • -

#6 MatiasMunk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-March 14

Re: Vector tile map, collision detection

Posted 10 December 2014 - 09:29 AM

But it's impossible for the code to know where the specific block is located on the x/y scale?

All it knows is that the tile type.
Sorry if this is double post, I don't know how to edit posts.
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14045
  • View blog
  • Posts: 56,201
  • Joined: 12-June 08

Re: Vector tile map, collision detection

Posted 10 December 2014 - 09:34 AM

Your blocks are square, right? With a fixed height and width, right? Then, as I said, it is just math. Look at my example and draw it out if need be. Since 0,0 typically starts in the upper left I know where those points are. I know I can draw a grid by counting ever N pixels on the X and Y and drawing a line.
Was This Post Helpful? 0
  • +
  • -

#8 MatiasMunk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-March 14

Re: Vector tile map, collision detection

Posted 10 December 2014 - 09:40 AM

Oh I think I know what you want me to do.
You want me to divide the player X and Y position with 32, since the width & height of each tile is 32 pixels.
Then you want me to apply the floor function to the result, and that is the tile number I am on?

Still then, how do I know what tile the blocks are on?
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14045
  • View blog
  • Posts: 56,201
  • Joined: 12-June 08

Re: Vector tile map, collision detection

Posted 10 December 2014 - 09:44 AM

Yes! YES!

Now.. for the blocks and tiles. Are they stackable, or is it only one tile to one square? If the latter then you know in your number array that anytime an entry is 0 you draw the dirt tile. If the value is 1 your draw the tree tile.. etc. That sort of logical leap/functionality comes from you.
Was This Post Helpful? 0
  • +
  • -

#10 MatiasMunk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-March 14

Re: Vector tile map, collision detection

Posted 10 December 2014 - 09:55 AM

They are not stackable, it's only 1 tile to 1 square, for now. Later when I get better at this stuff, I will have them stackable so I can have a tree that can be cut down.

Anyway, here's what I did, like you said.

int x = 96, y = 32, dx, dy, gravity = 1;
double playerTileX = x / 32, playerTileY = y / 32;
floor(playerTileX);
floor(playerTileY);



But anyway, to check collision can I do this then?
if(key[RIGHT])
    if(playerTileX + 1 == 1)
    //Collision
    else
    x += 3;



Edit:
No nvm, I cannot do this.
I am not that good with Vectors.
How do I retrieve the tile number from the map based on the x & y?
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14045
  • View blog
  • Posts: 56,201
  • Joined: 12-June 08

Re: Vector tile map, collision detection

Posted 10 December 2014 - 09:59 AM

Quote

How do I retrieve the tile number from the map based on the x & y?

Like I said - math. For a given pixel location X divide it by your common width of a square, and take the floor.

Quote

But anyway, to check collision can I do this then?

Since you only care about the player's relation on the array you would use that. a player can be on square 1, 5, but not on a non square like 1.4, 2.8. Your tiles are all whole numbered locations too (thank you arrays!). You would just check before the player moves if a tile is something they cannot be on.

Example:
If the player is on 3,3 and is looking to move to 3,4. Before the player moves you would inspect 3,4 on the array and see it is a mountain! A player cannot be on a mountain tile so you would not allow the move to happen. If, on the other hand, the 3,4 is a grass tile (and the player can be on the grass tile) you would allow the move to happen and the player's new location is now 3,4.
Was This Post Helpful? 0
  • +
  • -

#12 MatiasMunk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-March 14

Re: Vector tile map, collision detection

Posted 10 December 2014 - 10:13 AM

I see, but like I said, I am not that good with vectors.
What function do I use to get the number of the specific tile based on the location?
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14045
  • View blog
  • Posts: 56,201
  • Joined: 12-June 08

Re: Vector tile map, collision detection

Posted 10 December 2014 - 10:15 AM

This has little to do with vectors, but with basic math.


Quote

What function do I use to get the number of the specific tile based on the location?

I thought that is what the array is for. If you have coordinate 3, 4 you would plug those into your array indexes and get the value stashed there.
Was This Post Helpful? 0
  • +
  • -

#14 MatiasMunk   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-March 14

Re: Vector tile map, collision detection

Posted 10 December 2014 - 10:23 AM

My vector just stores the tile numbers of the tilesheet, not x/y positions.
so each 0 in the map.txt is empty.
each 1 is a dirt block.

So what I don't undestand is how would I get all the dirt block positions and check if there's a collision.

The full source code along with map.txt and tiles.png is here:
https://www.dropbox....viewer.rar?dl=0
Was This Post Helpful? 0
  • +
  • -

#15 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14045
  • View blog
  • Posts: 56,201
  • Joined: 12-June 08

Re: Vector tile map, collision detection

Posted 10 December 2014 - 10:27 AM

Quote

My vector just stores the tile numbers of the tilesheet, not x/y positions.

How do you access something in an array? You plug in an index. Make the intuitive leap that the indexes represent the X,Y on a grid. Come on.

Yeaaaaaaah.. I am not going to download some random RAR. If you want to share the code then post it in the thread.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2