GetWorldClosestPoint
State = GetWorldClosestPoint(WorldIndex, PointX#, PointY#, X1#, Y1#, X2#, Y2#)
 
Parameters:

    WorldIndex = The world you wish to query
    PointX# = The test points X position
    PointY# = The test points Y position
    X1# = The test regions top left X coordinate
    Y1# = The test regions top left Y coordinate
    X2# = The test regions bottom right X coordinate
    Y2# = The test regions bottom right Y coordinate
Returns:

    State = State of query. 0= Nothing close enough, 1 equals found clsoest point.
 

      The GetWorldClosestPoint function locates the closest point within a world from a test point. This test is preformed with a user defined zone. The function returns a true(1) if the closest point was inside the test zone, and false when there's nothing close enough.



FACTS:


      * GetWorldClosestPoint only calculates the closest point to line segments within a world. (Collision worlds)

      * The closest point coordinates can be accessed using the GetIntersectX#/GetIntersectY# functions

      * The closest point normal can be accessed using the GetNormalX# & GetNormalY# functions





Mini Tutorial:


      This example has two main parts.

      Part 1 - creates a test collision world. (collision worlds only have lines segments within them)

      Part 2 - Checks a 400 * 200 zone around the mouse pointer, looking the closest impact point upon any lines segement within this zone.


  
; ==============
; Part #1 - create a world to collide with
; ==============
  
; Get the Screen size and use it as the world size
  WorldWidth=GetScreenWidth()
  WorldHeight=GetScreenHeight()
  
; create a Camera
  MyCamera=NewCamera()
  
; Create world
  MyWorld=NewWorld()
  CaptureToWorld MyWorld
  
; 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 MyWorld,32
  
; Tell PB to return to Immediate drawing mode
  DrawGFXImmediate
  
  
  
  
  
  
; start of programs main DO/Loop
  Do
   ; capture to scene and grab the world info
     CaptureToScene
     ClsScene
     CaptureDepth 100
     CameraGrabWorld MyCamera,MyWorld
     
   ; Get the mouse position
     mx#=MouseX()
     my#=MouseY()
     
     
     
   ; ===============================================
   ; Find the closest impact point within a region
   ; ===============================================
     
     x1#=mx#-200
     y1#=my#-100
     x2#=mx#+200
     y2#=my#+100
     
   ; draw open box to represent this region
     BoxC x1#,y1#,x2#,y2#,0,RGB(0,255,0)
     
     
     result=GetWorldClosestPoint(MyWorld,mx#,my#,x1#,y1#,x2#,y2#)
     
   ; Check if any point was close enough to worry about
     If result<>0
        
      ; Get the Closest POint
        ClosestX#=GetIntersectX#(0)
        ClosestY#=GetIntersectY#(0)
      ; draw circle at closest points position
        CircleC ClosestX#,ClosestY#,5,0,RGB(255,0,0)
        
      ; get the normals of this point
        NX#=GetNormalX#(0)
        NY#=GetNormalY#(0)
        
      ; draw line representing this points direction
        LineC ClosestX#,ClosestY#,ClosestX#+(nx#*20),ClosestY#+(ny#*20),RGB(0,0,255)
     EndIf
     
     
   ; draw the camera
     DrawCamera MyCamera
     
   ; 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: CreateWorld | NewWorld | QueryWorldRegion | RayIntersectWorld :
 


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