jumping and collision

Blitz3D Forums/Blitz3D Programming/jumping and collision

Nexinarus(Posted 2010) [#1]
hello. I am trying to do jumping and collision in my little engine here. I got the pxp scrolling and the camera. every attempt i did for jumping gave me errors. Not thinking i deleted the jumping stuff i did.
anyways how would i go about doing the code for jumping and collision? here is the source


;MAP ARRAY
Dim LEVELMAP(19,14)
;WINDOW RESOLUTION CONSTANTS
Const RESX=320,RESY=240,RESD=16,RESS=3
;SET GRAPHICS RESOLUTION[X,Y] COLORDEPTH[16,24,32-BIT],SCREENMODE[0=FULLSCREEN,1=WINDOW,2=SCALEDWINDOW]
Graphics RESX,RESY,RESD,RESS
;DOUBLE BUFFERING
SetBuffer BackBuffer()

;TILE GRAPHICS - SET TO BE USED THROUGHOUT THE WHOLE PROGRAM
Global TYLES

;RESTORE BUILT IN TEST MAP. WILL BE CHANGED AS TEST GROUNDS PROGRESS.
Restore BUILTINMAP
For Y=0 To 14
For X=0 To 19
Read LEVELMAP(X,Y)
Next
Next

;TILE SIZE [SQUARE=LENGTH & WIDTH ARE THE SAME]
Const TILESIZE=32
;TOTAL TILES SHOWN ON SCREEN HORIZONTAL[X] AND VERTICAL[Y]
Const MAXTILEX=160/TILESIZE
Const MAXTILEY=160/TILESIZE

;LOAD TILES
TYLES=CreateImage(32,32)
SetBuffer ImageBuffer(tyles)
Color 0,128,0
Rect 0,0,100,100
SetBuffer BackBuffer()

Global PLAYERX,PLAYERY

playerx=64
playery=96



;BEGIN LOOP
Repeat
	Cls
	UPDATESCREEN PLAYERX,PLAYERY
	Color 0,0,0
	Text 0,15, (PLAYERX-1)/32
	Text 15,0, (PLAYERY-1)/32
	Text 30,15, (PLAYERX)/32+1
	Text 15,30, (PLAYERY)/32+1
	Text 0,45,PLAYERX/32+","+PLAYERY/32
	Text 0,60,(PLAYERX+31)/32+","+(PLAYERY+31)/32
	Flip
	;LEFT
	If KeyDown(203) PLAYERX=PLAYERX-1*(PLAYERX>0)
	;RIGHT
	If KeyDown(205) PLAYERX=PLAYERX+1*(PLAYERX+32<640)
	;UP
	If KeyDown(200) PLAYERY=PLAYERY-1*(PLAYERY>0)
	;DOWN
	If KeyDown(208) PLAYERY=PLAYERY+1*(PLAYERY+32<480)
;END LOOP IF [ESC] IS PRESSED
Until KeyHit(1)

;FREE TILES FROM MEMORY
FreeImage TYLES TYLES=0
;QUIT GRAPHICS MODE
EndGraphics
;END PROGRAM
End

Function UPDATESCREEN(PROMPTX,PROMPTY)
	;CALCULATING CAMERA'S TOP-LEFT CORNER.
	CAMERAX=promptX-64
	CAMERAY=promptY-64
	;TOP LEFT CORNER
	If CAMERAX<0 Then CAMERAX=0
	If CAMERAY<0 Then CAMERAY=0
	;BOTTOM RIGHT CORNER
	If CAMERAY>15*TILESIZE-1-160 Then CAMERAY=15*TILESIZE-0-160
	If CAMERAX>20*TILESIZE-1-160 Then CAMERAX=20*TILESIZE-0-160
	;CLEAR SCREEN
	Cls
	;DRAW MAP
	For Y=CAMERAY/TILESIZE To CAMERAY/TILESIZE+MAXTILEY-(CAMERAY Mod 32=0)
		For X=CAMERAX/TILESIZE To CAMERAX/TILESIZE+MAXTILEX-(CAMERAX Mod 32=0)
			TILEINT=LEVELMAP(X,Y)-1
			If TILEINT>=0 Then DrawImage TYLES,X*32-CAMERAX+80,Y*32-CAMERAY+40,TILEINT
			;TILENUMBER (DELETE WHEN DONE)
			Color 0,128,128
			Text X*32-CAMERAX+1+80+8,Y*32-CAMERAY+1+40+10,(TILEINT+1)
		Next
	Next
	;PLAYER POSITION
	Color 0,0,255
	Rect promptX+80-CAMERAX,promptY+40-CAMERAY,32,32
	Color 0,0,0
	Text 10,10,CAMERAY+MAXTILEY*TILESIZE-(CAMERAY Mod 32=0)

	;screen boudaries.
	Color 255,255,255
	Rect 0,0,80,240
	Rect 240,0,80,240
	Rect 0,0,320,40
	Rect 0,200,320,40


End Function


;BUILTIN MAP TO BE LOADED
.BUILTINMAP
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;1
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;2
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;3
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1;4
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1;5
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1;6
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1;7
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1;8
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1;9
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;0
Data 1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1;11
Data 1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1;12
Data 1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1;13
Data 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1;14
    ;0 1 2 3 4 5 6 7 8 910111213141516171819


i hope someone could help me out.


Axel Wheeler(Posted 2010) [#2]
Well, I here's something that doesn't work right, but may give an idea how to proceed.

Note that as conceived your tunnel at x=18 will be difficult to enter, since the character has to line up perfectly in the x direction before it will allow entry.

Sorry I don't have time to work on this more now...

Replace the control lines with these:

;LEFT
	If KeyDown(203) 
		PLAYERX=PLAYERX-1*(PLAYERX>0)
		If LEVELMAP(PLAYERX/32,PLAYERY/32)<>0 Then PLAYERX=PLAYERX+1*(PLAYERX>0)
	EndIf
;RIGHT
	If KeyDown(205) 
		PLAYERX=PLAYERX+1*(PLAYERX+32<640)
		If LEVELMAP((PLAYERX)/32+1,PLAYERY/32)<>0 Or LEVELMAP(PLAYERX/32,PLAYERY/32+1)<>0 Or LEVELMAP(PLAYERX/32+1,PLAYERY/32+1)<> 0 Then PLAYERX=PLAYERX-1*(PLAYERX>0)
	EndIf
;UP
	If KeyDown(200) 
		PLAYERY=PLAYERY-1*(PLAYERY>0)
		If LEVELMAP(PLAYERX/32,PLAYERY/32)<>0 Then PLAYERY=PLAYERY+1*(PLAYERY>0)
	EndIf
;DOWN
	If KeyDown(208) 
		PLAYERY=PLAYERY+1*(PLAYERY+32<480)
		If LEVELMAP(PLAYERX/32,(PLAYERY-1)/32+1)<>0 Or LEVELMAP(PLAYERX/32+1,PLAYERY/32)<>0 Or LEVELMAP(PLAYERX/32+1,PLAYERY/32+1)<> 0 Then PLAYERY=PLAYERY-1*(PLAYERY>0)
	EndIf



Nexinarus(Posted 2010) [#3]
Thnx I wlll definitely give this a try. Ill see what happens and go from there.


Nexinarus(Posted 2010) [#4]
thnx for the help on collision Axel Wheeler. I used the basic code and did a few changes as well. here it is



i added PLYRSPEED. and a custom size character box. Im going to attempt jumping tomorrow.


Axel Wheeler(Posted 2010) [#5]
Cool. Good luck!


Nexinarus(Posted 2010) [#6]
well i did more testing with the player movement (code in previous post) when i tried using different speeds my player(box) did a stupid bouncing thing. Therefore i did some changes with the movement again. I had to do smoe changes in a few places. Just to make it simpler for when i wanted to change something. Oh ya i forgot i managed to make it so that i could change the camerasize too. that took some tweaking too. I never really got 2 work on the jumping yet since i found the collision prob. so here is all the code (again).



now the only thing i h8 is the loop for the speed outside of the movement keys.

What it is now



I tried it on the inside of each movement...

What i tried before to fix the problem


Then of course i tested it lots. and when i went down the 1 tile column, pressing down&right or starting on the right and pressing down&left, sometimes it would totally skip that column even when the players dimensions were at 32x32 when i tested it out like the code is now it seemed to work perfectly.

and b4 the
For CHECKSPEED=1 To MAXSPEED
stuff i did do the
playerx=playerx+plyrspeed
code but then i got the array out of bounds thing there too. (especially on playerspeed=3)

The reason why i am doing all these checks is that i want the movements as perfect as possible before i start on jumping. i also want to make different sizes of characters besides everything being 32x32 and lower. i'd like some to be 48x48. or even 64x64. That is also why i revised the code to let the user change the camerasize.

BTW i will definitely need lots of luck doing the jumping. If anyone has any ideas of how to do it. plz let me know. or even how to cleanup the movements a bit too. I would greatly appreciate it.