Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Help with Tile collisions  (Read 17862 times)

0 Members and 1 Guest are viewing this topic.

gop_t3r

  • Newbie
  • *
  • Posts: 35
    • View Profile
Help with Tile collisions
« on: February 06, 2014, 04:13:47 pm »
So far I got collisions between sprites that are of type sf::Sprite; but my tiles are not of type sf::Sprite so the collisions do not work. My tiles are of sf::VertexArray type, and I used the tile-map as a base from

http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php

I can get my player to check for collisions with the tile-map, but the only problem is that because all the numbers represent a tile the collision check returns true.

Using the code from the tutorial above, how can I implement a simple tile collision with a player that has a type sf::Sprite? Thanks for reading.
« Last Edit: February 10, 2014, 04:13:43 pm by gop_t3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Help with Tile collisions
« Reply #1 on: February 06, 2014, 04:26:23 pm »
Not sure what you've already been using, but since you want a simple way to detect collision, you can just go with sf::Rect<T>::contains() function.

The sf::Sprite provides you a handy function to get it's global bounds: getGlobalBounds(), it returns a sf::FloatRect. All you now need to do is write a function to retrieve the global bounds of your tile. If there's no scaling or rotation involved with your vertex array, you can simply go and use the four vertices of your tile to construct the global bounds.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

gop_t3r

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Help with Tile collisions
« Reply #2 on: February 06, 2014, 04:52:42 pm »
That could work, but again, it will always return true because the player is in the tile map to begin with. How can I discriminate so that only some of the tiles are collidable and others aren't?

AN71

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Help with Tile collisions
« Reply #3 on: February 06, 2014, 05:48:11 pm »
Well, I, for one, am now confused.

Are you using  a grid system? Are you pre-rendering your screen to a buffer that is the size of the screen, and using that as the grid (making the boundaries of tiles unavailable)? I'm rather perplexed by what you mean by
Quote from: gop_t3r
it will always return true because the player is in the tile map to begin with
Isn't this the same for most tile games? Are you using some unusual method that prevents the tile's boundaries from being accessed on an individual basis? The more info given, the easier it is to find a solution :)
Quote from: Friedrich Nietzsche
He who would learn to fly one day must first learn to stand and walk and run and climb and dance; one cannot fly into flying.

Assassin0795

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Help with Tile collisions
« Reply #4 on: February 07, 2014, 02:32:46 am »
Why not just assign a particular state to tiles set by 2 and 3? I.e. If your character goes to move onto a pixel that part of the tile marked by the number 2 (let's say 2 denotes a wall tile), then movement in that particular direction will fail.

Or is this what you're stuck at? I apologize if I'm just rephrasing your question.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Help with Tile collisions
« Reply #5 on: February 07, 2014, 08:19:13 am »
You simply map the numbers to the wanted tile type and actually even construct the bounds from it (it's easy given that all tiles have the same size.

So iterate over the tile map, check if it's a collideable tile type (wall etc), if so construct the bounds, check for collision.

The next step and sometimes harder one, is to properly react to the collision. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

gop_t3r

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Help with Tile collisions
« Reply #6 on: February 07, 2014, 08:23:14 pm »
Thanks for your responses, I know which tiles I want collidable but I need to know how it can be implemented. Using the code above as to loading a tile, what must I modify in order to make tiles 2 and 3 collidable? Using a finite state in order to control this would be very helpful but I'm open to other ideas. So far I've been able to grep all the tile numbers according to their value using an iterator, but I figure that may not be enough since those are just numbers, not the tiles themselves. Suffice it to say I don't know how I can check if it's a collidable type since it's just a number, unless I say: if the number is 2 or 3, then the tile is collidable. It's the latter that I don't know how to implement. Sorry for the confusion!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help with Tile collisions
« Reply #7 on: February 07, 2014, 08:40:36 pm »
Using the code above...what must I modify in order to make tiles 2 and 3 collidable?
You wouldn't be modifying the code in the TileMap class. That just creates the actual image to display the map. You would be testing again the values in the array that holds the data on which tile should be shown at the tile position.

Do you know which position in the grid that your sprite is in? (i.e. in tile coordinates rather than pixel coordinates)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

gop_t3r

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Help with Tile collisions
« Reply #8 on: February 08, 2014, 12:36:03 am »
Using the code above...what must I modify in order to make tiles 2 and 3 collidable?
You wouldn't be modifying the code in the TileMap class. That just creates the actual image to display the map. You would be testing again the values in the array that holds the data on which tile should be shown at the tile position.

Do you know which position in the grid that your sprite is in? (i.e. in tile coordinates rather than pixel coordinates)

No, I don't know; how do I find out where my sprite is in tile coordinates?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help with Tile collisions
« Reply #9 on: February 08, 2014, 01:08:57 am »
Do you know which position in the grid that your sprite is in? (i.e. in tile coordinates rather than pixel coordinates)
No, I don't know; how do I find out where my sprite is in tile coordinates?
Starting at the position that you display your tilemap, you just need to work out how many times tileSize has been passed - in both x and y. e.g. (sprite.x - tilemapOrigin.x) / tileSize.x
That is assuming that your tilemap's origin is still located at the top-left corner of the map.
You might also want to round it down to get the exact grid position.
The results will be where in the grid the sprite is and you can then know which tile values (in the array) you need to be checking.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

gop_t3r

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Help with Tile collisions
« Reply #10 on: February 08, 2014, 01:27:55 am »
Do you know which position in the grid that your sprite is in? (i.e. in tile coordinates rather than pixel coordinates)
No, I don't know; how do I find out where my sprite is in tile coordinates?
Starting at the position that you display your tilemap, you just need to work out how many times tileSize has been passed - in both x and y. e.g. (sprite.x - tilemapOrigin.x) / tileSize.x
That is assuming that your tilemap's origin is still located at the top-left corner of the map.
You might also want to round it down to get the exact grid position.
The results will be where in the grid the sprite is and you can then know which tile values (in the array) you need to be checking.

When you refer to sprite.x, do you mean the player's sprite? How do I get the tilemap origin? There's no get origin function for Vertex Arrays.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help with Tile collisions
« Reply #11 on: February 08, 2014, 03:01:16 am »
When you refer to sprite.x, do you mean the player's sprite? How do I get the tilemap origin? There's no get origin function for Vertex Arrays.
Yes, I just meant the x position of the sprite. By origin I meant the position you draw it at. You should be able to do tilemap.setPosition(x, y) to choose where it goes. I believe that the default position is top-left corner, which is (0, 0).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

gop_t3r

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Help with Tile collisions
« Reply #12 on: February 08, 2014, 04:02:17 am »
I am no philosopher to be pragmatic as to know what we're supposed to do with tileSize. Right now, am clueless what you're asking for here, and I'd understand better if you elaborated how I'm supposed to use tileSize in order to get a tile collision through code rather than describing what's supposed to happen. I'm following what you're doing but I have no idea where I'm supposed to implement and how it's all coming together; everything's just fragmented.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help with Tile collisions
« Reply #13 on: February 08, 2014, 04:19:34 am »
I was talking only about finding out which tile your sprite is "in" so that you can know which tile it is in the array. That way, you can decide if it's a tile that should be collided with or should be ignored.
Then, when you know whether it should be collided with or not, you can compare the bounding box of the sprite and the tile.

That sort of thing...
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

gop_t3r

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Help with Tile collisions
« Reply #14 on: February 08, 2014, 05:15:30 am »
Okay, I think I understand. You mean I need to create a getTileSize() function wherein I just get the size of the tile and test whether it's been collided with or not?
« Last Edit: February 11, 2014, 07:52:36 pm by gop_t3r »

 

anything