// note: this demo should work in AGk V1 and V2
// this example uses the mouse to position a sphere on a plane
// create a ground
CreateObjectPlane(1,500,500)
SetObjectRotation(1,90,0,0)
// create a sphere to position on the ground
CreateObjectSphere(2,20,16,16)
SetObjectColor(2,255,0,0,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,200,-500)
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# + 200
start_z# = unit_z# - 500
// 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# + 200
end_z# = 800*unit_z# - 500
// if the mouse in one the ground then position the sphere on the mouse
if ObjectRayCast(1,start_x#,start_y#,start_z#,end_x#,end_y#,end_z#) <> 0
SetObjectPosition(2,GetObjectRayCastX(0),GetObjectRayCastY(0),GetObjectRayCastZ(0))
endif
sync()
loop
Help make AGK better by submitting an example for this command! (All examples are subject to approval)