RayIntersectWorld
Status = RayIntersectWorld(WorldIndex, RayX1#, RayY1#, RayX2#, RayY2#)
 
Parameters:

    WorldIndex = The index of the world you wish to partition
    RayX1# = The starting X coordinate of ray
    RayY1# = The starting Y coordinate of ray
    RayX2# = The ending X coordinate of ray
    RayY2# = The ending Y coordinate of ray
Returns:

    Status = Returns 1 when ray hits a line, or 0 when it doesn't
 

      The RayIntersectWorld function checks if a ray intersects a world. The function will return a True(1) if an impact occurs, or False(0).




FACTS:


      * The ray only checks for intersections with visible lines within the world

      * If an intersection occurs, you can get the intersection point by using the GetIntersectX#(), GetIntersectY#()

      * If an intersection occurs, you can get the normal at the point of intersection using the GetNormalX# and GetNormalY# functions

      * If an intersection occurs, you can get World Element index using the GetIntersectObject command

      * RayIntersectWorld returns the closest impact point to the rays starting position.





Mini Tutorial:


      This example builds a simple world, partitions it, then runs a series of ray intersections upon the world.


  
; 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
  
  
; statrt of 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()
     
   ; Cast 150 rays out from 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(MyWorld,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 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 | GetSpriteCollisionMode | GetWorldElements | GetWorldElementVisible | LineHitShape | LinesIntersect | NewWorld | PartitionWorld | QueryWorldRegion :
 


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