Jump to content

Getting position of createdVehicle


Puma

Recommended Posts

Posted

Ohai,

I'm doing a script in which I attach an object to a vehicle, created using createVehicle on position x, y, z. When I let the vehicle 'drive' to a new position using "setElementVelocity", the position (x, y, z) stays the same. Pretty weird, seen that the position of the vehicle itself DID change (I output the position right after it checks it, which happens to stay the same while the vehicle is driving away from me).

Why am I doing this? I want to change position of a colshape that belongs to the vehicle. Since it isn't possible to attach colshapes to elements, I request the position and rotation of the vehicle every 50 ms. Problem is that the position of the colshape stays the same: where it was created.

My script:

-- SERVERSIDE -- 
-- SERVERSIDE -- 
-- SERVERSIDE -- 
  
function bindGreen ( p, green ) 
    bindKey ( p, "lctrl", "down", shootGreen, green ) 
end 
  
function shootGreen ( p, a, b, green ) 
    unbindKey ( p, "lctrl", "down", shootGreen ) 
    local v = getPedOccupiedVehicle ( p ) 
    local x, y, z = getElementPosition ( v ) 
    local rx, ry, rz = getVehicleRotation ( v ) 
    local greenV = createVehicle ( 441, x, y, z, rx, ry, rz ) 
    local col = createColSphere ( x, y, z, 5 ) 
    setElementData ( col, "green", green ) 
    setElementData ( col, "greenV", greenV ) 
    setElementData ( greenV, "col", col ) 
    setElementData ( greenV, "green", green ) 
    setElementData ( greenV, "gdist", 0 ) 
    setElementData ( greenV, "gpos", {x, y, z} ) 
    setElementData ( greenV, "rot", { rx, ry, rz } ) 
    setElementData ( col, "source", v ) 
    checkGreen ( p, col, green, greenV ) 
end 
  
function checkGreen ( p, col, green, greenV ) 
    if isElement ( col ) and isElement ( green ) and isElement ( greenV ) then 
        local x, y, z = getElementPosition ( greenV ) 
        local rx, ry, rz = getVehicleRotation ( greenV ) 
        setElementPosition ( col, x, y, z ) 
        setElementRotation ( col, rx, ry, rz )   
        setElementData ( greenV, "greenHit", 0 ) 
        driveGreen ( p, col, green, greenV ) 
    end 
end 
  
function killShell ( col, greenV, green ) 
    destroyElement ( green ) 
    destroyElement ( greenV ) 
    destroyElement ( col ) 
end 
  
function driveGreen ( p, col, green, greenV ) 
    triggerClientEvent ( p, "onDriveGreen", greenV, p, col, green ) 
end 
  
function boostGreen ( p, col, green ) 
    local v = source 
    local rot = getElementData ( source, "rot" ) 
    local rx = rot[1] 
    local ry = rot[2] 
    local rz = rot[3] 
    setVehicleRotation ( source, rx, ry, rz ) 
    local vx, vy, vz = getElementVelocity ( v ) 
    setElementVelocity ( v, 0.5*math.sin(math.rad(360-rz)), 0.5*math.cos(math.rad(360-rz)), vz ) 
    setTimer ( checkGreen, 50, 1, p, col, green, source ) 
end 
addEvent ( "onBoostGreen", true ) 
addEventHandler ( "onBoostGreen", getRootElement(), boostGreen ) 
  
-- CLIENTSIDE -- 
-- CLIENTSIDE -- 
-- CLIENTSIDE -- 
  
function driveGreen ( p, col, green ) 
    triggerServerEvent ( "onBoostGreen", source, p, col, green ) 
end 
addEvent ( "onDriveGreen", true ) 
addEventHandler ( "onDriveGreen", getRootElement(), driveGreen ) 

There's a lot more in the functions, these are just the relevant parts. I bind a key, when it is pressed I create a vehicle and do some shit with it. The colshape changes position to the position of the created vehicle, a clientside event is triggered and then a serverside event is triggered, in which the created vehicle get's a boost. 50 ms later the whole process, from changing the position of the colshape, repeats untill a part of the script breaks it (I left that part out, because it isn't relevant).

In short:

local x, y, z = getElementPosition ( greenV )

setElementPosition ( col, x, y, z )

.. in "checkGreen" doesn't change the col's position. And yes, the elements that are checked do exist.

So... whut should I do?

Posted

Okay this is weird:

0

0

0

0

0

0

0

0

0

0

0

21.911735534668

That's what is being output when I use this:

function driveGreen ( p, col, green, greenV ) 
    local x, y, z = getElementPosition ( greenV ) 
    local oldPos = getElementData ( greenV, "gpos" ) 
    local oldDist = getElementData ( greenV, "gdist" ) 
    local newDist = getDistanceBetweenPoints3D ( x, y, z, oldPos[1], oldPos[2], oldPos[3] ) + oldDist 
    outputChatBox ( newDist ) 
    setElementData ( greenV, "gdist", newDist ) 
    setElementData ( greenV, "gpos", {x, y, z} ) 
    if newDist > 50 then 
        killShell ( col, greenV, green ) 
    else 
        triggerClientEvent ( p, "onDriveGreen", greenV, p, col, green ) 
    end  
end 

driveGreen is executed every 50 ms, and because the colshape doesn't change position I suddenly am 'in the colshape' and it kills me (other part of the script).

Posted

Ah, I know the problem: A vehicle that's created using createVehicle can't change position serverside without setElementPosition, since the server doesn't render the game and the game's physics: the moves of the vehicle are rendered clientside :D .

Posted

i don't get the idea of syncers, but i thought one of players should be syncer of the vehicle, so server should get its position.. but im not sure how it really works. in no players are around - you cant get its position for sure

Posted
getting the position of vehicles which are not in syncer range of anyone shouldn't be a problem.

yes, but setting the velocity on server when noone is around? server dont have psychics

Posted

When you create a vehicle and move it to x,y,z without a player sitting in it (so using setElementVelocity), the position of that vehicle does not change serverside, since the server doesn't render the game and its physics.

Posted

Well I use the player that shoots it as calculator: I trigger a clientside event, get the position and rotation and send that info back serverside by triggering a serverevent.

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