Jayceon Posted April 24, 2017 Share Posted April 24, 2017 Hello. I started to create a real GPS with sound effects (like -> turn left, turn right, go x meters and turn left/right), but when i'm try to get the road angles the left/right turns fails. function createGPSRoute(x, y) local occupiedVehicle = getPedOccupiedVehicle(localPlayer) if occupiedVehicle then local vehicleX, vehicleY, vehicleZ = getElementPosition(occupiedVehicle) local _, _, vehicleRotation = getElementRotation(occupiedVehicle) gpsRoute = calculatePathByCoords(vehicleX, vehicleY, vehicleZ, x, y, 0) gpsWaypoints = {} if #gpsRoute ~= 0 then local possibleRoutes = {} for k, v in pairs(gpsRoute) do --[[ -- -- First attempt -- local nextNodeID = k + 1 local nextNode = gpsRoute[nextNodeID] if nextNode then table.insert(possibleRoutes, {k, nextNodeID}) end ]] local previousNodeID = k - 1 local previousNode = gpsRoute[previousNodeID] if previousNode then table.insert(possibleRoutes, {k, previousNodeID}) end end local turns = {} -- only the gps gui, turn left/right, go x meters.. local previousRoadAngle = 0 local previousRoadDirection = "" local forwardRoadCount = 0 local forwardRoadDistance = 750 for k, v in pairs(possibleRoutes) do local roadAngle = getAngle(gpsRoute[v[2]].x, gpsRoute[v[2]].y, gpsRoute[v[1]].x, gpsRoute[v[1]].y) local roadDirection = "N/A" local roadDistance = getDistanceBetweenPoints3D(vehicleX, vehicleY, vehicleZ, gpsRoute[v[1]].x, gpsRoute[v[1]].y, gpsRoute[v[1]].z) if roadAngle <= 360 and roadAngle >= 180 then roadDirection = "right" elseif roadAngle >= 0 and roadAngle <= 180 then roadDirection = "left" end if k == 1 then previousRoadAngle = roadAngle previousRoadDirection = roadDirection end local nextNode = possibleRoutes[k + 1] table.insert(gpsWaypoints, {k, roadDirection, roadDistance, roadAngle}) -- this table for render to visible nodes and for the minimap lines if nextNode then local isTurnRoad = false if previousRoadAngle ~= roadAngle or roadDirection ~= previousRoadDirection then local angleDifference = getAnglesDifference(previousRoadAngle, roadAngle) if angleDifference ~= 0 then if angleDifference <= -12 or angleDifference >= 12 then table.insert(turns, {k, roadDirection, roadDistance}) isTurnRoad = true end end end if not isTurnRoad then if roadDistance >= forwardRoadDistance then -- add every 750 meters isForwardRoad = true forwardRoadDistance = forwardRoadDistance + 750 table.insert(turns, {k, "forward", roadDistance}) end end previousRoadAngle = roadAngle previousRoadDirection = roadDirection end if k == #possibleRoutes then table.insert(turns, {"end", "end", roadDistance}) -- the last node, when reach it the gps destination switch off end end outputConsole(inspect(turns)) -- print the possible turns/directions. format: {table id, direction (left/right/forward), distance between) end end end function getAnglesDifference(angle1, angle2) angle1 = angle1 % 360 angle2 = angle2 % 360 local difference = (angle1 - angle2) % 360 if difference <= 180 then return difference else return -(360 - difference) end end function getAngle(x1, y1, x2, y2) local angle = -math.deg(math.atan2(x2 - x1, y2 - y1)) if angle < 0 then angle = angle + 360 end return angle end --[[ -- I tried the next function, but the same problem function getAngle(x1, y1, x2, y2) return (360 - math.deg(math.atan2((x2 - x1), (y2 - y1)))) % 360 end]] Thanks the replies. Link to comment
3aGl3 Posted April 24, 2017 Share Posted April 24, 2017 Hm, my getAngle function works with y, then x: function getAngle( x1, y1, x2, y2 ) local x = x2 - x1 local y = y2 - y1 return math.deg( math.atan( y, x ) ) end Link to comment
Jayceon Posted April 24, 2017 Author Share Posted April 24, 2017 I tried it, but the same problem. Link to comment
Gordon_G Posted April 25, 2017 Share Posted April 25, 2017 https://wiki.multitheftauto.com/wiki/FindRotation Link to comment
Jayceon Posted April 25, 2017 Author Share Posted April 25, 2017 Thanks your answer, but this function is same problem. I now try to add previous road angle to every next road angle and then calculate it. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now