Jump to content

problem with OnCursorMove function


Gothem

Recommended Posts

well im creating a object placement with the cursor.

but the problem is...

than the worldX , worldY and worldZ parameters of OnCursorMove dont are totally correct.

so i have a problem.

i tried something for fix it but now is a lower error.

here is the code if someone know how fix it.

function MapaC() 
    if MComandante == true then 
        MComandante = false 
        showCursor ( false ) 
        toggleCameraFixedMode ( false ) 
        showPlayerHudComponent ("radar", true ) 
        unbindKey ( "arrow_u", "down", MovCam ) 
        unbindKey ( "arrow_d", "down", MovCam ) 
        unbindKey ( "arrow_l", "down", MovCam ) 
        unbindKey ( "arrow_r", "down", MovCam ) 
    else  
        MComandante = true 
        toggleCameraFixedMode ( true ) 
        x,y,z = getElementPosition ( getLocalPlayer() ) 
        setTimer(setCameraPosition,200,1, x, y, 75) 
        setTimer(setCameraLookAt,500,1, x, y, z) 
        objeto = createObject ( 3434, x, y, z) 
        showCursor(true) 
        showPlayerHudComponent ("radar", false ) 
        bindKey ( "arrow_u", "down", MovCam ) 
        bindKey ( "arrow_d", "down", MovCam ) 
        bindKey ( "arrow_l", "down", MovCam ) 
        bindKey ( "arrow_r", "down", MovCam ) 
    end 
end 
  
function MovCam ( key ) 
    if key == "arrow_u" then 
        x,y,z = getCameraPosition () 
        setCameraPosition( x, y+1, z) 
        setTimer(setCameraLookAt,250,1, x, y+1, getGroundPosition(x,y,z)) 
    elseif key == "arrow_d" then 
        x,y,z = getCameraPosition () 
        setCameraPosition( x, y-1, z) 
        setTimer(setCameraLookAt,250,1, x, y-1, getGroundPosition(x,y,z)) 
    elseif key == "arrow_l" then 
        x,y,z = getCameraPosition () 
        setCameraPosition( x-1, y, z) 
        setTimer(setCameraLookAt,250,1, x-1, y, getGroundPosition(x,y,z)) 
    elseif key == "arrow_r" then 
        x,y,z = getCameraPosition () 
        setCameraPosition( x+1, y, z) 
        setTimer(setCameraLookAt,250,1, x+1, y, getGroundPosition(x,y,z)) 
    end 
end 
  
function Construccion(cursorX, cursorY, absoluteX, absoluteY, worldX, worldY, worldZ) 
    if MComandante == true then 
        Z = getGroundPosition(worldX,worldY,worldZ) + getElementDistanceFromCentreOfMassToBaseOfModel(objeto) * 2 
        outputChatBox ( "Variables: X="..worldX.."Y="..worldY.."Z="..Z ) 
        setElementPosition ( objeto, worldX, worldY, Z) 
    end 
end 
  
addEventHandler("onClientCursorMove",getRootElement(),Construccion) 

if someone know how fix it thx

PD: you need to add

MapaC() 

in a command or something :wink:

Link to comment

I've simplified things a bit by taking the part that should interest you the most. It's easier to read now :lol: .

  
local selectedElement = false 
local screenX, screenY = guiGetScreenSize() 
  
function onClientCursorMove(cursorX, cursorY) 
  local worldX, worldY, worldZ = 0, 0, 0 
  local rotX, rotY, rotZ = 0, 0, 0 
  local depth = 0 
  local distance = 0 
  cursorX = screenX * (cursorX / 1) 
  cursorY = screenY * (cursorY / 1) 
  depth = (math.abs(cursorY - screenY) / screenY) * 100 
  depth = 50.0 * ((depth / 100) * (depth / 100)) 
  worldX, worldY, worldZ = getWorldFromScreenPosition(cursorX, cursorY, depth) 
  collision, pX, pY, pZ, element = processLineOfSight(worldX, worldY, worldZ + 5.0, worldX, worldY, -100, true, true, true, true, false, true, true, true, nil) 
  if (selectedElement) then 
    distance = getElementDistanceFromCentreOfMassToBaseOfModel(selectedElement) 
    if (collision and getKeyState("lshift")) then 
      worldZ = pZ + distance 
    end 
    if (getElementType(selectedElement) == "vehicle") then 
      setVehicleFrozen(selectedElement, true) 
    end 
    setElementPosition(selectedElement, worldX, worldY, worldZ) 
  end 
end 
  
addEventHandler("onClientCursorMove", getRootElement(), onClientCursorMove, true) 
  

Link to comment
I've simplified things a bit by taking the part that should interest you the most. It's easier to read now :lol: .
  
local selectedElement = false 
local screenX, screenY = guiGetScreenSize() 
  
function onClientCursorMove(cursorX, cursorY) 
  local worldX, worldY, worldZ = 0, 0, 0 
  local rotX, rotY, rotZ = 0, 0, 0 
  local depth = 0 
  local distance = 0 
  cursorX = screenX * (cursorX / 1) 
  cursorY = screenY * (cursorY / 1) 
  depth = (math.abs(cursorY - screenY) / screenY) * 100 
  depth = 50.0 * ((depth / 100) * (depth / 100)) 
  worldX, worldY, worldZ = getWorldFromScreenPosition(cursorX, cursorY, depth) 
  collision, pX, pY, pZ, element = processLineOfSight(worldX, worldY, worldZ + 5.0, worldX, worldY, -100, true, true, true, true, false, true, true, true, nil) 
  if (selectedElement) then 
    distance = getElementDistanceFromCentreOfMassToBaseOfModel(selectedElement) 
    if (collision and getKeyState("lshift")) then 
      worldZ = pZ + distance 
    end 
    if (getElementType(selectedElement) == "vehicle") then 
      setVehicleFrozen(selectedElement, true) 
    end 
    setElementPosition(selectedElement, worldX, worldY, worldZ) 
  end 
end 
  
addEventHandler("onClientCursorMove", getRootElement(), onClientCursorMove, true) 
  

thx :D

i will see if it works to me :P

EDIT:

well i see than it works better than before but when the cursor is in the buttom of the screen the position of the object start to fail :|

Link to comment
In what way is it failing?

well i have the camera like GTA 2

and when i try to make the object at the bottom of the screen it cant go there :|

Obviously because the code above takes the screen depth into account. The closer you get to the bottom of the screen the closer the object moves towards you. That's what it is supposed to do. It's not working for "bird view".

Link to comment
In what way is it failing?

well i have the camera like GTA 2

and when i try to make the object at the bottom of the screen it cant go there :|

Obviously because the code above takes the screen depth into account. The closer you get to the bottom of the screen the closer the object moves towards you. That's what it is supposed to do. It's not working for "bird view".

so what need to do for make it working? :|

Link to comment
  • 2 weeks later...

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