Jump to content

Create arrow pointing an element


'LinKin

Recommended Posts

Hello,

When I create an arrow and attach it to a player, I want it to be visible only for the player that it's attached to..

How can I do that via serverside?

setElementVisibleTo ( theArrow, getRootElement(), false ) did not work.. Everyone can see the arrow

Edited by Guest
Link to comment

(Not Tested)

  
local car = createVehicle ( 0, 0, 0 ) 
local arrow = createObject ( the_arrow_id, 0, 0, 0 ) 
addEventHandler ( "onClientPreRender", root, function ( ) 
  local cx, cy, cz = getElementPosition ( car ) 
  local x, y, z = getElementPosition ( localPlayer ) 
  local rot = findRotation ( cx, cy, x, y ) 
  setElementPosition ( arrow, x, y, z + 1 ) 
  setElementRotation ( arrow, 0, 0, rot ) 
end ) 
  
  
  
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 
  

Link to comment

Also.

Is it possible that the arrow points to a target in 3D? I mean, I see most of GPS systems just turn the arrow horizontally.

Is it possible to make an arrow point to the target vertically too?

For example; If the target is under you, it points down. If target's over you, it points up. If target's diagonally, it points diagonally (vertical & horizontal rotation)

Link to comment
Also.

Is it possible that the arrow points to a target in 3D? I mean, I see most of GPS systems just turn the arrow horizontally.

Is it possible to make an arrow point to the target vertically too?

For example; If the target is under you, it points down. If target's over you, it points up. If target's diagonally, it points diagonally (vertical & horizontal rotation)

It is, but you'd need to make your own "findRotation" function for that.

Link to comment
function GPS:update() 
    local vehicle = getPedOccupiedVehicle(localPlayer) 
    if not vehicle then 
        self:stopNavigation() 
        return 
    end 
     
    local x, y, z = getElementPosition(vehicle) 
    local horizontalRotation = findRotation(x, y, self.m_Destination.X, self.m_Destination.Y) - 90 
    local verticalRotation = math.deg(math.asin((self.m_Destination.Z - z) / getDistanceBetweenPoints3D(self.m_Destination.X, self.m_Destination.Y, self.m_Destination.Z, x, y, z))) 
     
    setElementPosition(self.m_Arrow, x, y, z + 1) 
    setElementRotation(self.m_Arrow, 0, 90 + verticalRotation, horizontalRotation) 
end 

Link to comment

It's a function that calculates the rotation you need to point to a specified position.

Transferred to your code:

local car = createVehicle ( 0, 0, 0 ) 
local arrow = createObject ( the_arrow_id, 0, 0, 0 ) 
addEventHandler ( "onClientPreRender", root, function ( ) 
  local cx, cy, cz = getElementPosition ( car ) 
  local x, y, z = getElementPosition ( localPlayer ) 
  local rotH = findRotation ( cx, cy, x, y ) - 90 
  local rotV = math.deg(math.asin((cz - z) / getDistanceBetweenPoints3D(cx, cy, cz, x, y, z))) 
  setElementPosition ( arrow, x, y, z + 1 ) 
  setElementRotation ( arrow, 0, 90 + rotV, rotH ) 
end ) 

Link to comment

I posted this some days ago:

function findPitch(camerax,cameray,cameraz,pointx,pointy,pointz) 
  
local dX=camerax-pointx 
local dY=cameraz-pointz 
local dZ=cameray-pointy 
  
local pitch=math.atan2(dY,math.sqrt(math.pow(dZ,2) + math.pow(dX,2))); 
return pitch 
end 

It's returning in radians, so you need to convert it to degrees:

local degree = pitchReturn*(180/math.pi) 

pitchReturn = The "findPitch" function.

It's the same as findRotation but then for the X rotation (up and down) so if someone is underneath you it will calculate the position under you. Here's a little example with a plane:

 function findYaw(x1,y1,x2,y2) 
  
  local zRot = -math.deg(math.atan2(x2-x1,y2-y1)) 
  if zRot < 0 then zRot = zRot + 360 end; 
  return zRot; 
  
end 
  
  
function findPitch(camerax,cameray,cameraz,pointx,pointy,pointz) 
  
local dX=camerax-pointx 
local dY=cameraz-pointz 
local dZ=cameray-pointy 
  
local pitch=math.atan2(dY,math.sqrt(math.pow(dZ,2) + math.pow(dX,2))); 
return pitch 
end 
  
  
car = createVehicle ( 513, 0,0, 10 ) 
  
  
addEventHandler ( "onClientPreRender", root, function () 
    local x,y,z = getElementPosition (localPlayer) 
    local pointX, pointY, pointZ = getElementPosition ( car ) 
    local rotZ = findYaw(pointX, pointY,x,y) 
    local rotX = findPitch(pointX,pointY,pointZ, x,y,z) 
    local degreePitch = rotX*(180/math.pi) 
    setElementPosition ( car, 0,0, 10 ) 
    setElementRotation ( car,-degreePitch,0,rotZ) 
end) 
  
  

findYaw is the same function as "findRotation".

Edit: Put the degree return always in the "-", or else it will point the other way

Regards,

Danny

Link to comment

Thanks Danny, arezu, Justonex. As I said, it's working fine, the horizontal and vertical rotation is perfect. I was just asking for the model of the arrow, because my arrow is different from others.. I don't know why.

Im using the same model as others. Maybe it is something else that changes its aspect?

Link to comment

I'm not a pro at math but the solution is quite simple as I see. In shooter maps we always shoot rockets in straight direction, so you want that arrow to be in straight direction, right? So download an shooter map and there should be 4 lines where math.cos is used, just use thèse four lines to get the position and to to keep the arrow in straight direction and add onClientRender to get the rotation. Right now I'm away from home, once I'm back. I'll help you then.

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