Xwad

getOffsetFromXYZ

Recommended Posts

Hello.

I wan't to determine which side of the vehicle was shooted (front or rear). My problem is that the front and the rear will be the opposite when the vehicle is rotated 180 deegres. I know, i have to use getOffsetFromXYZ but i dont really understand the logic.

 

function onFire(weapon,ammo,ammoInClip,hitX,hitY,hitZ,hitElement)
    if getElementType(hitElement) == "vehicle" then        

        local x,y,z = getOffsetFromXYZ( getElementMatrix(hitElement), getElementPosition(hitElement) )
        
        local offsetX = hitX - x
        local offsetY = hitY - y
        local offsetZ = hitZ - z
        
        if offsetX > 0 then
        outputChatBox("rear")
        elseif offsetX < 0 then
        outputChatBox("front")
        end
    end
end
addEventHandler("onClientPlayerWeaponFire", root, onFire)



function getOffsetFromXYZ( mat, vec )
    -- make sure our matrix is setup correctly 'cos MTA used to set all of these to 1.
    mat[1][4] = 0
    mat[2][4] = 0
    mat[3][4] = 0
    mat[4][4] = 1
    mat = matrix.invert( mat )
    local offX = vec[1] * mat[1][1] + vec[2] * mat[2][1] + vec[3] * mat[3][1] + mat[4][1]
    local offY = vec[1] * mat[1][2] + vec[2] * mat[2][2] + vec[3] * mat[3][2] + mat[4][2]
    local offZ = vec[1] * mat[1][3] + vec[2] * mat[2][3] + vec[3] * mat[3][3] + mat[4][3]
    return {offX, offY, offZ}
end

 

Share this post


Link to post
Posted (edited)
local posX, posY, posZ = getElementPosition(hitElement)

local offsetX, offsetY, offsetZ = getOffsetFromXYZ(getElementMatrix(hitElement), posX - hitX, posY - hitY, posZ - hitZ)

The matrix is already containing the position of the vehicle.

Untested and not even sure if this is the right approach.

 

@Xwad

 

 

P.s this function can return the surface position:https://wiki.multitheftauto.com/wiki/ProcessLineOfSight

normalX, normalY, normalZ

Edited by IIYAMA

Share this post


Link to post

nvm that is probably not going to work.

 

Share this post


Link to post

I'd just get the angle between the hit pos and vehicle's pos + vehicle.rotation.z, thats it.

Share this post


Link to post

And which function should i use for getting the angle?

Share this post


Link to post

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.