-- Init function called from K3dEngine during initialization -- Warning -- Don't delete this function -- Function must exist function Init() print("Loading map ./maps/test/intr_ray_sphere_map.lua") K3d_LoadMap("./maps/test/intr_ray_sphere_map.lua") InitScene() end -- Update function called from K3dEngine during update scene -- Warning -- Don't delete this function -- Function must exist function Update() IntrRaySphere() if K3d_CheckKeyboardKey("Esc") then -- Load new script and delete this script -- This function must be called as last function in this script -- else engine shut down in next funcion -- because next function doesn`t exist in new script K3d_LoadNewScript("./scripts/main_menu_script.lua") end end -- Delete function called from K3dEngine during delete scene -- Warning -- Don't delete this function -- Function must exist function Delete() print("Delete called") end -- Global variables -- Ray, sphere and camera object indexes g_iRay = -1 g_iSphere = -1 g_iCamera = -1 -- Draw aiming point g_iAimingPoint = -1 -- Init scene function InitScene() g_iRay = K3d_GetObject("AimingRay", ObjType.iRayObj) print("g_iRay =", g_iRay) if g_iRay == -1 then print("Ray doesn`t exists.") return false end g_iSphere = K3d_GetObject("BoundSphere", ObjType.iSphereObj) print("g_iSphere =", g_iSphere) if g_iSphere == -1 then print("BoundSphere doesn`t exists.") return false end g_iCamera = K3d_GetObject("MainCamera", ObjType.iCameraObj) print("g_iCamera =", g_iCamera) if g_iCamera == -1 then print("MainCamera doesn`t exists.") return false end CreateAimingPoint() return true end -- Create aiming point function CreateAimingPoint() local r=255 local g=0 local b=0 local size=5 g_iAimingPoint = K3d_AddVertex(0.0,0.0,0.0,r,g,b,size) end -- Check ray sphere intersection function IntrRaySphere() K3d_Print("K3dEngine 0.1") K3d_Print("Ray sphere intersection test") -- Get aiming ray local aimOrigX, aimOrigY, aimOrigZ, aimDirX, aimDirY, aimDirZ aimOrigX, aimOrigY, aimOrigZ, aimDirX, aimDirY, aimDirZ = K3d_GetAimRay() -- Set vertex position for visualize aiming point K3d_SetVertexPos(g_iAimingPoint, aimDirX, aimDirY, aimDirZ) -- Set ray for ray sphere intersection K3d_SetRay(g_iRay, aimOrigX, aimOrigY, aimOrigZ, aimDirX, aimDirY, aimDirZ) -- Return true if intersect ray the sphere local bIsIntr = K3d_GetIntersection(g_iRay, g_iSphere,ObjType.iRayObj, ObjType.iSphereObj, 0) if bIsIntr then K3d_Print("Ray intersect the sphere") K3d_SetColor(g_iSphere, ObjType.iSphereObj, 255,0,0) else K3d_SetColor(g_iSphere, ObjType.iSphereObj, 0,255,0) end end