(AGK version: 108.21)
Submitted: 2014-12-14 09:47:18
// note: this demo should work in AGk V1 and V2

// the example demonstrates a 3D object picker
// hover the mouse over the spheres to turn them red

// create some spheres and position them randomly
for i = 1 to 10
	CreateObjectSphere(i,20,16,16)
	SetObjectPosition(i,random(0,200),0,random(0,200))
next i

// create a directional light
CreateLightDirectional(1,-1,-1,1,255,255,255)

// position the camera and have it look at the centre of the "map"
SetCameraPosition(1,100,200,-100)
SetCameraLookAt(1,100,0,100,0)

do

	// turn all the objects green
	for i = 1 to 10
		SetObjectColor(i,0,255,0,255)
	next i

	// get the position of the pointer (mouse)
	pointer_x = GetPointerX()
	pointer_y = GetPointerY()

	// get the x, y and z unit vectors based on the pointer position
	unit_x# = Get3DVectorXFromScreen(pointer_x,pointer_y)
	unit_y# = Get3DVectorYFromScreen(pointer_x,pointer_y)
	unit_z# = Get3DVectorZFromScreen(pointer_x,pointer_y)

	// calculate the start of the ray cast, which is the unit vector + the camera position
	start_x# = unit_x# + 100
	start_y# = unit_y# + 200
	start_z# = unit_z# - 100

	// calculate the end of the vector, which is the unit vector multiplied by the length of the ray cast and then add the camera position to it
	end_x# = 800*unit_x# + 100
	end_y# = 800*unit_y# + 200
	end_z# = 800*unit_z# - 100

	// determine which object has been hit
	object_hit = ObjectRayCast(0,start_x#,start_y#,start_z#,end_x#,end_y#,end_z#)

	// if an object has been hit then turn it red
	if object_hit <> 0
		SetObjectColor(object_hit,255,0,0,255)
	endif

	sync()

loop
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.