News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

How accurate is CheckMapImpact()?

Started by eatfishy, June 01, 2008, 11:14:06 AM

Previous topic - Next topic

eatfishy

I'm trying to move the sprite across a map tile with collision. I have created 2 map: one is the background in the game and one is for collision. Sometimes the CHECKMAPIMPACT works, where it'll detect collision, but not always. Can someone please help?!?!?!?!?! Thanks


Below is the code I have:

; PROJECT : Map Collision
; AUTHOR  : Mike Guan
; CREATED : 5/31/2008
; EDITED  : 6/1/2008
; ---------------------------------------------------------------------
TITLESCREEN "Multiplayer Online RPG in progress by Mike G."
OPENSCREEN 800,600,16,0
SETFPS 60

CONSTANT tileWidth=64
CONSTANT tileHeight=64

GLOBAL arrowKey=0;0=none,1=left,2=down,3=right,4=up
GLOBAL checkLevelTileX
GLOBAL checkLevelTileY
GLOBAL checkPlayerTileX
GLOBAL checkPlayerTileY


CREATEMAP 1,1;background map
CREATEMAP 2,1;collision map

REM main map
; LOADMAPGFX "bg.bmp",1,64,64,RGB(0,0,0) There is a bug with LOADMAPGFX
LOADIMAGE "bg.bmp",1
MAKEMAPGFX 1,1,64,64,5,RGB(255,0,255)
CREATELEVEL 1,1,19,19
FOR y=0 TO GETLEVELHEIGHT(1,1)
FOR x=0 TO GETLEVELWIDTH(1,1)
POKELEVELTILE 1,1,x,y,READDATA();Block Index starts from 0
NEXT x
NEXT y


REM collision map
LOADIMAGE "tileCollision.bmp",2
MAKEMAPGFX 2,2,tileWidth,tileHeight,2,RGB(255,0,255)
CREATELEVEL 2,0,19,8
FOR j=0 TO GETLEVELHEIGHT(2,0)
FOR i=0 TO GETLEVELWIDTH(2,0)
POKELEVELTILE 2,0,i,j,READDATA()
   NEXT i
NEXT j


;Create Camera
CREATECAMERA 1
`CAMERAVIEWPORT 1,0,0,800,600
`CAMERACLS 1,ON
CAPTURETOSCENE

DIM camX AS FLOAT
DIM camY AS FLOAT
POSITIONCAMERA 1,camX,camY

 
LOADIMAGE "char1.bmp",1
CREATESPRITE 1
SPRITEIMAGE 1,1

TYPE person
name$
spr
posX#
posY#
newPosX#
newPosY#
speed#
ENDTYPE

DIM player1 AS person
player1.name$="Mike"
player1.spr=1
player1.posX#=400
player1.posY#=400
player1.newPosX#=player1.posX#
player1.newPosY#=player1.posY#
player1.speed#=5
IMAGEMASKCOLOUR player1.spr,RGB(255,0,255)


TYPE world
posX#
posY#
speed#
tileIndex
ENDTYPE


DIM level1 AS world
level1.posX#=player1.posX#
level1.posY#=player1.posY#
level1.speed#=player1.speed#
level1.tileIndex=0


DO
  collision()
graphicDisplay()
keyBoardControl()
  playerInfo()
  debug()
  SYNC
LOOP
 
FUNCTION graphicDisplay()

`DRAWMAP 1,1,0,0
DRAWMAP 2,0,0,0
  DRAWCAMERA 1
player1.newPosX#=player1.posX#
  player1.newPosY#=player1.posY#
  POSITIONSPRITE player1.spr,player1.posX#,player1.posY#
  DRAWSPRITE player1.spr
 
ENDFUNCTION


FUNCTION debug()
TEXT 400,470,"Arrow Key: "+STR$(arrowKey)
TEXT 400,480,"CheckLvlTile X: "+STR$(checkLeveltileX)
TEXT 400,490,"CheckLvlTile Y: "+STR$(checkLeveltileY)
TEXT 400,500,"CheckPlytileX: "+STR$(checkPlayerTileX)
TEXT 400,510,"CheckPlyTileY: "+STR$(checkPlayerTileX)

TEXT 600,480,"Player X: "+STR$(player1.posX#)
TEXT 600,490,"Player Y: "+STR$(player1.posY#)
TEXT 600,500,"Tile Index: "+STR$(level1.tileIndex)
TEXT 600,510,"Level Y: "+STR$(level1.posY#)
TEXT 600,520,"getmapimpactX: "+STR$(GETMAPIMPACTX())
TEXT 600,530,"getmapimpactY: "+STR$(GETMAPIMPACTY())
TEXT 600,540,"Level X "+STR$(level1.posX#)
ENDFUNCTION
 
FUNCTION playerInfo()
TEXT 10,10,"Name: " +player1.name$
ENDFUNCTION
 
 
FUNCTION keyBoardControl()

IF UPKEY()=1
arrowKey=4
IF GETSPRITEY(player1.spr)>0 THEN level1.posY#=level1.posY-level1.speed#
player1.newPosY#=player1.posY#-player1.speed#
ENDIF

IF DOWNKEY()=1
arrowKey=2
IF GETSPRITEY(player1.spr)<600 THEN level1.posY#=level1.posY+level1.speed#
player1.newPosY#=player1.posY#+player1.speed#
ENDIF

IF LEFTKEY()=1
arrowKey=1
IF level1.posX#>0 THEN level1.posX#=level1.posX-level1.speed#
IF GETSPRITEX(player1.spr)>100
player1.newposX#=player1.posX#-player1.speed#
ELSE
IF level1.posX#>0 THEN MOVECAMERA 1,-player1.speed#,0
ENDIF
ENDIF

IF RIGHTKEY()=1
arrowKey=3
IF level1.posX#<2000 THEN level1.posX#=level1.posX+level1.speed#
IF GETSPRITEX(player1.spr)<650
player1.newposX#=player1.posX#+player1.speed#
ELSE
IF level1.posX#<2000 THEN MOVECAMERA 1,player1.speed#,0
ENDIF
ENDIF



ENDFUNCTION

FUNCTION collision()
REM determine which side of the sprite to check collision against tile
SELECT arrowKey
CASE 1`sprite walking left
checkLevelTileX=(((level1.posX#-(tileHeight/2))-1)/tileHeight)
CASE 2`sprite walking down
checkLevelTileY=(((player1.posY#+(tileWidth/2))+1)/tileHeight)
CASE 3`sprite walking right
checkLevelTileX=(((level1.posX#+(tileWidth/2))+1)/tileWidth)
CASE 4`sprite walking up
checkLevelTileY=(((player1.posY#-(tileHeight/2))-1)/tileHeight)
DEFAULT
ENDSELECT

REM read level tile index from makeMapGFX
level1.tileIndex= PEEKLEVELTILE(2,0,checkLevelTileX,checkLevelTileY)

IF CHECKMAPIMPACT(2,0,player1.posX#,player1.posY#,player1.newPosX#,player1.newPosY#,tileWidth,tileHeight) AND level1.tileIndex=1
player1.posX#=GETMAPIMPACTX()
player1.posY#=GETMAPIMPACTY()
ELSE
player1.posX#=player1.newPosX#
player1.posY#=player1.newPosY#
ENDIF


ENDFUNCTION


REM bg tiles
DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
DATA 0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,1,1
DATA 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1
DATA 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1
DATA 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1

eatfishy

Nevermind again. I decided not to use CheckMapImpact.

kevin


There's a few issues in it, but can you post the media so we can actually run it.