TriangleHitMap
Collision = TriangleHitMap(ThisMap, TheLevel, X1, Y1, X2, Y2, X3, Y3)
 
Parameters:

    ThisMap = The Index of the map you query for a collision
    TheLevel = The level within the map to query
    X1 = The X coordinate of Vertex 1
    Y1 = The Y coordinate of Vertex 1
    X2 = The X coordinate of Vertex 2
    Y2 = The Y coordinate of Vertex 2
    X3 = The X coordinate of Vertex 3
    Y3 = The Y coordinate of Vertex 3
Returns:

    Collision = The collision state. It'll return True (1) with the polygon hit something and false (0) if it didn't
 
      The TriangleHitMap() function checks if a triangle (3 sided polygon) region collides (overlaps) a map level. The collision in this command is based on the regions and not the pixels. Each block in the level has a rectangular region that it covers. So this command compares it's area with the regions of the level. If any overlap, it'll return a true(1) for an impact, it not it'll return a false (0)



FACTS:


      * TriangleHitMap compares the the region with the map not at pixel level, but at the rectangular level. Use TriangleHitMapPixels for a pixel to pixel level impacts.

      * TriangleHitMap only supports convex polygons.

 
Example Source: Download This Example
;     -----------------------------------------------
;       ---->> GENERIC POLYGON TO MAP DEMO <<-----
;     -----------------------------------------------
  
  
;     -----------------------------------------------
;           --------->> SET UP <<------------
;     -----------------------------------------------
  
  
; Size of the  test rectangle
  RectWidth     =50
  RectHeight     =100
  
; Angle of the rotated polygon
  Angle#=45
  
; Arrays used for the DrawQuad routine
  Dim vertx#(4)
  Dim vertY#(4)
  
  
; create the random map from some circles
  Map,Level=Build_Random_Map(32,32)
  
  
; make a backdrop picture
  BackDrop=NewImage(800,600)
  RenderToImage Backdrop
  c1=RndRGB()
  c2=RndRGB()
  ShadeBox 0,0,800,600,c1,c1,c2,c2
  RenderToScreen
  
  
; create a camera to view the scene with
  Cam=NewCamera()
  CameraCls Cam,off
  
  
  
  
;     ------------------------------------------------
;      --------->> MAIN LOOP OF PROGRAM <<----------
;     ------------------------------------------------
  
  
  Do
     
   ; Get the camera current position
     CamX=GetCameraX(cam)
     CamY=GetCameraY(cam)
     
   ; Get the mouse position
     MX=MouseX()+CamX
     MY=MouseY()+CamY
     
   ; Check if the Left Mouse is pressed ?
     If LeftMouseButton()
        RectWidth=Mod(RectWidth+1,400)
     EndIf
     
   ; Check if the RIGHT Mouse is pressed ?
     If RightMouseButton()
        RectHeight=Mod(RectHeight+1,400)
     EndIf
     
   ; Check if the MIDDLE Mouse is pressed ?
     If MidMouseButton()
      ; if so, rotate the polygon angle
        Angle#=WrapAngle(angle#,0.25)
     EndIf
     
     
   ; tell PB to capture the following drawing commands
     CaptureToScene
     
   ; clear the scene buffer so it's empty
     ClsScene
     
     
     
     Select Method
         Case 0
             Method$="TriHitMap"
             
         Case 1
             Method$="TriHitMapPixels"
             
         Case 2
             Method$="QuadHitMap"
         Case 3
             Method$="QuadHitMapPixels"
     EndSelect
     Collision=DRawQuad(Map,Level,MX,MY,RectWidth,RectHeight,Angle#,Method)
     
     
     
     If Collision
        Colour=$ff0000
        Message$="Hit"
     Else
        Colour=$0000ff
        Message$="Missed"
     EndIf
     
     
   ; set the capture depth of the next item to a depth of
   ; 100 units
     CaptureDepth 100
     DrawImage BackDrop,CamX,CamY,false
     
     
   ; draw the map level
     CaptureDepth 10
     DrawMap map,Level,0,0
     
     
   ; draw the scene with this camera
     DrawCamera cam
     
     
   ; Check if the Space key was pressed
     If SpaceKey()
      ; if so, change the collision method
        Method=Mod(Method+1,4)
        FlushKeys
     EndIf
     
   ; check if the ENTER key was pressed
     If EnterKey()
      ; If so, toggle Debug mode on the map
        MapDebug Map,1-GetMapDebug(Map)
        FlushKeys
     EndIf
     
     
     
     
   ; check if the users wanted to move the camera
     If LeftKey()  Then     MoveCamera Cam,-2,0
     If RightKey() Then     MoveCamera Cam,2,0
     If UpKey()    Then     MoveCamera Cam,0,-2
     If DownKey()  Then     MoveCamera Cam,0,2
     
     SetCursor 0,0
     Ink $ffffff
     Print "Press Space To Change Methods"
     Print "Mouse Buttons To Change Size"
     Print "Method:"+Method$
     Print  Message$
     Sync
  Loop
  
  
;     -------------------------------------------------
;       ------>> Draw Polygon With Collision <<-------
;     -------------------------------------------------
  
  
Psub DRawQuad(ThisMap,ThisLevel,X,Y,Width,Height,Angle#,Method)
  
  
  vertx#(0)=(Width/-2.0)
  verty#(0)=(Height/-2.0)
  
  vertx#(1)=(Width/2.0)
  verty#(1)=(Height/-2.0)
  
  vertx#(2)=(Width/2.0)
  verty#(2)=(Height/2.0)
  
  vertx#(3)=(Width/-2.0)
  verty#(3)=(Height/2.0)
  
  ca#=Cos(angle#)
  sa#=Sin(angle#)
  
  For lp=0 To 3
     xpos#=     vertx#(lp)
     ypos#=     verty#(lp)
     vertx#(lp)=Floor(x+((ca#*xpos#)-(sa#*ypos#)))
     vertY#(lp)=Floor(y+((ca#*ypos#)+(sa#*xpos#)))
  Next
  
  
  
  
  Select Method
      Case 0
          Collision=TriangleHitMap(ThisMap,ThisLevel,vertx#(0),vertY#(0),vertx#(1),vertY#(1),vertx#(2),vertY#(2) )
          Collision+=TriangleHitMap(ThisMap,ThisLevel,vertx#(0),vertY#(0),vertx#(2),vertY#(2),vertx#(3),vertY#(3) )
          
      Case 1
          Collision=TriangleHitMapPixels(ThisMap,ThisLevel,vertx#(0),vertY#(0),vertx#(1),vertY#(1),vertx#(2),vertY#(2) )
          Collision+=TriangleHitMapPixels(ThisMap,ThisLevel,vertx#(0),vertY#(0),vertx#(2),vertY#(2),vertx#(3),vertY#(3) )
          
          
      Case 2
          Collision=QuadHitMap(ThisMap,ThisLevel,vertx#(0),vertY#(0),vertx#(1),vertY#(1),vertx#(2),vertY#(2),vertx#(3),vertY#(3) )
          
      Case 3
          Collision=QuadHitMapPixels(ThisMap,ThisLevel,vertx#(0),vertY#(0),vertx#(1),vertY#(1),vertx#(2),vertY#(2),vertx#(3),vertY#(3) )
  EndSelect
  
  
  If Collision
     col=$f06070
  Else
     Col=$5060f0
  EndIf
  
  
  Select Method
      Case 0,1
          TriC vertx#(0),vertY#(0),vertx#(1),vertY#(1),vertx#(2),vertY#(2),Col
          TriC vertx#(0),vertY#(0),vertx#(2),vertY#(2),vertx#(3),vertY#(3),Col
          
          
      Case 2,3
          QuadC vertx#(0),vertY#(0),vertx#(1),vertY#(1),vertx#(2),vertY#(2),vertx#(3),vertY#(3),Col
          
          
  EndSelect
  
  CaptureDepth 5
; draw the outline in lines
  Line vertx#(0),vertY#(0),vertx#(1),vertY#(1)
  Line vertx#(1),vertY#(1),vertx#(2),vertY#(2)
  Line vertx#(2),vertY#(2),vertx#(3),vertY#(3)
  Line vertx#(3),vertY#(3),vertx#(0),vertY#(0)
  
  If Method<2
     Line vertx#(0),vertY#(0),vertx#(2),vertY#(2)
  EndIf
  
EndPsub Collision
  
  
  
  
  
;     -------------------------------------------------
;      --------->> Build Random Map Scene <<---------
;     -------------------------------------------------
  
  
Function Build_Random_Map(BlockWidth,BlockHeight)
  
  BackdropColour=$008f00
  
  screen=NewFXImage(2400,1600)
  RenderToImage screen
  Cls  BackdropColour
  CircleC 800,400,400,true,$8f8f8f
  CircleC 1700,400,400,true,$0f8faf
  
  For lp=0 To 200
     x=Rnd(2400)
     y=Rnd(1600)
     rx=RndRange(50,150)
     ry=RndRange(10,50)
     EllipseC x,y,rx,ry,true, BackdropColour
  Next
  
  
  
  Map=NewMap(50)
  
; Create 1024 FX BLocks for this map
  CreateMapGFX  Map,BlockWidth,BlockHeight,1024,_
  BackdropColour,2
  
  BlocksX=GetSurfaceWidth()/BlockWidth
  BlocksY=GetSurfaceHeight()/BlockWidth
  
  Level=NewLevel(Map,BlocksX,BlocksY)
  LevelTransparent Map,Level,0
  GetMapBlk Map,Tile,0,0
  
  Tile=1
  For ylp=0 To GetLevelHeight(map,level)-1
     Ypos=ylp*BlockHeight
     If Ypos+BlockHeight<=GetSurfaceHeight()
        For xlp=0 To GetLevelWidth(map,level)-1
           Xpos=xlp*BlockWidth
           GetMapBlk Map,Tile,Xpos,ypos
           
           If GetMapBlockTransparent(Map,Tile)>-1
              PokeLevelTile Map,Level,xlp,ylp,tile
              tile++
           EndIf
           If Tile=>GetMapBlocks(Map)
              ExitFor ylp
           EndIf
        Next
     EndIf
  Next
  
  RenderToScreen
  
; Turn Debug Map for this map
  MapDebug Map,on
  
EndFunction Map,Level
  
  
  
  
 
Related Info: CircleHitMap | CircleHitMapPixels | EllipseHitMap | EllipseHitMapPixels | GetMapBlockTransparent | PointHitMapPixels | QuadHitMapPixels | RayHitMapPixels | TriangleHitMapPixels :
 


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