Jump to content

Adding Missiles to Shamal Plane


#MacTavish

Recommended Posts

Hi all, im trying to add missile launchers on shamal plane right and left wings. I added them and i can fire them, no problem till this point. But there is a problem when i turn the plane 90 degree, missiles overlaps each others. I tried to explain it by image : https://prnt.sc/uknal0  What should i do ? Please help me.  

function shootMissiles()
    thePilot = getLocalPlayer()
    theVehicle = getPedOccupiedVehicle(thePilot)
    if theVehicle and getElementType(theVehicle) == 'vehicle' then
        theVehicleModel = getElementModel(theVehicle)
        if theVehicleModel == 519 then
            local x, y, z = getElementPosition(theVehicle)
			local rotX, rotY, rotZ = getElementRotation(thePilot)
			missileLeft = createProjectile(theVehicle, 19, x, y, z)
			missileRight = createProjectile(theVehicle, 19, x, y, z)
			attachElements ( missileLeft, theVehicle, -3, 3.5, 0, 0, 30, 90, 1.0, nil, rotX, rotY, rotZ)
			attachElements ( missileRight, theVehicle, 3, 3.5, 0, 0, 30, 90, 1.0, nil, rotX, rotY, rotZ)
            end
        end
    end
bindKey("lctrl", "down", shootMissiles)

 

Link to comment
  • Moderators
28 minutes ago, #MacTavish said:

What should i do ? Please help me.  

Use the function: getPositionFromElementOffset

Which you can find on this page, under examples:

https://wiki.multitheftauto.com/wiki/GetElementMatrix

 

 

That function allows you to get offset positions, no matter what rotation a vehicle has.

-- Get the position of a point 1 unit above the element:
x,y,z = getPositionFromElementOffset(element,0,0,1)

 

Edited by IIYAMA
  • Thanks 1
Link to comment
33 minutes ago, IIYAMA said:

Use the function: getPositionFromElementOffset

Which you can find on this page, under examples:

https://wiki.multitheftauto.com/wiki/GetElementMatrix

 

 

That function allows you to get offset positions, no matter what rotation a vehicle has.


-- Get the position of a point 1 unit above the element:
x,y,z = getPositionFromElementOffset(element,0,0,1)

 

i tried to solve it by using func that you told me. But i couldn't do it, i didn't understood how can i use this exactly ? 

function shootMissiles()
    thePilot = getLocalPlayer()
    theVehicle = getPedOccupiedVehicle(thePilot)
    if theVehicle and getElementType(theVehicle) == 'vehicle' then
        theVehicleModel = getElementModel(theVehicle)
        if theVehicleModel == 519 then
            local x, y, z = getElementPosition(theVehicle)
			local rotX, rotY, rotZ = getElementRotation(theVehicle)
			local matrix = getElementMatrix(theVehicle)
			local offX = 0 * matrix[1][1] + 1 * matrix[2][1] + 0 * matrix[3][1] + 1 * matrix[4][1]
			local offY = 0 * matrix[1][2] + 1 * matrix[2][2] + 0 * matrix[3][2] + 1 * matrix[4][2]
			local offZ = 0 * matrix[1][3] + 1 * matrix[2][3] + 0 * matrix[3][3] + 1 * matrix[4][3]
			local vx = offX - x
			local vy = offY - y
			local vz = offZ - z
			x = 0 * matrix[1][1] + 3 * matrix[2][1] + 0 * matrix[3][1] + 1 * matrix[4][1]
			y = 0 * matrix[1][2] + 3 * matrix[2][2] + 0 * matrix[3][2] + 1 * matrix[4][2]
			z = 0 * matrix[1][3] + 3 * matrix[2][3] + 0 * matrix[3][3] + 1 * matrix[4][3]
			missileLeft = createProjectile(theVehicle, 19,x+2,y,z, 200, nil, 0, 0, 360 - rotZ, vx, vy, vz)
			missileRight = createProjectile(theVehicle, 19,x-2,y,z, 200, nil, 0, 0, 360 - rotZ, vx, vy, vz)
            end
        end
    end
bindKey("lctrl", "down", shootMissiles)

 

Link to comment
  • Moderators
14 hours ago, #MacTavish said:

i tried to solve it by using func that you told me. But i couldn't do it, i didn't understood how can i use this exactly ? 

Just copy the getPositionFromElementOffset function and place it above or below your code.

No need to merge functions.

Use it like this (inside of your function):

local x, y, z = getPositionFromElementOffset(theVehicle, -3, 3.5, 0)

 

Edited by IIYAMA
  • Thanks 1
Link to comment
30 minutes ago, IIYAMA said:

Just copy the getPositionFromElementOffset function and place it above or below your code.

No need to merge functions.

Use it like this (inside of your function):


local x, y, z = getPositionFromElementOffset(theVehicle, -3, 3.5, 0)

 

Okay, i will try to do it. If it works, i will notify you, thank you :)

  • Like 1
Link to comment

@IIYAMAthank you so much, it works good ! Here is the code, maybe you want to see. Thank you !

function shootMissiles()
    thePilot = getLocalPlayer()
    theVehicle = getPedOccupiedVehicle(thePilot)
    if theVehicle and getElementType(theVehicle) == 'vehicle' then
        theVehicleModel = getElementModel(theVehicle)
        if theVehicleModel == 519 then
			local x1, y1, z1 = getPositionFromElementOffset(theVehicle, -3, 3.5, 0)
			local x2, y2, z2 = getPositionFromElementOffset(theVehicle, 3, 3.5, 0)
			missileLeft = createProjectile(theVehicle, 19, x1, y1, z1)
			missileRight = createProjectile(theVehicle, 19, x2, y2, z2)
            end
        end
    end
bindKey("lctrl", "down", shootMissiles)

function getPositionFromElementOffset(element,offX,offY,offZ)
    local m = getElementMatrix ( element )  
    local x = offX * m[1][1] + offY * m[2][1] + offZ * m[3][1] + m[4][1]  
    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
end

 

  • Like 1
Link to comment
  • Moderators
16 minutes ago, #MacTavish said:

@IIYAMAthank you so much, it works good ! Here is the code, maybe you want to see. Thank you !


function shootMissiles()
    thePilot = getLocalPlayer()
    theVehicle = getPedOccupiedVehicle(thePilot)
    if theVehicle and getElementType(theVehicle) == 'vehicle' then
        theVehicleModel = getElementModel(theVehicle)
        if theVehicleModel == 519 then
			local x1, y1, z1 = getPositionFromElementOffset(theVehicle, -3, 3.5, 0)
			local x2, y2, z2 = getPositionFromElementOffset(theVehicle, 3, 3.5, 0)
			missileLeft = createProjectile(theVehicle, 19, x1, y1, z1)
			missileRight = createProjectile(theVehicle, 19, x2, y2, z2)
            end
        end
    end
bindKey("lctrl", "down", shootMissiles)

function getPositionFromElementOffset(element,offX,offY,offZ)
    local m = getElementMatrix ( element )  
    local x = offX * m[1][1] + offY * m[2][1] + offZ * m[3][1] + m[4][1]  
    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
end

 

Good job!

 

The last example on that page can also be handy:

Quote

This example function allows you to get the element matrix of an element that is not streamed in.

 

If you change that a bit, to this:

You will be able to create matrices based on position and rotation without the need of elements. ::idea:

function createMatrix(x, y, z, rx, ry, rz)
    rx, ry, rz = math.rad(rx), math.rad(ry), math.rad(rz)
    local matrix = {}
    matrix[1] = {}
    matrix[1][1] = math.cos(rz)*math.cos(ry) - math.sin(rz)*math.sin(rx)*math.sin(ry)
    matrix[1][2] = math.cos(ry)*math.sin(rz) + math.cos(rz)*math.sin(rx)*math.sin(ry)
    matrix[1][3] = -math.cos(rx)*math.sin(ry)
    matrix[1][4] = 1
    
    matrix[2] = {}
    matrix[2][1] = -math.cos(rx)*math.sin(rz)
    matrix[2][2] = math.cos(rz)*math.cos(rx)
    matrix[2][3] = math.sin(rx)
    matrix[2][4] = 1
	
    matrix[3] = {}
    matrix[3][1] = math.cos(rz)*math.sin(ry) + math.cos(ry)*math.sin(rz)*math.sin(rx)
    matrix[3][2] = math.sin(rz)*math.sin(ry) - math.cos(rz)*math.cos(ry)*math.sin(rx)
    matrix[3][3] = math.cos(rx)*math.cos(ry)
    matrix[3][4] = 1
	
    matrix[4] = {}
    matrix[4][1], matrix[4][2], matrix[4][3] = x, y, z
    matrix[4][4] = 1
	
    return matrix
end


function getPositionFromMatrixOffset(m, offX, offY, offZ)
    local x = offX * m[1][1] + offY * m[2][1] + offZ * m[3][1] + m[4][1]  
    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
end


And with that you can make the most crazy stuff, for example: 3D (rotated) dx effects that are not attached to an element.

local m = createMatrix(x1, y1, z1, rx, ry, rz)
local x2, y2, z2 = getPositionFromMatrixOffset(m, offX, offY, offZ)

 

 

 

  • Thanks 1
Link to comment
  • Moderators
1 hour ago, Gordon_G said:

Hi,

Isn't the use of OOP better (i mean, faster) to do this kind of position transformation ?

In most cases yes.

There is of course the Matrix function which will return a matrix class. And it will indeed perform better for this user case.

But I am not going to explain OOP and vectors, when people are not familiar with them / using them.

 

Edited by IIYAMA
  • Like 1
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...