Main Menu

Walking on Sprite Collision Map

Started by kevin, January 20, 2010, 09:41:14 PM

Previous topic - Next topic

kevin

 Walking on Sprite Collision Map

  This example uses a set of  sprites for as a collision map.   The player (or character) runs pixel level collisions against this sprite set in order to determine what it's standing upon.   The collision map in the example is made up of basic primitives (triangles and boxes)..


PlayBASIC Code: [Select]
   setfps 60

// ---------------------------------------------------------------------
// ----------------->> CREATE COLLISION MAP TILES <<-------------------
// ---------------------------------------------------------------------

global TileWidth =64
global TileHeight =64


Cls 0
Tri 0,TileHeight,TileWidth,0,TileWidth,TileHeight
getimage 1,0,0,tilewidth,tileheight

Cls 0
box 0,0,tilewidth,tileheight,true
getimage 2,0,0,tilewidth,tileheight

Cls 0
Tri 0,0,TileWidth,TileHeight,0,TileHeight
getimage 3,0,0,tilewidth,tileheight

for lp=1 to 3
preparefximage lp
next





Dim CollisionMap(50,10)
Dim CollisionMapSprites(50,10)


for ylp=0 to 9
row$=Readdata$()
for xlp=1 to 50
ThisChr=mid(row$,Xlp)

select ThisChr
case ASC(".")
; Null (Transparent chr)
ThisImage=0
case ASC("/")
; triangle wedge /|
ThisImage=1
case ASC("\")
; triangle wedge |\
ThisImage=3

case ASC("-")
; Solid Block []
ThisImage=2
EndSelect

CollisionMap(xlp,ylp)=ThisImage
next
next

CreateSpriteSceneFromCollisionMap()



// --------------------------------------------------------------------------------
// --------------->> Init Player <<-------------------
// --------------------------------------------------------------------------------

CreateCamera 1

PlayerX#=100
PlayerY#=100

PLayerHeight=32

PlayerCollisionWidth=4
PlayerCollisionHeight=4

// Create a strip of pixels as the ground height detector
Cls $ffffffff
GetIMage 100,0,0,PlayerCollisionWidth,PlayerCollisionHeight
preparefximage 100


// Create Player sprite
PlayerSprite=NewSprite(PlayerX#,playerY#,100)

SpriteDrawmode PLayerSprite,2
SpriteCollisionMode PLayerSprite,6
SpriteHandle PLayerSprite, PlayerCollisionWidth/-2,0
#if playbasicversion>163
SpriteTint PlayerSprite ,$00ff0000
#endif



// ---------------------------------------------------------------------
// ----------------->> Main Loop <<-------------------
// ---------------------------------------------------------------------

Do


// init capture to scene mode
CaptureToScene

// clear the scene buffer of any old data
ClsScene





if leftkey() then playerx#=playerx#-4
if rightkey() then playerx#=playerx#+4

PositionSprite PLayerSprite,playerx#,playery#


// Check if Player hits the collision world ??
NewPLayerX#=playerx#
NewPLayerY#=playerY#

ThisSprite=SpriteHit(PLayerSprite,GetFirstSprite(),1)

Collision=false
While ThisSprite>0

Collision=true

// Move this sprite up by one pixel
MoveSprite PLayerSprite,0,-1

// Check to see it still's still overlapping
// if so, then check moving it up.
While SpritesOverLap(PlayerSprite,ThisSprite)
MoveSprite PLayerSprite,0,-1
EndWhile

// Get the players new position
NewPLayerX#=GetSpriteX(PLayerSprite)
NewPLayerY#=GetSpriteY(PLayerSprite)

// Continue Searching for hits
ThisSprite=GetNextSprite(ThisSprite)
if ThisSprite>0
ThisSprite=SpriteHit(PLayerSprite,ThisSprite,1)
endif
EndWhile
Login required to view complete source code



Related To:

    Platform Game Movement Based on Ray Intersection
   Walking On Sprites
   Platformer Pixel Collisions Via Ray Casting
   Advanced Platform Collision (Vector WORLD)