-- Line Plane intersection test -- Init function called from K3dEngine during initialization -- Warning -- Don't delete this function -- Function must exist function Init() print ("___ intr_line_plane_script out = Init() function called") K3d_LoadMap("./maps/test/intr_line_plane_map.lua") -- Include script with global variables definitions K3d_IncludeScript("scripts/global_var_script.lua") InitScene() end -- Update function called from K3dEngine during update scene -- Warning -- Don't delete this function -- Function must exist function Update() UpdateScene() 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("___ intr_line_plane_script out = Delete() function called") end -- Line and plane objects g_iLine = -1 g_iPlane = -1 -- Ray for aiming test g_iRay = -1 -- Spheres for aiming test g_iSphereLine = -1 g_iSpherePlane = -1 -- Intersection point g_iIntrPoint = -1 -- Index of selected sphere g_iSelectedSphereId = -1 -- Camera object g_iCamera = -1 -- Init scene in scene initialization function InitScene() -- Your some scene initialization print("___ intr_line_plane_script out = InitScene() function called") g_iLine = K3d_GetObject("Line0", ObjType.iLineObj) print("___ intr_line_plane_script out = g_iLine =", g_iLine) if g_iLine == -1 then return false end K3d_Hide(g_iLine, ObjType.iLineObj, false); g_iPlane = K3d_GetObject("Plane0", ObjType.iPlaneObj) print("___ intr_line_plane_script out = g_iPlane =", g_iPlane) if g_iPlane == -1 then return false end K3d_Hide(g_iPlane, ObjType.iPlaneObj, false); g_iRay = K3d_GetObject("AimingRay", ObjType.iRayObj) print("___ intr_line_plane_script out = g_iRay =", g_iRay) if g_iRay == -1 then print("Ray doesn`t exists.") return false end g_iSphereLine = K3d_GetObject("LineBoundSphere", ObjType.iSphereObj) print("___ intr_line_plane_script out = g_iSphereLine =", g_iSphereLine) if g_iSphereLine == -1 then print("___ intr_line_plane_script out = LineBoundSphere doesn`t exists.") return false end g_iSpherePlane = K3d_GetObject("PlaneBoundSphere", ObjType.iSphereObj) print("___ intr_line_plane_script out = g_iSpherePlane =", g_iSpherePlane) if g_iSpherePlane == -1 then print("___ intr_line_plane_script out = g_iSpherePlane doesn`t exists.") return false end g_iCamera = K3d_GetObject("MainCamera", ObjType.iCameraObj) print("___ intr_line_plane_script out = g_iCamera =", g_iCamera) if g_iCamera == -1 then print("___ intr_line_plane_script out = g_iCamera doesn`t exists.") return false end CreateIntrPoint() -- Link plane to the sphere K3d_Link(g_iPlane, g_iSpherePlane, ObjType.iPlaneObj, ObjType.iSphereObj) -- Link line to the sphere K3d_Link(g_iLine, g_iSphereLine, ObjType.iLineObj, ObjType.iSphereObj) return true end -- Update scene in every frame function UpdateScene() -- Your some scene updates K3d_Print("K3dEngine 0.1") K3d_Print("Test intersection between line and plane") IntrLinePlane() IntrRaySphere() end -- Create default intersection point function CreateIntrPoint() local r=255 local g=0 local b=0 local size=5 g_iIntrPoint = K3d_AddVertex(0,0,0,r,g,b,size) end -- Move with plane function MovePlane() if g_iSelectedSphereId == g_iSpherePlane then -- Get distance between camera and sphere positions --print("function MovePlane()") local dist = K3d_GetDistance(g_iCamera,g_iSpherePlane, ObjType.iCameraObj, ObjType.iSphereObj) K3d_PrintValue("Distance between camera and sphere position = " , dist) K3d_MoveByMouse(g_iSpherePlane) -- Get sphere position local posX, posY, posZ = K3d_GetSpherePos(g_iSpherePlane) K3d_PrintValue("Plane sphere pos X = " , posX) K3d_PrintValue("Plane sphere pos Y = " , posY) K3d_PrintValue("Plane sphere pos Z = " , posZ) end if K3d_IsMouseRight() then K3d_Print("Mouse right button clicked") end end -- Move with line function MoveLine() if g_iSelectedSphereId == g_iSphereLine then -- Get distance between camera and sphere positions --print("function MoveLine()") local dist = K3d_GetDistance(g_iCamera,g_iSphereLine, ObjType.iCameraObj, ObjType.iSphereObj) K3d_PrintValue("Distance between camera and sphere position =" , dist) K3d_MoveByMouse(g_iSphereLine) -- Get sphere position local posX, posY, posZ = K3d_GetSpherePos(g_iSphereLine) K3d_PrintValue("Line sphere pos X = " , posX) K3d_PrintValue("Line sphere pos Y = " , posY) K3d_PrintValue("Line sphere pos Z = " , posZ) end end -- Check ray sphere intersection function IntrRaySphere() -- Get aiming ray local aimOrigX, aimOrigY, aimOrigZ, aimDirX, aimDirY, aimDirZ aimOrigX, aimOrigY, aimOrigZ, aimDirX, aimDirY, aimDirZ = K3d_GetAimRay() -- Set ray for ray sphere intersection K3d_SetRay(g_iRay, aimOrigX, aimOrigY, aimOrigZ, aimDirX, aimDirY, aimDirZ) -- Return true if intersect ray the sphere local bIsIntr1 = K3d_GetIntersection(g_iRay, g_iSpherePlane,ObjType.iRayObj, ObjType.iSphereObj, 0) if bIsIntr1 then K3d_Print("Ray intersect the sphere of plane") if g_iSelectedSphereId == -1 or g_iSelectedSphereId == g_iSphereLine then -- Set selected sphere index g_iSelectedSphereId = g_iSpherePlane K3d_SetColor(g_iSpherePlane, ObjType.iSphereObj, 255,0,0) K3d_SetColor(g_iSphereLine, ObjType.iSphereObj, 0,255,0) end end local bIsIntr2 = K3d_GetIntersection(g_iRay, g_iSphereLine,ObjType.iRayObj, ObjType.iSphereObj, 0) if bIsIntr2 then K3d_Print("Ray intersect the sphere of line") if g_iSelectedSphereId == -1 or g_iSelectedSphereId == g_iSpherePlane then -- Set selected sphere index g_iSelectedSphereId = g_iSphereLine K3d_SetColor(g_iSphereLine, ObjType.iSphereObj, 255,0,0) K3d_SetColor(g_iSpherePlane, ObjType.iSphereObj, 0,255,0) end end MovePlane() MoveLine() end -- Check line plane intersection and show intersection point function IntrLinePlane() local x,y,z = 0 -- Return if intersect line plane and x,y,z intersection point position bIsIntr, x, y, z = K3d_GetIntersection(g_iLine, g_iPlane,ObjType.iLineObj, ObjType.iPlaneObj, 1) if bIsIntr then K3d_Print("Line intersect the plane") -- Change intersection point position K3d_SetVertexPos(g_iIntrPoint, x,y,z) -- Unhide vertex K3d_Hide(g_iIntrPoint, ObjType.iVertexObj, false); K3d_PrintValue("Intr poin X = ", x) K3d_PrintValue("Intr poin Y = ", y) K3d_PrintValue("Intr poin Z = ", z) else -- Hide vertex K3d_Hide(g_iIntrPoint, ObjType.iVertexObj, true); end end