(AGK version: 108.24)
Submitted: 2014-12-19 18:18:08
// note: this demo should work in AGk V1 and V2

// pass the mouse over the sphere, the blue box shows the direction of the
// ray cast collision normal


// make a sphere (will automatically be placed at 0,0,0)
CreateObjectSphere(1,50,48,48)

// create a blue box that will show the direction of the ray cast normal
CreateObjectBox(2,1,1,20)
SetObjectColor(2,0,0,255,255)

// 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,0,0,-100)
SetCameraLookAt(1,0,0,0,0)

do

        // 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#
        start_y# = unit_y#
        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#
        end_y# = 800*unit_y#
        end_z# = 800*unit_z#-100


		// only ray cast against the sphere
        if ObjectRayCast(1,start_x#,start_y#,start_z#,end_x#,end_y#,end_z#) = 1

			// get the coordinates of the intersect
			intersect_x# = GetObjectRayCastX(0)
			intersect_y# = GetObjectRayCastY(0)
			intersect_z# = GetObjectRayCastZ(0)

			// get the ray cast normal
			normal_x# = GetObjectRayCastNormalX(0)
			normal_y# = GetObjectRayCastNormalY(0)
			normal_z# = GetObjectRayCastNormalZ(0)

			// position the blue box at the intersect point and orientate to point in the direction of the normal
			SetObjectPosition(2,intersect_x#,intersect_y#,intersect_z#)
			SetObjectLookAt(2,intersect_x#+normal_x#,intersect_y#+normal_y#,intersect_z#+normal_z#,0)

			// if the ray cast intersects the sphere then show the blue box
			SetObjectVisible(2,1)

		else

			// if the ray cast does not intersect the sphere then hide the blue box
			SetObjectVisible(2,0)

        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.