PartitionWorld
PartitionWorld WorldIndex, Size
 
Parameters:

    WorldIndex = The index of the world you wish to partition
    Size = The size of the partition regions. (recommended values 48, 64. Absolute min of 16)
Returns: NONE
 

      PartitionWorld builds a special partition map for all the elements captured in this world. This map is used to accelerate calculations like collision and ray intersections.

      When you call PartitionWorld you give the command two parameters. The first is world and the second is the partition size. What partitioning does, is builds a grid like table that represents the world. Each cell in the table is square and will represent a region of our selected size. These cells contain a list of the world elements that share this region in world.

      What this does, is allows PlayBASIC to rapidly calculate a short list of elements that are within any given area of the world. This is particularly beneficial when dealing with collision and ray intersection. As it allows those functions to focus on a smaller groups of elements, rather than everything. Greatly accelerating there functionality.



FACTS:


      * Avoid setting the partition size too small. This not only makes the structure eat up a huge amount of memory, but can actually be slower to search. I find setting the region size to somewhere between 48 and 256 to be a good balance.

      * Building the Partition Map can be a bit slow for large worlds.




Mini Tutorial:


      This example build a simple world, partitions this world then runs a series of ray intersections on the world.

  
; Get the Screen size and use it as the world size
  WorldWidth=GetScreenWidth()
  WorldHeight=GetScreenHeight()
  
; create a Camera
  CreateCamera 1
  
; Create world
  CreateWorld 2
  CaptureToWorld 2
  
; draw a series of boarder line for this world
  Line 0,0,worldwidth,0
  Line worldwidth,0,worldwidth,worldheight
  Line worldwidth,worldheight,0,worldheight
  Line 0,worldheight,0,0
  
; draw a series of polygon shaped obejct into the world
  For lp=1 To 10
     xpos#=50+Rnd(worldwidth-100)
     zpos#=50+Rnd(worldheight-100)
     size=RndRange(30,100)
     angle=Rnd(359)
     Make_Convex(RndRange(3,20),xpos#,zpos#,Size,angle)
  Next lp
  
; Partition The world up into 32 by 32 cells
  PartitionWorld 2,32
  
; Tell PB to return to Immediate drawing mode
  DrawGFXImmediate
  
  
; statrt of DO/Loop
  Do
   ; capture to scene and grab the world info
     CaptureToScene
     ClsScene
     CaptureDepth 100
     CameraGrabWorld 1,2
     
     
   ; Get the mouse position
     mx#=MouseX()
     my#=MouseY()
     
   ; Cast 150 rays out fomr the mouses position
     rays=150
     For Ray=1 To Rays
        angle#=(360.0/Rays)*Ray
        x2#=CosNewValue(mx#,angle#,300)
        y2#=SinNewValue(my#,angle#,300)
        If RayIntersectWorld(2,mx#,my#,x2#,y2#)=true
           x2#=GetIntersectX#(0)
           y2#=GetIntersectY#(0)
           CircleC x2#,y2#,3,1,RGB(255,0,0)
        EndIf
        Line mx#,my#,x2#,y2#
     Next
     
   ; draw the camera
     DrawCamera 1
     
   ; show the fps rate and continue this loop
     Text 0,0,FPS()
     Sync
  Loop
  
  
; This function creates a convex polygon shape
  
Function Make_Convex(edges,xpos#,ypos#,Size,angle)
  sa#=360.0/edges
  c=RndRGB()
  For lp=0 To edges-1
     a#=angle+(lp*sa#)
     x1#=xpos#+CosRadius(a#,size)
     y1#=ypos#+SinRadius(a#,size)
     If lp<(edges-1)
        a#=angle+((lp+1)*sa#)
     Else
        a#=angle
     EndIf
     x2#=xpos#+CosRadius(a#,size)
     y2#=ypos#+SinRadius(a#,size)
     Line x2#,y2#,x1#,y1#
  Next lp
EndFunction i
  
  


 
Related Info: CameraBasics | CreateWorld | NewWorld | QueryWorldRegion | RayIntersectWorld | SpriteCollisionMode :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com