News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Problem with minus and add, causing collision check to fail completely!

Started by LemonWizard, January 01, 2012, 01:15:44 AM

Previous topic - Next topic

LemonWizard

Okay I have an issue.
The issue is this.

Before you read the rest it's simple.
I have collision working but, when I add the -1 and +1 operands to account for ONE SINGLE EXTRA PIXEL, the ENTIRE collision check fails.
AND stops player movement to the right side in the CURRENT engine. Continue reading below for full explanation.
I don't understand WHY , the -1 and +1 operands on the collision check are BREAKING the collision at all... It's confusing me! >.<
The reason is because I did the exact same thing before in my other program and it worked. It should work here to!

I'm using the same collision checking routine I used in one of my older engines, for my new sidescroller engine.

So far, there's an issue with tile collision, and I'm doing tile collision manually simply because, well I can.

The problem has to do with a difference between these two snippets.
THIS works:


if playerx2 >checkingx1
if playerx2 <checkingx2
if playery2 >checkingy1
if playery1 <checkingy2
moveright=false
else
moveright=true
endif
endif
endif
endif



and THIS does not work:

PlayBASIC Code: [Select]
if playerx2 >checkingx1-1
if playerx2 <checkingx2+1
if playery2 >checkingy1-1
if playery1 <checkingy2+1
moveright=false
else
moveright=true
endif
endif
endif
endif



I have no idea why but if anyone can figure it out that would be great.
My other project uses the same one pixel bias for checking collision and works just fine only it doesn't use tiles.
Please help!! >.<
What I want to do is check the collision between the player and a tile, minus one pixel from the tiles sides, and top/bottom that way the tile itself, doesn't allow the player to move INTO it. Rather. the player is directly against it.

Oh yes.. the old checking routines here did work in my OLD engine which is NOT the same as this engine at all in any way. But. It uses stored platforms and takes their x1, y1, x2, y2 value against the players x1, y1, x2, y2 values. Here is the collision checking routine for my OLD project.


PlayBASIC Code: [Select]
for temp=1 to index
X1=platforms(temp,1 )
Y1=platforms(temp,2 )
X2=platforms(temp,3 )
Y2=platforms(temp,4 )
Px=Object1X
Py=Object1Y
Px2=Object1Y2
Py2=Object1Y2

;reset values just in case
Px2=Px+32
Py2=Py+32

;check collisions

;if were on the first loop
;make the player fall by 1
;only if landed=false

if landed=false and temp=1
if bouncing=false
object1y=object1y+1
endif
endif

if bouncing=true and temp=1
object1y=object1y-1
bouncetime=bouncetime-1
endif



if bouncetime <=0 and temp=1
bouncing=false
bouncetime=0
endif




;check left collision (Player is here --> ||Colliding with LEFT wall of object)
if Px2 >X1-1
if Px2 <X1+1
if Py2 >Y1-1
If Py <Y2+1
moveright=false
else
moveright=true
endif
endif
endif
endif

;check right collisions (Colliding with RIGHT wall of object || <--Player is here)
if Px >X2-1
if Px <X2+1
if Py2 >Y1-1
If Py <Y2+1
moveleft=false
else
moveleft=true
endif
endif
endif
endif
;(1 pixel difference between player's Y2 position and the wall's Y1 position!)

; Player is here
; |
; |
; V
;check the top collisions (Colliding with TOP wall of object;; |--------| <==Wall
; |Platform|
; | |
; |--------|
if Px2 >X1-1
If Px <X2+1
if Py2 >Y1-5
If Py2 <Y1+1
checklanded=true ;check landed means we have to set landed to true
endif ;after we exit the for-temp loop
endif ;if landed=true the player's Y isn't -1
endif ;repeatedly!That's what makes the player fall
endif ; ( If landed=false then object1y=object1y-1)



;check the bottom collisions (Colliding with the TIP wall of object;;

if Px2 >X1
If Px <X2
if Py <Y2+5
if Py2 >Y2+1
falling=true
bouncing=false
bouncetime=0
endif
endif
endif
endif



boxc platforms(temp, 1), platforms(temp, 2), platforms(temp, 3), platforms(temp, 4), 0, rgb(255, 0, 0)

next temp




AND Here is the whole code below for the current engine.


PlayBASIC Code: [Select]
; PROJECT : Castle Crush
; AUTHOR : NA
; CREATED : 12/27/2011
; EDITED : 12/31/2011
; ---------------------------------------------------------------------
castle=loadnewfximage("castlecrush.png")
mode=loadnewfximage("mode.png")
HEIGHT=getimageheight(castle)
WIDTH=getimagewidth(castle)
save=loadnewfximage("save.png")


SCROLLX=0
SCROLLY=0
MAPWIDTH=roundup(WIDTH/16)
MAPHEIGHT=roundup(HEIGHT/16)
dim map(MAPWIDTH, MAPHEIGHT)

type solid_tile
x1
y1
x2
y2
endtype


dim solids(MAPWIDTH, MAPHEIGHT) as solid_tile

myimage=newfximage(640, 480)
MODE$="View"


rendertoimage castle

for tempx=1 to WIDTH/16
for tempy=1 to HEIGHT/16
ink rgb(255,255, 255)
box (tempx-1)*16, (tempy-1)*16, tempx*16, tempy*16, 0
next tempy
next tempx


charx=200
chary=0
falling=false


do

if enterkey()
falling=true
endif


;draw map


;render to the map image
rendertoscreen



rendertoimage castle

x=mousex()
y=mousey()

if SCROLLX=>16
amountx=roundup(SCROLLX/16)
else amountx=1
endif
;top side same thing as above
if SCROLLY=>16
amounty=roundup(SCROLLY/16)
else
amounty=1
endif





if charx=>16
checkcharx=roundup(charx/16)
else checkcharx=1
endif
;top side same thing as above
if chary=>16
checkchary=roundup(chary/16)
else
checkchary=1
endif






for tempx=amountx to 32 ;disable x scroll for this entirely for the right side
for tempy=amounty to 30+amounty

x=(tempx-1)*16
y=(tempy-1)*16

xdraw=x-SCROLLX
ydraw=y-SCROLLY
ink rgb(0, 255, 0)
box x, y, x+16, y+16, 0

;check solid tiles below character



;check player on tile

if map(tempx, tempy)=1 ;if the tile is solid, check against player collision


checkingx1=solids(tempx, tempy).x1
checkingy1=solids(tempx, tempy).y1
checkingx2=solids(tempx, tempy).x2
checkingy2=solids(tempx, tempy).y2



playerx1=charx
playery1=chary
playerx2=charx+16
playery2=chary+16

if playerx2>checkingx1 ;the players x coordinates align
if playerx1<=checkingx2

if playery2=>checkingy1 ;the player is ontop of the platform or past it
if playery1<=checkingy1 ;the players top edge is not past the top of the platform
landed=true
endif
endif
endif
endif



;check for left and right sides
;check left collision (Player is here --> ||Colliding with LEFT wall of object)


if playerx2 >checkingx1-1
if playerx2 <checkingx2+1
Login required to view complete source code





LemonWizard

Omg I solved it with this bunch:

PlayBASIC Code: [Select]
if map(tempx, tempy)=1       ;if the tile is solid, check against player collision         


checkingx1=solids(tempx, tempy).x1
checkingy1=solids(tempx, tempy).y1
checkingx2=solids(tempx, tempy).x2
checkingy2=solids(tempx, tempy).y2



playerx1=charx
playery1=chary
playerx2=charx+16
playery2=chary+16

if playerx2>checkingx1 ;the players x coordinates align
if playerx1<checkingx2
if playery1<checkingy1 ;the players top edge is not past the top of the platform
if playery2>checkingy1-1 ;the players bottom edge IS past the top of the platform-1 pixel
landed=true
endif
endif
endif
endif




;check for left and right sides
;check left collision (Player is here --> ||Colliding with LEFT wall of object)


if playerx2>checkingx1-1
if playerx2<checkingx1+1
if playery2>checkingy1
if playery1<checkingy2
moveright=false
else
moveright=true
endif
endif
endif
endif

;check right collision
if playerx1>checkingx2-1
if playerx1<checkingx2+1
if playery2>checkingy1
if playery1<checkingy2
moveleft=false
else
moveleft=true
endif
endif
endif
endif






endif