// this example should work in AGK V1 and AGK V2 // use the w and s key or the push up and down on the on screen joystick to move the two spheres back and forth // the cube will turn red if the raycast - from the green sphere to the red sphere - intersects it // create a box (it will automaticall be located at point 0,0,0) CreateObjectBox(1,20,20,20) // create a green sphere and position it CreateObjectSphere(2,10,12,12) SetObjectColor(2,0,255,0,255) SetObjectPosition(2,-50,0,50) // create a red sphere and position it CreateObjectSphere(3,10,12,12) SetObjectColor(3,255,0,0,255) SetObjectPosition(3,50,0,50) // create a direction to make the whole scene look better (go on, delete this line and you'll find out why I included it) CreateLightDirectional(1,-1,-1,-1,255,255,255) // position and orientate the camera SetCameraPosition(1,0,150,-60) SetCameraLookAt(1,0,0,0,0) // main loop do // get player input (multiply by -1 so sphere move in direction the joystick is pushed, once again delete it and see what happens) joystick_y# = GetJoystickY() * -1 // move the spheres based on input MoveObjectLocalZ(2, joystick_y#) MoveObjectLocalZ(3, joystick_y#) // ray cast from the green sphere to the blue sphere old_x# = GetObjectX(2) old_y# = GetObjectY(2) old_z# = GetObjectZ(2) new_x# = GetObjectX(3) new_y# = GetObjectY(3) new_z# = GetObjectZ(3) if ObjectRayCast(1,old_x#,old_y#,old_z# ,new_x#,new_y#,new_z#) = 1 SetObjectColor(1,255,0,0,255) else SetObjectColor(1,0,255,0,255) endif sync() loop
rem rem AGK Application 2017.05.15 rem 3D Shooter decal rem MR 16.05.2017 rem Landscape App SetDisplayAspect( 4.0/3.0 ) setdefaultwrapu(0) setdefaultwrapv(0) //------------------------------------------ Cam cameraID=1 SetCameraRange( cameraID, 1, 1000 ) SetCameraPosition( cameraID, 0,2,-10 ) SetCameraLookAt( cameraID, 0, 0, 5, 0 ) //------------------------------------------ Light SetSunActive(1) SetSunColor(255, 255, 255) SetSunDirection(0, -1, 1) //------------------------------------------ Ground iGround=CreateObjectBox( 100, 0.01, 100 ) SetObjectCollisionMode( iGround, 1 ) //<<<< SetObjectColor( iGround, 128,255,128,255 ) SetObjectLightMode(iGround,1) //------------------------------------------ Cube iBox=CreateObjectBox( 1, 1, 1 ) SetObjectPosition( iBox, 0, 0.5, 4 ) //+1 SetObjectCollisionMode( iBox, 1 ) //<<<< SetObjectColor( iBox, 255,255,255,255 ) SetObjectLightMode(iBox,1) RotateObjectLocalY( iBox, 45 ) //------------------------------------------ Image decal imgBlood=loadimage("Blood.png") //------------------------------------------ Scope imgScope=loadimage("Scope.png") sprScope=createsprite(imgScope) setspritesize(sprScope,4,4) //------------------------------------------ do cx#=GetCameraX(1) cy#=GetCameray(1) cz#=GetCameraz(1) mx#=getpointerx() my#=getpointery() sx#=Get3DVectorXFromScreen(mx#,my#)*1000.0 sy#=Get3DVectorYFromScreen(mx#,my#)*1000.0 sz#=Get3DVectorZFromScreen(mx#,my#)*1000.0 ObjectRayCast(0,cx#,cy#,cz#,sx#,sy#,sz#) n=GetObjectRayCastNumHits() print(n) for i=0 to n-1 obj = GetObjectRayCastHitID(i) x#=GetObjectRayCastx(i) y#=GetObjectRayCastY(i) z#=GetObjectRayCastz(i) nx#=GetObjectRayCastNormalX(i) ny#=GetObjectRayCastNormalY(i) nz#=GetObjectRayCastNormalZ(i) tx#=GetScreenXFrom3D(x#,y#,z#) ty#=GetScreenYFrom3D(x#,y#,z#) setspritepositionbyoffset(sprScope,tx#,ty#) next if getpointerpressed()=1 iBlood=createobjectplane(0.5,0.5) SetObjectLightMode(iBlood,1) SetObjectPosition(iBlood,x#+nx#*0.1,y#+ny#*0.1,z#+nz#*0.1) SetObjectLookAt(iBlood,x#+nx#*1.0,y#+ny#*1.0,z#+nz#*1.0,0) SetObjectTransparency( iBlood, 1 ) setobjectimage(iBlood,imgBlood,0) SetObjectCollisionMode( iBlood, 0 ) //<<<< endif Sync() loop end