CheckMapImpact
State = CheckMapImpact(MapIndex, LevelIndex, CurrentXpos, CurrentYpos, NewXpos, NewYpos, Width, Height)
 
Parameters:

    MapIndex = The Index of the map you wish to check a level within
    LevelIndex = The index of the level you with to check collision against
    CurrentXpos = The Current X (top left) position of this region
    CurrentYpos = The Current Y (top left) position of this region
    NewXpos = The new X position (topleft) where this region wants to move to
    NewYpos = The new Y position (top left) where this region wants to move to
    Width = The Width fo the region your checking for collision
    Height = The Height of the region your checking for collision
Returns:

    State = The collision state of this movement (0 = no collision, 1 = Collision)
 

      The CheckMapImpact function returns if a moving rectangular region has made impact (collides) with a level. If there was collision, the function returns a True (1) value, if not a False (0) value.

      If an impact does occur, CheckMapImpact will handle the repositioning/ sliding for you. The functions GetMapImpactX and GetMapImpactY will return the new position of the region after sliding

      To work correctly, CheckMapImpact requires you that to give it, both the position of your object before movement and the position where this object wishes to move to. As well as the width and height of the object.




FACTS:


      * CheckMapImpact Expects the transparent block in the level to be block 0

      * Your collision Region can't move longer distances then the levels blocks width/height

      * Worlds & Sprites can also be used for sliding collision. See CreateWorld, SpriteCollision




Mini Tutorial:


      This example builds map, and level. The primary focus of this example is the detection of collisions of moving region and the level.


  
; Create a map with provision for 5 levels
  MyMap=NewMap(1)
  
; Create some blank blocks for this map
  CreateMapGFX MyMap,25,25,2,RGB(0,0,0)
  BoxC 0,0,30,30,1,RGB(255,0,200)
  GetMapBlk MyMap,1,0,0
  
  
  width=25
  Height=20
  
; create the level
  MyLevel=NewLevel(Mymap, Width,Height)
  
  
; Fill the Level edges with blocks
  For Xlp=0 To Width
     PokeLevelTile Mymap,MyLevel,xlp,0,1
     PokeLevelTile Mymap,MyLevel,xlp,Height,1
  Next
  
  For Ylp=0 To Height
     PokeLevelTile Mymap,MyLevel,0,ylp,1
     PokeLevelTile Mymap,MyLevel,Width,ylp,1
  Next
  
; Draw a strip of blocks across the middle
; of the page
  For Xlp=3 To Width-3
     PokeLevelTile Mymap,MyLevel,xlp,Height/2,1
  Next
  
  
; Define some variables for a PLayer
  
  PlayerX=50
  PlayerY=50
  
  PlayerWidth     =20
  PlayerHeight     =20
  
  Speed=4
  
; Start a Do / Loop
  Do
   ; Clear the screen
     Cls RGB(0,0,0)
     
   ; Draw the map to the screen
     DrawMap MyMap,MyLevel,0,0
     
   ; Store the players possible new position
     PlayerNewX=PlayerX
     PlayerNewY=PlayerY
     
   ; Handle the players Movement
     If UpKey() Then PlayerNewY=PLayerY-Speed
     If DownKey() Then PlayerNewY=PLayerY+Speed
     If LeftKey() Then PlayerNewX=PLayerX-Speed
     If RightKey() Then PlayerNewX=PLayerX+Speed
     
   ; ==========================
   ; Handle Collision
   ; ===========================
     
     If CheckMapImpact(MyMap,MyLevel,PlayerX,playerY,PlayerNewX,PlayerNewY,PLayerWidth,PlayerHeight)
      ; If there was impact with the map, then read our
      ; new position back.
        PlayerX=GetMapImpactX()
        PlayerY=GetMapImpactY()
        
      ; Check what side the impact occured upon
      ; and display a message
        
        If GetMapImpactLeft()
           Print "Impact occured on the LEFT side"
        EndIf
        
        If GetMapImpactRight()
           Print "Impact occured on the RIGHT side"
        EndIf
        
        If GetMapImpactTop()
           Print "Impact occured on the TOP side"
        EndIf
        
        If GetMapImpactBot()
           Print "Impact occured on the Bottom side"
        EndIf
     Else
      ; No Collision, Then set the current players
      ; position to it's new position
        PlayerX=PlayerNewX
        PlayerY=PlayerNewY
     EndIf
     
     
   ; draw the player (box) to the  screen
     X2=playerX+PlayerWidth
     Y2=playerY+PlayerHeight
     Box playerX,playerY,x2,y2,1
     
   ; Display the screen
     Sync
   ; Loop back to the do statement
  Loop
  



This example would output.

  
  no Text Output
  

 
Related Info: GetMapImpactBot | GetMapImpactLeft | GetMapImpactRight | GetMapImpactTop | GetMapImpactX | GetMapImpactY :
 


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