(AGK version: 108.24)
Submitted: 2015-03-18 18:17:27
remstart

        this was written in AGK V2 alpha 9 but should work in V108+

	first person example showing collision with multiple objects

	wasd or on screen joystick to move and turn
	spacebar or on screen button to jump

remend


// setup
gosub _declare_variables
_f_create_player()
_f_create_level()

// main loop
do

	_f_player_input()
	_f_move_player()
	_f_hud()

	sync()

loop


_declare_variables:

	type _player_type

		// size of player
		radius# as float
		jump_speed# as float

		// states
		jump_state as integer

		// position
		xpos# as float
		ypos# as float
		zpos# as float
		xpos_old# as float
		ypos_old# as float
		zpos_old# as float

		//velocity
		yvel# as float

	endtype

	global player as _player_type

	type _joystick_type
		x# as float
		y# as float
	endtype

	global joystick as _joystick_type

	// map / level
	dim ground[100]
	dim sphere[100]
	dim column[100]
	global num_spheres as integer
	global num_columns as integer
	global gravity# as float

return


function _f_create_player()

	player.radius# = 2.5
	player.jump_speed# = 0.75

endfunction


function _f_create_level()

	// create a grid of 100 boxes, 10x10, to act as a the ground
	size = 30
	obj = 0
	for z = 0 to 9
		for x = 0 to 9
			inc obj
			ground[obj] = CreateObjectBox(size,10,size)
			SetObjectColor(ground[obj],150,150,random(150,255),255)
			SetObjectCollisionMode(ground[obj],1)
			SetObjectPosition(ground[obj],x*size,random(0,5)/3.0,z*size)
		next x
	next z

	// create some spheres and places them just above the ground
	obj = 0
	for z = 2 to 7 step 3
		for x = 2 to 7 step 3
			inc obj
			sphere[obj] = CreateObjectSphere(20,12,12)
			SetObjectColor(sphere[obj],255,255,0,255)
			SetObjectCollisionMode(sphere[obj],1)

			// postion the sphere just above the ground
			for i = 1 to 100
				if ObjectRayCast(ground[i], x*size,100,z*size, x*size,-100,z*size) <> 0
					SetObjectPosition(sphere[obj],x*size,getObjectRayCastY(0)+11,z*size)
				endif
			next i

		next x
	next z
	num_spheres = obj


	// create some columns (the assumption here is that they'll be too tall to jump on top of)
	obj = 0
	for z = 4 to 6 step 2
		for x = 4 to 6 step 2
			inc obj
			column[obj] = CreateObjectCylinder(100,20,9)
			SetObjectColor(column[obj],150,255,150,255)
			SetObjectCollisionMode(column[obj],1)
			SetObjectPosition(column[obj],x*size,50,z*size)
		next x
	next z
	num_columns = obj

	// set up environment
	CreateLightDirectional(1,0.5,-1,0,255,255,255)
	gravity# = -0.02

	// position player
	player.xpos# = 0.0
	player.ypos# = 50.0
	player.zpos# = 0.0
	player.jump_state = 0

	// position camera
	SetCameraPosition(1,player.xpos#,player.ypos#,player.zpos#)
	SetCameraRotation(1,0,0,0)

endfunction


function _f_player_input()

	// wasd to move and turn
	joystick.x# = GetJoystickX()
	joystick.y# = GetJoystickY()*-1

	// spacebar to jump
	if player.jump_state = 1
		if GetButtonPressed(1) = 1
			player.yvel# = player.jump_speed#
		endif
	endif


endfunction


function _f_move_player()
	// save player position
	player.xpos_old# = GetCameraX(1)
	player.ypos_old# = GetCameraY(1)
	player.zpos_old# = GetCameraZ(1)

	// rotate and move camera
	RotateCameraGlobalY(1,joystick.x#)
	MoveCameraLocalZ(1,joystick.y#)

	// get player coordinates
	player.xpos# = GetCameraX(1)
	player.zpos# = GetCameraZ(1)

	// determine player vertical velocity and position
	player.yvel# = player.yvel# + gravity#
	player.ypos# = player.ypos# + player.yvel#

	// set flag
	player.jump_state = 0

	// collision with spheres
	for i = 1 to num_spheres
		if ObjectSphereCast(sphere[i], player.xpos_old#,player.ypos_old#,player.zpos_old#, player.xpos#,player.ypos#,player.zpos#, player.radius#) <> 0
			player.xpos# = GetObjectRayCastSlideX(0)
			player.ypos# = GetObjectRayCastSlideY(0)
			player.zpos# = GetObjectRayCastSlideZ(0)
		endif
	next i

	// collision with columns
	for i = 1 to num_columns
		if ObjectSphereCast(column[i], player.xpos_old#,player.ypos_old#,player.zpos_old#, player.xpos#,player.ypos#,player.zpos#, player.radius#) <> 0
			//because the columns are too we don't need to worry about the Y axis
			player.xpos# = GetObjectRayCastSlideX(0)
			player.zpos# = GetObjectRayCastSlideZ(0)
		endif
	next i

	// collision of player with the ground
	for i = 1 to 100

		// the sphere cast straight down
		if ObjectSphereCast(ground[i], player.xpos#,player.ypos#+player.radius#,player.zpos#, player.xpos#,player.ypos#-100.0,player.zpos#, player.radius#-0.2) <> 0

			// determine where the ground is
			ground# = GetObjectRayCastY(0)

			// if the player's position is below the ground then place them on the ground, set their down velocity to zero and set flag so they can jump
			if player.ypos# < ground#
				player.ypos# = ground#
				player.yvel# = 0.0
				player.jump_state = 1
			endif

		endif

	next i

	// position the camera
	SetCameraPosition(1,player.xpos#,player.ypos#,player.zpos#)

endfunction


function _f_hud()

	print("wasd or on screen joystick to move and turn")
	print("spacebar or on screen button to jump")

endfunction
Help make AGK better by submitting an example for this command!
(All examples are subject to approval)
Login to post an example of your own.