I think I am getting the hang of this....

Blitz3D Forums/Blitz3D Beginners Area/I think I am getting the hang of this....

po(Posted 2004) [#1]
I am trying to do a small jumping test program. I think I got it down except for 1 thing.

(The blut squares are jumping pads and use left and right to move and space to jump)

Graphics 800,600

Global man_x#,man_y#,jump#,man_speed#,j_speed#,j_num#,j_dir#

SetBuffer BackBuffer()

SeedRnd MilliSecs()

man_x=395
man_y=400
j_speed#=3
jump=0
j_dir=-1
j_num=-0.06
man_speed#=3.0
tramp=0
level=400

While Not KeyHit(1)

Color 255,0,255
Oval man_x,man_y,10,10,1

Color 0,255,0
Line 0,410,800,410

Color 255,0,0
Rect 100,340,300,10,1

Color 55,155,255
Rect 600,400,10,10,1
Rect 200,400,10,10,1

If KeyDown(203) :man_x=man_x-1*man_speed:EndIf 
If KeyDown(205) :run=1:man_x=man_x+1*man_speed:EndIf 

If man_speed>5 Then man_speed=5

If man_x<0 Then man_x=799
If man_x>799 Then man_x=0

If RectsOverlap(man_x,man_y,10,10,600,400,10,10) Then jump=1
If RectsOverlap(man_x,man_y,10,10,200,400,10,10) Then jump=1

If RectsOverlap(man_x,man_y,10,10,100,340,300,10) Then 
	level=331
Else
	level=400
EndIf 

If KeyHit(57) Then jump=1

If jump=1 Then
	
	j_speed#=j_speed#+j_num
	man_y=man_y+j_dir*j_speed#
	
	If man_speed<0 Then 
		j_dir=1
		j_num=0.06
	EndIf
	
	If man_y>level Then 
		jump=0
		man_y=level
		j_dir=-1
		j_speed#=3
	EndIf 

EndIf 

Text 10,10,j_speed
Text 10,20,man_y
Text 10,30,man_speed

Flip
Cls

Wend


The problem is when you try to go off the platform without jumping, you don't fall. I have tried to correct this but with no luck.


Ross C(Posted 2004) [#2]
Try doing a linepick downwards, a short distance. If the picked distance is greater than the player collision sphere, he's not touching the ground, so apply gravity to him :)

This is a linepick example.

USE arrow keys to move, and spacebar to jump. Line pick used to see if the cone is still 'jumping'

PositionEntity cube5,-4,11,12
EntityPickMode cube5,2
EntityType cube5,2

Global cube6=CreateCube()
PositionEntity cube6,-6,13,10
EntityPickMode cube6,2
EntityType cube6,2

Global cube7=CreateCube()
PositionEntity cube7,0,13,20
ScaleEntity cube7,4,0.2,4
EntityPickMode cube7,2
EntityType cube7,2

Global sphere=CreateSphere(10)
ScaleEntity sphere,10,1,10
EntityPickMode sphere,2
EntityType sphere,2
PositionEntity sphere,-2,0,19

Collisions 1,2,2,2; set up collisions between the ground and the player

Global jump_force#
Global jump_flag=0; set jump flag to 'no jump'
Global gravity#=0.9; set gravity value

While Not KeyHit(1)

	
	LinePick(EntityX(player,True),EntityY(player,True),EntityZ(player,True),0,-30,0); do a linepick straight down form the players location
	
	If jump_flag=0 Then
		If EntityY#(player,True)-PickedY()>1.05 Then; WARNING. THIS NUMBER MUST BE SLIGHTLY LARGER THAN THE PLAYERS ENTITYRADIUS.
		; ABOVE. if the players y position is greater than a distance of 1.05, then make the player fall.
		; Since the players entity radius is 1, This number must be slightly greater than 1, so that the collision
		; system doesn't work.
			jump_flag=1; set jump flag to jump
			jump_force=0.02; set jump force to 0.02. You cannot set this to 0. It must always be slightly more
		End If
	End If


	If KeyHit(57) And jump_flag=0 Then; if user presses spacebar
		jump_flag=1; set jump flag to 'jump'
		jump_force=0.4; set jumping force. make larger for higher jump
	End If
		
	
	If KeyDown(200) Then MoveEntity player,0,0,0.1
	If KeyDown(208) Then MoveEntity player,0,0,-0.1
	If KeyDown(203) Then TurnEntity player,0,1,0
	If KeyDown(205) Then TurnEntity player,0,-1,0	
	
	If jump_flag=1 Then; if jump flag set then 'jump'
		update_jump()
	End If
	update_camera()
	
	UpdateWorld
	RenderWorld
	Text 0,0," Press Space to jump. Arrow keys to move!"
	;DebugLog(" pickedy="+PickedY())
	Flip
Wend
End

Function update_camera()
	RotateEntity camera,EntityPitch(player),EntityYaw(player),EntityRoll(player); make the camera equal to the rotations of the camera
	PositionEntity camera,EntityX(player),EntityY(player)+2,EntityZ(player); position the camera at the x and z pos of the player
	MoveEntity camera,0,0,-7; move the camera back from the player by 7 units
	PointEntity camera,player
End Function

Function update_jump()

	jump_force=jump_force*gravity; apply gravity to the jumping force

	MoveEntity player,0,jump_force,0; move the player
	If jump_force>0 And jump_force<0.05 Then; when players upward speed reaches alomost zero, reverse it
											; make smaller to keep player in the air for longer
		jump_force=jump_force*-1; reverse the jumping force
		gravity=gravity+(2*(1-gravity)); turn gravity around
	End If
	If jump_force<-0.5 Then jump_force=-0.5; stop the player from falling to quick
	If EntityCollided(player,2) Then; if the player has collided with the ground
		If EntityY(player,True)-PickedY()<=1 Then; WARNING. THIS NUMBER MUST BE THE SAME AS THE PLAYER'S ENTITYRADIUS
			jump_force=0; reset the jumping force
			jump_flag=0; reset jump flag back to zero (not jumping)
			gravity=gravity-(2*(gravity-1)); reset gravity
		End If
	End If
End Function



Pineapple(Posted 2004) [#3]
You could also take a peek at my ickle platform demo... includes source!!! :)

http://mysite.freeserve.com/dabz_site/Platform.zip

Dabz

:)


GfK(Posted 2004) [#4]
Don't think linepick works in 2D, Ross. :P


Ross C(Posted 2004) [#5]
DOH!! Sorry man, hehe :D


eBusiness(Posted 2004) [#6]
Think you are in a sort of deadend, try reprogramming the jump/graw system, more flexibility is needed for a complete game, but keep it up, you do good.