Jump to content

[Solv]Changing Element Position according to camera position


darhal

Recommended Posts

Hello all,

This problem is hard to explain by the way I will post images it will explain better then words :

here is my code :

http://imgur.com/2p7DggD,L4yKIok,SH8KZ3f#0 -- work fine here

http://imgur.com/2p7DggD,L4yKIok,SH8KZ3f#1 --this how I want it when the camera change poistion or rotation

http://imgur.com/2p7DggD,L4yKIok,SH8KZ3f#2 -- this how it work in real

  
--------------------------------- 
-- Utility functions 
--------------------------------- 
  
function getPositionFromElementOffset(element,offX,offY,offZ) 
    local m = getElementMatrix ( element )  -- Get the matrix 
    local x = offX * m[1][1] + offY * m[2][1] + offZ * m[3][1] + m[4][1]  -- Apply transform 
    local y = offX * m[1][2] + offY * m[2][2] + offZ * m[3][2] + m[4][2] 
    local z = offX * m[1][3] + offY * m[2][3] + offZ * m[3][3] + m[4][3] 
    return x, y, z                               -- Return the transformed point 
end 
  
------------------------------------ 
-- End of Utility functions 
------------------------------------ 
  
function controlObject() 
    if not objectInEditor then return end 
    if pressedButton == "num_8" then         
        x,y,z = getMatrixForward(objectInEditor,0,moveSpeed,0) 
        setElementPosition(objectInEditor, x, y, z) 
    elseif pressedButton == "num_2" then         
        x,y,z = getPositionFromElementOffset(objectInEditor,0,0-moveSpeed,0) 
        setElementPosition(objectInEditor, x, y, z) 
    elseif pressedButton == "num_4" then     
        x,y,z = getPositionFromElementOffset(objectInEditor,moveSpeed,0,0) 
        setElementPosition(objectInEditor, x, y, z) 
    elseif pressedButton == "num_6" then     
        x,y,z = getPositionFromElementOffset(objectInEditor,0-moveSpeed,0,0) 
        setElementPosition(objectInEditor, x, y, z) 
    elseif pressedButton == "pgup" then  
        x,y,z = getPositionFromElementOffset(objectInEditor,0,0, moveSpeed) 
        setElementPosition(objectInEditor, x, y, z) 
    elseif pressedButton == "pgdn" then  
        x,y,z = getPositionFromElementOffset(objectInEditor,0,0, 0-moveSpeed) 
        setElementPosition(objectInEditor, x, y, z) 
    end 
end 
addEventHandler("onClientRender", root, controlObject) 
  

in map editor this problem is already solved but I can t find the function that find the position

Please help me guys this made me confused I already checked the forum evean the old posts and I cant find solution :?::!:

thx :)

Edited by Guest
Link to comment

Well I m making something like map editor it mean u can control objects via keys wich are arrow up arrow left arrow rightt etc... the pictures show how controles work fine for same camera sides and for some camera postion/rotation the element move to the wrong direction I want to fix this

Link to comment
  • Moderators

Try something like this:

function controlObject() 
    if not objectInEditor then return end 
    local camera = getCamera () 
    local cameraX,cameraY,cameraZ = getElementPosition(camera) 
    local objectX,objectY,objectZ = getElementPosition(objectInEditor) 
    local x,y,z 
    if pressedButton == "num_8" then        
        x,y,z = getMatrixForward(camera,0,moveSpeed,0) 
    elseif pressedButton == "num_2" then        
        x,y,z = getPositionFromElementOffset(camera,0,0-moveSpeed,0) 
    elseif pressedButton == "num_4" then    
        x,y,z = getPositionFromElementOffset(camera,moveSpeed,0,0) 
    elseif pressedButton == "num_6" then    
        x,y,z = getPositionFromElementOffset(camera,0-moveSpeed,0,0) 
    elseif pressedButton == "pgup" then  
        x,y,z = getPositionFromElementOffset(camera,0,0, moveSpeed) 
    elseif pressedButton == "pgdn" then  
        x,y,z = getPositionFromElementOffset(camera,0,0, 0-moveSpeed) 
    end 
    if x then 
        setElementPosition(objectInEditor, objectX+(x-cameraX),objectY+(y-cameraY),objectZ+(z-cameraZ)) 
    end 
end 
addEventHandler("onClientRender", root, controlObject) 

I haven't build something like this before, so I have no idea how to script this perfect.

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