Jump to content

Rotating to face a point


darhal

Recommended Posts

Hi as the title said I want the picture to face the cursor here is my code

  
showCursor(true) 
  
angle = 0 
addEventHandler("onClientRender", root, 
    function() 
        --showCursor(true) 
        x, y, _, _, _= getCursorPosition ( ) 
        --local angle = math.deg (math.atan2 ((x), (y))) 
        angle = math.atan2(y - 100/2, x - 100/2) 
        angle = math.deg(angle) 
        if(angle < 0) then 
            angle = 360 - (-angle) 
        end 
        outputChatBox(angle) 
        dxDrawImage(358, 100, 100, 100, ":CUPvehicles/files/lock.png", 0, 0, 0, tocolor(255, 255, 255, 255), false) 
        dxDrawImage(140, 276, 100, 100, ":CUPvehicles/files/engine.png", 0, 0, 0, tocolor(255, 255, 255, 255), true) 
        dxDrawImage(537, 276, 100, 100, ":CUPvehicles/files/lights.png", 0, 0, 0, tocolor(255, 255, 255, 255), true) 
        dxDrawImage(358, 276, 100, 100, ":CUPvehicles/files/switch.png",  angle+90, 0, 0, tocolor(255, 255, 255, 255), true) 
    end 
) 
  

Link to comment
  
showCursor(true) 
  
  
angle = 0 
addEventHandler("onClientRender", root, 
    function() 
        --showCursor(true) 
        x, y, _, _, _= getCursorPosition ( ) 
        angle = findRotation(x, y, 358, 276) 
        outputChatBox(angle) 
        dxDrawImage(358, 100, 100, 100, ":CUPvehicles/files/lock.png", 0, 0, 0, tocolor(255, 255, 255, 255), false) 
        dxDrawImage(140, 276, 100, 100, ":CUPvehicles/files/engine.png", 0, 0, 0, tocolor(255, 255, 255, 255), true) 
        dxDrawImage(537, 276, 100, 100, ":CUPvehicles/files/lights.png", 0, 0, 0, tocolor(255, 255, 255, 255), true) 
        dxDrawImage(358, 276, 100, 100, ":CUPvehicles/files/switch.png",  angle, 0, 0, tocolor(255, 255, 255, 255), true) 
    end 
) 
  
  
function findRotation( x1, y1, x2, y2 )  
    local t = -math.deg( math.atan2( x2 - x1, y2 - y1 ) ) 
    return t < 0 and t + 360 or t 
end 
  

Dont even rotate !

Link to comment

Try this untested

function findPictureAngle(picX,picY) 
    local cursor = isCursorShowing () 
    if cursor then 
        local screenx, screeny, worldx, worldy, worldz = getCursorPosition() 
        local imageRot = math.atan2 (( worldx - screenx ), (worldy - screeny)) 
        local X = picX - screenx 
        local Y = picY - screeny 
        local Z = math.atan2(X,Y) 
        local angle = Z - imageRot 
        return math.deg(angle) 
    end  
    return false 
end 

Link to comment
  
showCursor(true) 
  
  
angle = 0 
addEventHandler("onClientRender", root, 
    function() 
        --showCursor(true) 
        x, y, _, _, _= getCursorPosition ( ) 
        angle = findRotation(x, y, 358, 276) 
        outputChatBox(angle) 
        dxDrawImage(358, 100, 100, 100, ":CUPvehicles/files/lock.png", 0, 0, 0, tocolor(255, 255, 255, 255), false) 
        dxDrawImage(140, 276, 100, 100, ":CUPvehicles/files/engine.png", 0, 0, 0, tocolor(255, 255, 255, 255), true) 
        dxDrawImage(537, 276, 100, 100, ":CUPvehicles/files/lights.png", 0, 0, 0, tocolor(255, 255, 255, 255), true) 
        dxDrawImage(358, 276, 100, 100, ":CUPvehicles/files/switch.png",  angle, 0, 0, tocolor(255, 255, 255, 255), true) 
    end 
) 
  
  
function findRotation( x1, y1, x2, y2 )  
    local t = -math.deg( math.atan2( x2 - x1, y2 - y1 ) ) 
    return t < 0 and t + 360 or t 
end 
  

Dont even rotate !

It because getCursorPosition return the position in relative (from 0 to 1).

Try this:

showCursor(true) 
  
local sx, sy = guiGetScreenSize() 
angle = 0 
addEventHandler("onClientRender", root, 
    function() 
        --showCursor(true) 
        local x, y = getCursorPosition() 
        angle = findRotation(x*sx, y*sy, 358, 276) 
        outputChatBox(angle) 
        dxDrawImage(358, 100, 100, 100, ":CUPvehicles/files/lock.png", 0, 0, 0, tocolor(255, 255, 255, 255), false) 
        dxDrawImage(140, 276, 100, 100, ":CUPvehicles/files/engine.png", 0, 0, 0, tocolor(255, 255, 255, 255), true) 
        dxDrawImage(537, 276, 100, 100, ":CUPvehicles/files/lights.png", 0, 0, 0, tocolor(255, 255, 255, 255), true) 
        dxDrawImage(358, 276, 100, 100, ":CUPvehicles/files/switch.png",  angle, 0, 0, tocolor(255, 255, 255, 255), true) 
    end 
) 
  
function findRotation( x1, y1, x2, y2 ) 
    local t = -math.deg( math.atan2( x2 - x1, y2 - y1 ) ) 
    return t < 0 and t + 360 or t 
end 

Link to comment
  
showCursor(true) 
  
  
angle = 0 
addEventHandler("onClientRender", root, 
    function() 
        --showCursor(true) 
        x, y, _, _, _= getCursorPosition ( ) 
        angle = findRotation(x, y, 358, 276) 
        outputChatBox(angle) 
        dxDrawImage(358, 100, 100, 100, ":CUPvehicles/files/lock.png", 0, 0, 0, tocolor(255, 255, 255, 255), false) 
        dxDrawImage(140, 276, 100, 100, ":CUPvehicles/files/engine.png", 0, 0, 0, tocolor(255, 255, 255, 255), true) 
        dxDrawImage(537, 276, 100, 100, ":CUPvehicles/files/lights.png", 0, 0, 0, tocolor(255, 255, 255, 255), true) 
        dxDrawImage(358, 276, 100, 100, ":CUPvehicles/files/switch.png",  angle, 0, 0, tocolor(255, 255, 255, 255), true) 
    end 
) 
  
  
function findRotation( x1, y1, x2, y2 )  
    local t = -math.deg( math.atan2( x2 - x1, y2 - y1 ) ) 
    return t < 0 and t + 360 or t 
end 
  

Dont even rotate !

It because getCursorPosition return the position in relative (from 0 to 1).

Try this:

showCursor(true) 
  
local sx, sy = guiGetScreenSize() 
angle = 0 
addEventHandler("onClientRender", root, 
    function() 
        --showCursor(true) 
        local x, y = getCursorPosition() 
        angle = findRotation(x*sx, y*sy, 358, 276) 
        outputChatBox(angle) 
        dxDrawImage(358, 100, 100, 100, ":CUPvehicles/files/lock.png", 0, 0, 0, tocolor(255, 255, 255, 255), false) 
        dxDrawImage(140, 276, 100, 100, ":CUPvehicles/files/engine.png", 0, 0, 0, tocolor(255, 255, 255, 255), true) 
        dxDrawImage(537, 276, 100, 100, ":CUPvehicles/files/lights.png", 0, 0, 0, tocolor(255, 255, 255, 255), true) 
        dxDrawImage(358, 276, 100, 100, ":CUPvehicles/files/switch.png",  angle, 0, 0, tocolor(255, 255, 255, 255), true) 
    end 
) 
  
function findRotation( x1, y1, x2, y2 ) 
    local t = -math.deg( math.atan2( x2 - x1, y2 - y1 ) ) 
    return t < 0 and t + 360 or t 
end 

WOW thx a lot TAPL :D

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...