10.03.2019 - Round 1 results of our "A Love Letter For FreeBASIC" game dev competition have been published. Please be sure to check the results thread: http://games.freebasic.net/forum/index.php?topic=629.0. Don't forget that the competition is continuing with a round 2, lasting till 29th of April, 300 USD first prize. Stay tuned!

Author Topic: What data structure can I use to draw walls on a map?  (Read 4188 times)

Pyramidschem

  • Amoeba
  • *
  • Posts: 3
    • View Profile
What data structure can I use to draw walls on a map?
« on: January 13, 2013, 05:49:04 PM »
Hello again,

I am still working on my little project. I want my circles to be able to move on a labyrinth-like map ( omg i'm rewritting pacman :) ).

As I am a very beginer, I don't know at all how to store my walls definitions. I've thought about saving only the two extremities of lines so that each time I read such a couple of coordinates in a table I will redraw the lines.

The thing that doesn't satisfy me is about collision. Doing such a table doesn't help me to save collisions tests when they are not needed. I've heard about z-buffer and so on in my very youth, but I've forgotten all of that now. Is there a simple structure I should use you are awared about, and you know perfectly working for my purpose?

Thanks if so, because it is possible to waste or gain a lot of time with those data structures depending on how you are ignorant about them.


Jonge

  • Novice
  • ***
  • Posts: 78
    • View Profile
    • codeheim.NET
Re: What data structure can I use to draw walls on a map?
« Reply #1 on: January 14, 2013, 03:00:17 PM »
I'm not sure this is what you want, but here's an example on how to do collision detection on a simple tile map:
Code: [Select]
#Include "fbgfx.bi"

Declare Function Collision(x As Integer, y As Integer) As Integer

'A variable to hold some tiles
Dim As FB.IMAGE Ptr Tile(1)

'An array to hold the tile map
Dim Shared As Integer Map(9, 9)

'Setup a small GFX window
ScreenRes 320, 240, 32

'Create a couple of tiles
Tile(0) = ImageCreate(20, 20, RGB(192, 192, 192))
Tile(1) = ImageCreate(20, 20, RGB(45, 45, 45))

'Copy the map data into the Map array
Restore TileMap
For y As Integer = 0 To 9
For x As Integer = 0 To 9

Read Map(x, y)

Next
Next

'Create some needed variables
Dim As Integer Quit, PutX, PutY, PlayerX = 30, PlayerY = 30

'The main loop
Do

'Get some keyboard input, and move the player
If MultiKey(FB.SC_ESCAPE) Then Quit = -1

If MultiKey(FB.SC_UP) Then
If Not Collision(PlayerX, PlayerY - 1) Then PlayerY -= 1
End If

If MultiKey(FB.SC_DOWN) Then
If Not Collision(PlayerX, PlayerY + 1) Then PlayerY += 1
End If

If MultiKey(FB.SC_LEFT) Then
If Not Collision(PlayerX - 1, PlayerY) Then PlayerX -= 1
End If

If MultiKey(FB.SC_RIGHT) Then
If Not Collision(PlayerX + 1, PlayerY) Then PlayerX += 1
End If

'Draw the tile map
ScreenLock
PutY = 0
For y As Integer = 0 To 9
PutX = 0
For x As Integer = 0 To 9

Put (PutX, PutY), Tile(Map(x, y)), PSet
PutX += 20

Next
PutY += 20
Next

'Draw the "player"
PSet (PlayerX, PlayerY), RGB(255, 255, 255)

'Sync the drawing to the monitors refressrate
ScreenSync
ScreenUnLock

Loop Until Quit


'The collison check function
Function Collision(x As Integer, y As Integer) As Integer

Dim As Integer MapX, MapY

'Tile widht and height = 20
MapX = x \ 20
MapY = y \ 20

If Map(MapX, MapY) = 1 Then
Return -1
Else
Return 0
End If

End Function



TileMap:
Data 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 1, 1, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 0, 0, 0, 0, 0, 0, 0, 0, 1
Data 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
« Last Edit: January 14, 2013, 03:28:20 PM by Jonge »
codeheim.NET - Homemade game madness!

Pyramidschem

  • Amoeba
  • *
  • Posts: 3
    • View Profile
Re: What data structure can I use to draw walls on a map?
« Reply #2 on: January 14, 2013, 03:33:39 PM »
Thanks a lot, I will study it and then report.

Edit1 :
Ok I'm this type of guy that needs pictures. So here what I got for begin.


I didn't reach yet your nice collision function. I've to deal with the fact that I want to be able to choose the screen resolution at the execution. So the size of the map array may change.

Maybe I see one solution, it would be to design the map at his higher resolution and then fix the datas accordingly, then to make a sort of rescale function. One fact is that I need a good collision function like what you've shown above, so I've to keep the most part of your code.

I'm watching this right now. Feedback will come later.
« Last Edit: January 15, 2013, 07:36:53 AM by Pyramidschem »