2d Collision

Kamac

Active Member
Licensed User
Longtime User
So, is there an implemented 2d boxes or pixel-perfect/circle collision, or we are supposed to write them ourselves? I'd like to know if i will be capatible to do everything which will fulfill my needings before i purchase :).


:sign0085:



@Edit

And is there any handful arrays library?

Because setting every value of array is pretty tought.
 
Last edited:

Kamac

Active Member
Licensed User
Longtime User
For now you will need to write the collision detection method yourself.

Hmm. Allright.

You can use the Array keyword to fill an array:

I'd rather mean of arrays to create maps (multi-dimensional). Here's an example of what i mean wrote in C/C++

B4X:
Map[5][5]={
{ 1,1,1,1,1 },
{ 1,0,0,0,1 },
{ 1,0,0,0,1 },
{ 1,0,0,0,1 },
{ 1,1,1,1,1 },
};

And here's how i would draw map with it:

B4X:
For(int y, y<5; y++)
{
    For(int x, x<5; x++)
    {
        if(Map[y][x] == 1)
        {
            //Print some image of wall
        }
    }
}

I don't really know if that's possible in B4A.
 
Upvote 0

ssg

Well-Known Member
Licensed User
Longtime User
Hi Kamac,

Multi-dimensional arrays are possible with B4A (I've not gone beyond 2 dimensions yet though), as well as your logic for looping the arrays and drawing the maps, yep.. they're do-able in B4A.

Cheers!
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
I have a maze program I am working on, which is similar to your 2d map.

I have a 2d array
B4X:
Dim Maze (20,20) As Boolean
Then after generating the maze in this array (True = a wall, False = empty space) I draw it with
B4X:
Sub DrawMaze
   Dim DestRect As Rect 

   For i = 0 To 19
      For j = 0 To 19
         DestRect.Initialize (i*CellSize, j*CellSize, (i+1)*CellSize , (j+1)*CellSize)
         If Maze (i,j) Then      
            MazeCanvas.DrawBitmap ( Wall, Null, DestRect)
         Else
            MazeCanvas.DrawBitmap ( Empty, Null, DestRect)
         End If
      Next
   Next
   Activity.Invalidate 
End Sub
 
Upvote 0

ssg

Well-Known Member
Licensed User
Longtime User
here you go, 2 dimensions array of panels which displays with a random color.

B4X:
Sub Globals
   Dim p(10,10) As Panel 'use imageview if u wanna use tiles images
End Sub

Sub Activity_Create(FirstTime As Boolean)
   For i = 0 To 9
      For j = 0 To 9
         p(i,j).Initialize("")
         Activity.AddView(p(i,j), i * 10%x, j * 10%y, 10%x, 10%y)
         p(i,j).Color = Rnd(Colors.Black,Colors.white) 'set your image to panel or imageview here
      Next
   Next
End Sub
 
Upvote 0

Kamac

Active Member
Licensed User
Longtime User
I know all of these guys, i just don't know if it is possible to assign values to my 2 dimensional array.

So if i have:

B4X:
Dim Maze (5,5) As Integer

I want to draw my maze.

0 for grass, 1 for wall and 2 for sand.

In C++ i can write to my array like that:

B4X:
Maze(5,5) = {
{ 1,1,1,1,1 },
{ 1,2,2,2,1 },
{ 1,0,0,0,1 },
{ 1,0,0,0,1 },
{ 1,1,1,1,1 },
};

And i don't know how can i write my map as in C++ i did.

@kickaha

How do you define how your maze will look like?

That's what i'm asking for :).
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
There is no way to directly assign values to multi-dimensional arrays that is similar to C++, you would have to assign each value separately.

When I am defining an array like that, I populate it from a text file, that way the map is separate and can be changed without changing the code.

The maze program that the code is from generates the maze, so each value is set individually.
 
Upvote 0

Kamac

Active Member
Licensed User
Longtime User
Okay, tell me if that code would be valid&working for your method please:

B4X:
Dim Table As String
   Table = File.ReadString(File.DirInternal,"ScoreTable.txt")
        Dim ArrayOf6Dimensions(3,3,3) As Int
        Dim Line as Int
        Line = 0
   c = Table.IndexOf(Level)
   Table = Table.SubString(c)
   For y = 0 To 2
                For x = 0 to 2
         d=Table.IndexOf(",")
         ArrayOf6Dimensions(y,y,y) = Table.SubString2(Line,d)
         Table = Table.SubString(d + 1)
                Next
        Line = Line + 4
   Next

Or paste your method ;)?
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Sorry mate, but I really cannot follow your code.

I do it this way:

Lets assume your map is stored in assets, and is named Map.txt. Map.txt contains integers
B4X:
1,2,3
4,5,6
7,8,9
so a 3x3 map just for illustration

First load up the StringUtils library
Then use this code to extract the data into a 3x3 array of Integers called MyMap
B4X:
Dim MyMap (3,3)As Int   ' this would normally be a global - I put it here
         ' so you can see what the code is working on
Dim su As StringUtils
Dim Table As List
Dim Dummy () As String

Table = su.LoadCSV(File.DirAssets, "Map.txt", ",") 

For i=0 To 2
   For j= 0 To 2
      Dummy = Table.Get (i)
      MyMap (i,j)= Dummy(j)
   Next
Next

Now thats (for me) the easy way
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
If you add the file (inthe Files tab of the IDE), it will be stored in the apk, and be available via File.DirAssets. It is also stored in the "Files" directory of your app, so you can edit it from there - it will be updated when you compile your code.

You are correct, it is THAT simple, and it allows you to tinker with your map without messing with your code. I think this method is superior to hardcoding.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
No - one spotted my error, code should have been
B4X:
Dim MyMap (3,3)As Int   ' this would normally be a global - I put it here
         ' so you can see what the code is working on
Dim su As StringUtils
Dim Table As List
Dim Dummy () As String

Table = su.LoadCSV(File.DirAssets, "Map.txt", ",") 

For i=0 To 2
   Dummy = Table.Get (i)
   For j= 0 To 2
      MyMap (i,j)= Dummy(j)
   Next
Next
 
Upvote 0
Top