Jump to content

GPS system


Jusonex

Recommended Posts

Hey guys,

I am scripting at the moment on a little navigation/GPS System for mta, but I've some problems.

First of all my script:

function findRotation(x1,y1,x2,y2) 
     local t = -math.deg(math.atan2(x2-x1,y2-y1)) 
     if t < 0 then t = t + 360 end 
     return t 
end 
  
local x, y, z = 0, 0, 0 
  
function showArrow(player, seat, jacked) 
    if ( player == getLocalPlayer() ) then 
        local px, py, pz = getElementPosition(player) 
        arrow = createObject(1318, px, py, pz) 
        setElementCollisionsEnabled(arrow, false)    
        attachElements(arrow, source, 0, 0, 2) 
         
        addEventHandler("onClientRender", getRootElement(), rotArrow) 
    end 
end 
addEventHandler("onClientVehicleEnter", getRootElement(), showArrow) 
function rotArrow() 
    local vehicle = getPedOccupiedVehicle(getLocalPlayer()) 
    local px, py, pz = getElementPosition(vehicle) 
    detachElements(arrow, vehicle) 
    local rot = findRotation(px, py, x, y) 
    attachElements(arrow, vehicle, 0, 0, 2, 0, 0, rot) 
    outputChatBox(rot) 
end  

I think the calculation in function findRotation is not right, because I always get coords between 220 and 250.

Where is the error?

Justus

EDIT: I saw a sa-mp video on youtube: http://www.youtube.com/watch?v=S4NhdeYyC8k

Link to comment
  • Moderators
function findRotation(x1,y1,x2,y2) 
    local X = math.abs( x2 - x1 ) 
    local Y = math.abs( y2 - y1 ) 
    Rotm = math.deg( math.atan2( Y , X ) ) 
    if ( x2 >= x1 ) and ( y2 > y1 ) then    -- north-east 
        Rotm = 90 - Rotm 
    elseif ( x2 <= x1 ) and ( y2 > y1 ) then    -- north-west 
        Rotm = 270 + Rotm 
    elseif ( x2 >= x1 ) and ( y2 <= y1 ) then   -- south-east 
        Rotm = 90 + Rotm 
    elseif ( x2 < x1 ) and ( y2 <= y1 ) then    -- south-west 
        Rotm = 270 - Rotm 
    end 
    return (360-Rotm) 
end 

Not tested ( it's an adaptation for your script )

Link to comment

Everything is fine in findRotation. Maybe it's always between 220 and 250 because you don't go to areas which need other angles to reach the destination.

Look at your rotArrow function. It gets the angle to the destination and uses it as rotation offset in attachElements. That means the rotation of arrow will be rot + rotation of the vehicle. You need it to be rot. It's better not to use any attachment functions, but to simply get vehicle coordinates, put the arrow above it every frame (if you want it to move smooth, you need to use onClientPreRender) and set its rotation to the angle which findRotation returns.

Link to comment
function findRotation(x1,y1,x2,y2) 
    local X = math.abs( x2 - x1 ) 
    local Y = math.abs( y2 - y1 ) 
    Rotm = math.deg( math.atan2( Y , X ) ) 
    if ( x2 >= x1 ) and ( y2 > y1 ) then    -- north-east 
        Rotm = 90 - Rotm 
    elseif ( x2 <= x1 ) and ( y2 > y1 ) then    -- north-west 
        Rotm = 270 + Rotm 
    elseif ( x2 >= x1 ) and ( y2 <= y1 ) then   -- south-east 
        Rotm = 90 + Rotm 
    elseif ( x2 < x1 ) and ( y2 <= y1 ) then    -- south-west 
        Rotm = 270 - Rotm 
    end 
    return (360-Rotm) 
end 
  
local x, y, z = 0, 0, 0 
  
function showArrow(player, seat, jacked) 
    if ( player == getLocalPlayer() ) then 
        local px, py, pz = getElementPosition(player) 
        arrow = createObject(1318, px, py, pz) 
        setElementCollisionsEnabled(arrow, false) 
         
        addEventHandler("onClientPreRender", getRootElement(), rotArrow) 
    end 
end 
addEventHandler("onClientVehicleEnter", getRootElement(), showArrow) 
function rotArrow() 
    local vehicle = getPedOccupiedVehicle(getLocalPlayer()) 
    local px, py, pz = getElementPosition(vehicle) 
    local rotX, rotY, rotZ = getElementRotation(vehicle) 
    local rot = findRotation(px, py, x, y) 
    setElementPosition(arrow, px, py, pz+2) 
    setElementRotation(arrow, 0, 90, rot+rotZ) 
end 

Ohh..you're right. I forgot to add the rotation of the vehicle. I changed this, but the direction of the arrow is still wrong.

Link to comment
  • Moderators

I fix it ( by some tests ), at the begining this function is for the ped rotation ( it's not the same as the object rotation ):

function findRotation(x1,y1,x2,y2) 
    local X = math.abs( x2 - x1 ) 
    local Y = math.abs( y2 - y1 ) 
    Rotm = math.deg( math.atan2( Y , X ) ) 
    if ( x2 >= x1 ) and ( y2 > y1 ) then    -- north-east 
        Rotm = 90 - Rotm 
    elseif ( x2 <= x1 ) and ( y2 > y1 ) then    -- north-west 
        Rotm = 270 + Rotm 
    elseif ( x2 >= x1 ) and ( y2 <= y1 ) then   -- south-east 
        Rotm = 90 + Rotm 
    elseif ( x2 < x1 ) and ( y2 <= y1 ) then    -- south-west 
        Rotm = 270 - Rotm 
    end 
    return (630-Rotm) 
end 
  
local x, y, z = 0, 0, 0 
  
function showArrow(player, seat, jacked) 
    if ( player == getLocalPlayer() ) then 
        local px, py, pz = getElementPosition(player) 
        arrow = createObject(1318, px, py, pz) 
        setElementCollisionsEnabled(arrow, false) 
        
        addEventHandler("onClientPreRender", getRootElement(), rotArrow) 
    end 
end 
addEventHandler("onClientVehicleEnter", getRootElement(), showArrow) 
function rotArrow() 
    local vehicle = getPedOccupiedVehicle(getLocalPlayer()) 
    local px, py, pz = getElementPosition(vehicle) 
    local rotX, rotY, rotZ = getElementRotation(vehicle) 
    local rot = findRotation(px, py, x, y) 
    setElementPosition(arrow, px, py, pz+2) 
    setElementRotation(arrow, 0, 90, rot) 
end 

And it works !

Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...