Welcome to Phrogram Sign in | Join | Help


Re: Interesting Array Question

  •  02-12-2007, 9:31 AM

    Re: Interesting Array Question

    Attachment: SpaceSectors.kpl

    On number 1, one thought that comes to mind is to iterate through 3D space in blocks, rather than simply randomly generating X, Y and Z values, and deciding whether within each block you will place asteroid or a planet or nothing at all..  Think of dividing the 3D space in the game into 6 blocks along each axis, X, Y and Z.  This ends up with 216 (6x6x6) different blocks or sectors in 3D space.

    You could use an approach like this along with random number generation that identifies a specific number of those blocks to place asteroids and planets within.  Or, going the other way, you could simply iterate through ALL of those blocks in a nested loop, and use a random percentage within each one to decide whether to place an asteroid or a planet or nothing at all in that block of space.

    The latter would be a lot more unpredictable, and I suspect would end up with sometimes quirky results because it's based on a random percentage at each of many points in space. 

    I've attached a program that demonstrates the first approach - I threw it together quickly, but I think it does work to pick 10 random "sectors" or space, and of course from that one could place asteroids or planets in those 10.    

    Here's how the code looks:

     

    Program PickRandomSectors

     

          Method Main()

     

                Define SelectedSectors As IntegerList

               

                Define DoneGeneratingObjects As Boolean = False

               

                Define RandomSector As Integer

               

                Define Index As Integer

               

                Define SectorCoordinateX As Integer

                Define SectorCoordinateY As Integer

                Define SectorCoordinateZ As Integer

     

                While Not DoneGeneratingObjects

               

                      RandomSector = Random(0, 215)

                     

                      If Not SelectedSectors.Contains(RandomSector) Then

                            SelectedSectors.Add(RandomSector)

                      End If

                           

                      If SelectedSectors.Count = 10 Then             

                            DoneGeneratingObjects = True

                      End If

                     

                End While

     

                For Index = 1 To SelectedSectors.Count

                     

                      // Now we need to turn a value from 0 to 215 into three different values to identify

                      // the particular sector along the X, Y and Z axes

                      SectorCoordinateX = (SelectedSectors.Item(Index) / 36)  // 0 to 5

                      SectorCoordinateY = ((SelectedSectors.Item(Index) - (36 * SectorCoordinateX)) / 6)

                      SectorCoordinateZ = SelectedSectors.Item(Index) Mod 6

                     

                      PrintLine( SelectedSectors.Item(Index) )

                      PrintLine( "   (" + SectorCoordinateX + ", " + SectorCoordinateY + ", " + SectorCoordinateZ + ")")

                     

                Next

               

          End Method

     

    End Program

     

    Filed under: , , , ,
View Complete Thread