QueryWorldRegion
Items = QueryWorldRegion(ThisWorld, X1, Y1, X2, Y2, Address)
 
Parameters:

    ThisWorld = The index of the world you wish to query
    X1 = The Top Left X coordinate
    Y1 = The Top Left Y coordinate
    X2 = The Bottom Right X coordinate
    Y2 = The Bottom Right Y coordinate
    Address = The Address the query items should be written into
Returns:

    Items = The Number of items within this region
 

      QueryWorldRegion is an advanced command. It's purpose is to let the user get list of what captured graphic item are stored within a particular region within the world. While it's sounds simple enough, it's implementation can be a little clunky. So bare with me.

      To query what's within at a particular region of a world, all we do is give QueryWorldRegion the world we wish to check, a rectangular region it should compare against and the address of where it should output the results.

      QueryWorldRegion check each item bounding box. If an items bounding box overlaps your query region, then this item is considered to be within this zone. Even though that actually graphic item might well be just outside of it.

      Now here's the the tricky part. The list of items QueryWorldRegion returns, will the index of the of that was captured. You can't actually access the captured item though. This means that user must keep track of the items that were captured and there positions in order to really make use of the results. Which is how the attached example works.




FACTS:


      * QueryWorldRegion expects to be passed the address of where you want the found items stored. It's recommended you use a bank for this. So when calling the QueryWorldRegion function would call the GetBankPtr function, which will get the address of your bank, and thus pass it to QueryWorldRegion. So it can store data directly in that memory

      * QueryWorldRegion assumes the address you give it will be a piece of memory that is large enough to hold the results it might generate. If it's not, your program will most probably crash.

      * QueryWorldRegion returns it's results as a list of integer values. If you've stored them within a bank it's recommended you use the PeekBankInt to retrieve them.


 
Example Source: Download This Example
; Convert the default font to a bitmap font
  MakeBitmapFont 1,$ffffff
  
  
  
; Create 2 variables that will be used as the worlds size
  WorldWidth     =3000
  WorldHeight     =3000
  
; create a Camera (this camera will attach it self to the
; current surface, in this that example, current surface will
; be the Screen)
  MyCamera=NewCamera()
  
; Limit the cameras movement to the inside the world space
  LimitCamera MyCamera,true
  LimitCameraBounds MyCamera,0,0,WorldWidth+1,WorldHeight+1
  
  
Restart:
  
; check if the world previously exists, if so, delete it
  If GetWorldStatus(MyWorld) Then DeleteWorld MyWorld
  
  
; Create world
  MyWorld=NewWorld()
  
; ReDirect All Drawing to be captured to world buffer #2
  CaptureToWorld MyWorld
  
  
; Create a list of Constants that will be used to ID the different gfx types
; that were captured into the world buffer
  ACSet =0
  Constant Gfx_Item_Dot           =AC(1)
  Constant Gfx_Item_Line           =AC(1)
  Constant Gfx_Item_Box           =AC(1)
  Constant Gfx_Item_Circle      =AC(1)
  Constant Gfx_Item_Shape      =AC(1)
  Constant Gfx_Item_Max      =AC(1)
  
; max size of a gfx item
  Size=500
  
; NUmber of gfx items to capture
  Max_Gfx_Items=250
  
  
; Define a type and array to hold the position/type information
; about each item.  Just so we can highlight them in this demo.
  
  Type tObjectInfo
     ItemType
     Coords(4)
     Radius
     FillState
     Colour
     MediaIndex
  EndType
  
  Dim Items(Max_Gfx_Items) As tObjectInfo
  
  
; Set the Capture Z Depth to 100
  CaptureDepth 100
  
; Capture the Gfx items
  For lp=0 To Max_Gfx_Items
     
   ; Random select a Item Type ot capture into the world buffer
     
     ThisGFXitem =      Rnd(Gfx_Item_Max-1)
     
   ; reset the coords
     x=0:y=0
     x2=x:y2=y
     col               =RndRGB() And $7f7f7f
     FillState     =Rnd(1)
     radius=0
     MediaIndex=0
     
     Select ThisGfxItem
             
         Case Gfx_Item_Dot
             X=Rnd(WorldWidth)
             Y=Rnd(WorldHeight)
             
           ; draw the dot the world buffer
             DotC X,Y,Col
             
         Case Gfx_Item_Line
             X=Rnd(WorldWidth)
             Y=Rnd(WorldHeight)
             
             X2=x+Rnd(size)
             Y2=Y+Rnd(size)
           ; draw the dot the world buffer
             LineC X,Y,x2,y2,Col
             
         Case Gfx_Item_Box
             X=Rnd(WorldWidth)
             Y=Rnd(WorldHeight)
             
             X2=x+Rnd(size)
             Y2=Y+Rnd(size)
           ; draw the dot the world buffer
             BoxC X,Y,x2,y2,fillstate,Col
             
         Case Gfx_Item_Circle
             X=Rnd(WorldWidth)
             Y=Rnd(WorldHeight)
             
             Radius=Rnd(100)
           ; draw the dot the world buffer
             CircleC X,Y,radius,fillstate,Col
             
             
             
         Case Gfx_Item_Shape
             Radius=Rnd(100)
             MediaIndex=GetFreeShape()
             CreateConvexShape MediaIndex,Radius,RndRange(3,20)
             
             X=Rnd(WorldWidth)
             Y=Rnd(WorldHeight)
             
           ; draw/capture the Shape the world buffer
             DrawShape MediaIndex,X,Y,1
             
     EndSelect
     
   ; Store the info about this ITem in array, so we can read it later
     
     Items(lp).ItemType     =ThisGfxItem
     Items(lp).Coords(1)     =x
     Items(lp).Coords(2)     =y
     Items(lp).Coords(3)     =x2
     Items(lp).Coords(4)     =y2
     Items(lp).Radius          =radius
     Items(lp).FillState     =FillState
     Items(lp).Colour          =Col
     Items(lp).MediaIndex     =MediaIndex
  Next
  
; Partition The world up into 64 by 64 cells
  PartitionWorld MyWorld,64
  
; Tell PB to return to Immediate drawing mode
  DrawGFXImmediate
  
  
  
  Camx#=WorldWidth/2
  Camy#=WorldHeight/2
  
  
  
  
; =================================================
; CReate a Memory bank.  This Bank is where we'll store the QueryWorldRegion results
; =================================================
  
; Create the bank with plenty of space
  CreateBank 1,1000
  
  
  
; start of DO/Loop
  Do
     
   ; Tell PB to redirect all GFX drawing commands to the scene buffer
     CaptureToScene
     
   ; Clear the scene buffer
     ClsScene
     
     
   ;Move the Camera With Arrow Keys
     CamSpeed#=5
     If LeftKey() Then CamX#=CamX#-CamSpeed#
     If RightKey() Then CamX#=CamX#+CamSpeed#
     If UpKey() Then CamY#=CamY#-CamSpeed#
     If DownKey() Then CamY#=CamY#+CamSpeed#
     
     
     
     
   ; Calc A region 200*200 around the current camera position
     
     x1#=Camx#-100
     y1#=Camy#-100
     x2#=Camx#+100
     y2#=Camy#+100
     
   ; draw this region the user can see it
     CaptureDepth 1
     Box x1#,y1#,x2#,Y2#,0
     
     
   ; Query World Region check the worlds parition buffer for a list of objects
   ; that share this region.
     
   ; The function returns the number of Items who's BOUNDING BOXES share
   ; the same region.
     
   ; NOTE:  Objects are returned in a pre-allocated memory buffer, function
   ;        expected you to pass it a POINTER to this buffer.  So you
   ;        actually get it write the valeus into an INTEGER array if you
   ;        want also.  But that's not a easy for the user at this time..
     
     
     ItemsFound=QueryWorldRegion(MyWorld,x1#,y1#,x2#,y2#,GetBankPtr(1))
     
     If ItemsFound>0
        
        CaptureDepth 10
        
        For lp=0 To (ItemsFound-1)
           
           GfxItem=PeekBankInt(1,lp*4)
           
           x     =Items(GfxItem).Coords(1)
           y     =Items(GfxItem).Coords(2)
           x2     =Items(GfxItem).Coords(3)
           y2     =Items(GfxItem).Coords(4)
           
           Radius          =Items(GfxItem).Radius
           FillState     =Items(GfxItem).FillState
           MediaIndex     =Items(GfxItem).MediaIndex
           Col               =RGBFade(Items(GfxItem).Colour,200)
           
           Select Items(GfxItem).ItemType
                   
               Case Gfx_Item_Dot
                 ; draw the dot the world buffer
                   DotC X,Y,Col
                   CircleC x,y,3,0,RndRGB()
                   
               Case Gfx_Item_Line
                 ; draw the dot the world buffer
                   LineC X,Y,x2,y2,Col
                   CircleC x,y,3,0,RndRGB()
                   CircleC x2,y2,3,0,RndRGB()
                   
               Case Gfx_Item_Box
                 ; draw the dot the world buffer
                   BoxC X,Y,x2,y2,fillstate,Col
                   CircleC x,y,3,0,RndRGB()
                   CircleC x2,y2,3,0,RndRGB()
                   CircleC x2,y,3,0,RndRGB()
                   CircleC x,y2,3,0,RndRGB()
                   
               Case Gfx_Item_Circle
                 ; draw the dot the world buffer
                   CircleC X,Y,radius,fillstate,Col
                   
                   CircleC x,y,2,0,RndRGB()
                   
                   
               Case Gfx_Item_Shape
                   Ink Col
                 ; draw/capture the Shape the world buffer
                   DrawShape MediaIndex,X,Y,2
                   
           EndSelect
           
         ; Store the info about this ITem
         ;               Items(lp).ItemType=ThisGfxItem
           
        Next
        
     EndIf
     
     
     PositionCamera MyCamera,CamX#-(GetScreenWidth()/2),Camy#-(GetScreenHeight()/2)
     
     CaptureDepth 100
     CameraGrabWorld MyCamera,MyWorld
     
     
   ; draw the camera
     DrawCamera MyCamera
     
   ; show the fps rate and continue this loop
     Text 0,0,FPS()
     Text 0,10,itemsfound
     
     If SpaceKey() Then Goto restart
     
     Sync
  Loop
  
  
  
  
 
Related Info: CameraBasics | GetWorldClosestPoint | GetWorldElements | PartitionWorld | RayIntersectWorld | SpriteCollisionMode | WorldElementVisible :
 


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