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

Author Topic: VertexArray collision?  (Read 4176 times)

0 Members and 1 Guest are viewing this topic.

OeysteinLO

  • Newbie
  • *
  • Posts: 2
    • View Profile
VertexArray collision?
« on: November 18, 2013, 08:59:08 pm »
Hello

I am making a platformer game with my friends and i am looking into ways of making a map. Last year we made something very simple where the map was made from sf::Rect object, and we detected collisions using the intersects method.
We can do that again, but i am also interested in a tileMap using a vertex array. I dont want to use too much time just trying it and not getting it to work, so here is my question:

Is there any way to detect collision with distinct quad vertices inside the whole vertex array, or can you only get the outer bounds of the whole thing?

Just so you can easier understand my question:
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 1 1 1 1 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1
 

This represents the tilemap. The number 1 will create a quad with a texture and the number 0 will create a quad with no texture, so its transparent.
Is there a way to collide with only the '1' quads and not the '0' quads?

As i said, its no biggie if this wont work, i can just use a similar approach with sf::Rect's instead.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: VertexArray collision?
« Reply #1 on: November 18, 2013, 09:24:07 pm »
Why do you want to perform collision detection using the vertices while you have a very simple and efficient data structure: a 2D array with all the information about tiles "collidability"? Just calculate the tile coordinates of whatever you want to collide (like the player), and check in your array if it's a '0' or a '1'.

Quote
the number 0 will create a quad with no texture, so its transparent
What's the point of creating a quad that will not be visible? Can't you just not create it? ???
Laurent Gomila - SFML developer

OeysteinLO

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: VertexArray collision?
« Reply #2 on: November 18, 2013, 09:46:43 pm »
Well, i guess i didn't think about that. That seems doable. I would use the coordinates of the player, iterate the VertexArray and check if the corresponding tile is 'collideable'?

Hmm, that would save space, wouldn't it. I guess i didn't think about that either. I haven't been doing this for long, so i guess that will be my excuse...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: VertexArray collision?
« Reply #3 on: November 18, 2013, 10:43:22 pm »
Quote
iterate the VertexArray
Why iterate? The player's coordinates give you a direct access to the tile type in the array.

Working with tiles arranged in an axis-aligned 2D grid offers many opportunities to optimize and simplify algorithms. Don't over-complicate things ;)
Laurent Gomila - SFML developer

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: VertexArray collision?
« Reply #4 on: November 19, 2013, 01:57:11 pm »
I will attempt to help further.

Quote
This is just for a example, it is not fleshed out properly and is only to give you an idea what next step could be

You will probably need few things like:
Code: [Select]
int Get_Vertex_ID(int posX, int posY);//This function will take two arguments, witch represent position of a unti for example
//And will return a ID of vertex to check collision with
For example:
if you pass x=100 y=100 and you map have following characteristics:
tile_Width = 4, tile_Height = 4, tile_Size = 32, mapSizeX = tile_Width*tile_Size...
0 4 8 12
16 20 24 28
32 36 40 44
48 52 56 60
Function with x=100 y=100 would return 40 and thus you do only one to maximum of four collision detections(if your unit is same size as the tile, in case it is bigger...) and there is better code then you intended :)

But with this there are few problems.
One is that if the collision for Vertex_ID = 40 is walkable and (44,56,60) is unwalkable your unit is on that place so you need to do additional checking
Code: [Select]
int Get_Vertex_ID(int posX, int posY)
{
    if(posX%32 != 0)
        Tiles_To_Check_X = 2;
    else
        Tiles_To_Check_X = 1;
}
But then again there is another problem with this.
What if the unit is at Vertex_ID = 60 and tries moving right or down.
The code above would produce error cause the program tried accessing above Vertex_Array range.(Vertex_Count = 60) but the program tried accesing 64 or higher ^^

« Last Edit: November 19, 2013, 01:58:56 pm by BaneTrapper »
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

 

anything