Jump to content

[HELP] Problem With Rotations


Recommended Posts

Posted

Hi Guy's i have been maken a mounted minigun Script and i have few problems with it

first problem is when i command to create the minigun and attach it to the Player the rotations

of the Elements { Player , Weapon } are not Match's i mean by that the Weapon not In front of the Player <

a Screen Shot of the Problem :arrowdown:

MSzQNc.png

The second Problem is I want the Weapon to move up & down for aiming i made it move but !! the problem

is the weapon and the player are Attached Of course but if there a way to move the weapon only up and down

ScreenShot :

FpbB0D.png

  
 setElementFrozen(localPlayer,false) 
local x,y,z = getElementPosition(localPlayer) 
local gunCol = createColSphere ( x, y, z, 2 ) 
local vehicle = getPedOccupiedVehicle (localPlayer) 
local x2,y2,z2 = getElementPosition(vehicle) 
local screenW,screenH = guiGetScreenSize() 
local weapon 
function attach ( ) 
local detection = isElementWithinColShape ( localPlayer, gunCol ) 
    if detection then 
     weapon = createWeapon("minigun", x2, y2, z2+2) 
    attachElements ( weapon, vehicle, 0, -1.5, 0.7,0,0,180 ) 
    setWeaponClipAmmo(weapon, 99999) 
  --  setWeaponState(weapon, "firing") 
    setWeaponProperty(weapon, "fire_rotation", 0, -30, 0) 
    --setElementPosition ( localPlayer,  x2, y2-1.5, z2+2 ) 
    attachElements ( localPlayer, weapon, 0, 0, 0 ) 
    setElementFrozen(localPlayer,true) 
    toggleAllControls(localPlayer,false) 
    setElementData(localPlayer,"firedoska",true) 
    setPedRotation(localPlayer,90 ) 
    setPedAnimation( localPlayer, "WEAPONS", "SHP_Tray_Pose",0,false) 
setWeaponOwner(weapon,localPlayer) 
end 
end 
addCommandHandler ( "attach", attach ) 
  
local aimSensitivity = 330; 
  
function rotateGun_Handler(cX, cY, aX, aY, wX, wY, wZ) 
    local oX, oY, oZ, oRX, oRY, oRZ = getElementAttachedOffsets(weapon); 
    if (oRX-((cY-0.5)*aimSensitivity) > 45 or oRX-((cY-0.5)*aimSensitivity) < 30) then -- Pitch Restriction 
           oRX = oRX-((cY-0.5)*aimSensitivity); 
    end 
    if (oRZ-((cX-0.5)*aimSensitivity) > 30 or oRZ-((cX-0.5)*aimSensitivity) < 50) then -- Yaw Restriction 
            oRZ = oRZ-((cX-0.5)*aimSensitivity); 
    end 
    setCameraTarget(localPlayer,localPlayer) 
    setElementAttachedOffsets(weapon, oX, oY, oZ, oRX, oRY, oRZ); 
    setCursorPosition(screenW/2, screenH/2); 
end 
addEventHandler( "onClientCursorMove", getRootElement( ), rotateGun_Handler) 
  
           function pedrot() 
              local _,_,rZ = getElementRotation(weapon) 
              setElementRotation(localPlayer, 0, 0, rZ) 
           end 
  addEventHandler("onClientRender", getRootElement(),pedrot) 
  
function fireme() 
if getElementData(localPlayer,"firedoska") == true then 
if getWeaponState(weapon) == "ready" then 
fireWeapon(weapon) 
else return end 
end 
end 
bindKey("mouse1", "down",fireme) 
  
function unaat() 
detachElements (localPlayer, weapon) 
removeEventHandler( "onClientCursorMove", getRootElement( ), rotateGun_Handler) 
removeEventHandler("onClientRender", getRootElement(),pedrot) 
destroyElement(weapon) 
end 
bindKey("k", "down",unaat) 
setTimer(function() 
if getElementData(localPlayer,"firedoska") == true then 
addEventHandler( "onClientCursorMove", getRootElement( ), rotateGun_Handler) 
  addEventHandler("onClientRender", getRootElement(),pedrot) 
end 
    end,1000,0) 
  
  

i well be grateful for help Thanks all, :oops:

  • Moderators
Posted

This piece of code will give you the matrix of 1 point which has a direction vector.

        local rx, ry, rz = 0,0,0 
        local vectorX,vectorY,vectorZ = -- fill in... (the vector) 
  
        local rz = -math.deg( math.atan2(vectorX,vectorY ) )  
         
  
        local rx = math.atan2(vectorZ,math.sqrt(math.pow(vectorY,2) + math.pow(vectorX,2)))*(180/math.pi) 
         
        local 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] = -- fill in... (position) 
        matrix[4][4] = 1 

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

Posted
This piece of code will give you the matrix of 1 point which has a direction vector.
        local rx, ry, rz = 0,0,0 
        local vectorX,vectorY,vectorZ = -- fill in... (the vector) 
  
        local rz = -math.deg( math.atan2(vectorX,vectorY ) )  
         
  
        local rx = math.atan2(vectorZ,math.sqrt(math.pow(vectorY,2) + math.pow(vectorX,2)))*(180/math.pi) 
         
        local 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] = -- fill in... (position) 
        matrix[4][4] = 1 

i'm sorry but can you explain for me more i don't get it >?

  • Moderators
Posted

Well, you want the minigun aiming to a location somewhere in mta. (pointX2,pointY2,pointZ2 )

You also have the location of the minigun. (which you can fill in on line 31) (pointX1,pointY1,pointZ1)

When you get the direction vector from those 2 locations you can make a matrix(table).

This matrix can be used on your minigun element: https://wiki.multitheftauto.com/wiki/SetElementMatrix

How to find this vector?

local pointX1,pointY1,pointZ1 
local pointX2,pointY2,pointZ2  
  
local vectorX,vectorY,vectorZ = pointX2-pointX1,pointY2-pointY1,pointZ2-pointZ1 

I am not a pro at matrix and vectors, so it is kinda hard to explain it to you.

Do you want to improve your Lua programming skills and make less mistakes?   Start with Lua Language Server!   🙀

 

  Useful functions  3x 

  Tutorials  4x 

 

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