Jumping again

Blitz3D Forums/Blitz3D Programming/Jumping again

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Hey Ruz, i wrote this a wee while ago as an example. It works pretty well. It's a wee bit messy and uses linepicks. Only short ones tho. If you keep them small, they are pretty fast.

Spacebar to jump and left and right to turn, and up and down to move forward and back. The code basically linepicks down and check the length of the pick. If it return <0.95, then the player has hit the ground. The minimum distance the pick returns, must be roughly the same as the players entity radius.

Try it out.

;linepick example by RossC

Graphics3D 800,600
SetBuffer BackBuffer()


Global camera=CreateCamera()
Global light=CreateLight()
Global player=CreateCone()
EntityType player,1
EntityRadius player,1
EntityColor player,50,70,255
PositionEntity player,0,0.5,0

PositionEntity camera,0,2,-6

Global tex=CreateTexture(64,64,1+8)
SetBuffer TextureBuffer(tex)
For loop=0 To 3000
	Color Rnd(190,255),Rnd(190,255),Rnd(190,255)
	Rect Int(Rnd(0,63)),Int(Rnd(0,63)),1,1
Next
Color 255,255,255

SetBuffer BackBuffer()

Global plane=CreatePlane()
EntityType plane,2
EntityColor plane,200,100,120
EntityTexture plane,tex
EntityPickMode plane,2

Global cube=CreateCube()
PositionEntity cube,0,1,10
EntityPickMode cube,2
EntityType cube,2

Global cube1=CreateCube()
PositionEntity cube1,0,3,12
EntityPickMode cube1,2
EntityType cube1,2

Global cube2=CreateCube()
PositionEntity cube2,0,5,14
EntityPickMode cube2,2
EntityType cube2,2

Global cube3=CreateCube()
PositionEntity cube3,0,7,20
ScaleEntity cube3,4,0.2,4
EntityPickMode cube3,2
EntityType cube3,2


Global cube4=CreateCube()
PositionEntity cube4,-2,9,14
EntityPickMode cube4,2
EntityType cube4,2

Global cube5=CreateCube()
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