Jump to content

Calculating a Offset + Vector from Position and Rotation


Recommended Posts

Hello Community,

I try to calculate a Position offset and a vector from a given Position X, Y, Z and Rotation X, Y, Z:

http://i.imgur.com/4HUOGMg.jpg

I hope you know what i mean.

My plan is to remove the original Rhino grenade and replace it with my own:

1. Get position and Rotation of the Turret (That works very well now)

2. Get position of the barrel (I have no idea how to do that)

3. Create an RC-Cam on that Position, setElementVelocity to this object, playSound3D and let it explode after impact

Problem is, that MTA has no function like "getVehicleMuzzlePosition" (Why ever...) so it is not easy to calculate an offset from the Turret center to the end of the cannon. The original Rhino Projectile is just annoying the way it is.

This is what i have:

  
function getPosRot() 
x, y, z = getElementPosition (getPedOccupiedVehicle ( getLocalPlayer())) 
xc, yc, zc = getVehicleComponentPosition ( getPedOccupiedVehicle ( getLocalPlayer()), "misc_b" ) --misc_b = barrel 
local rx, ry_unused, rz_unused = getVehicleComponentRotation ( getPedOccupiedVehicle ( getLocalPlayer()), "misc_b" ) 
local rotX, ry, rotZ = getElementRotation (getPedOccupiedVehicle ( getLocalPlayer())) 
local rx_unused, ry_unused, rz = getVehicleComponentRotation ( getPedOccupiedVehicle ( getLocalPlayer()), "misc_a" ) --misc_a = turret 
  
rz = rotZ - rz 
rx = rotX - rx 
z = z + zc 
  
--now we have the exact Position and Rotation of the Turret (tested) 
  
xv,yv,zv = getPositionFromElementOffset(getPedOccupiedVehicle(getLocalPlayer()),0,2,0) --call the other Function, which should calculate the Y offset (0,2,0) from turret center to the end of the barrel 
grenade = createVehicle (594, xv, yv, zv, rx, ry, rz ) --create the Custom Projectile 
engineSetModelLODDistance ( 594, 170 ) --increase visible Distance of the Projectile (doesnt work correctly) 
setElementVelocity (grenade, 0, 10, 0 ) --set Velocity, those are just some values because i have no idea how to calculate it 
end 
addCommandHandler("get", getPosRot) 
  
function getSpecialMatrix(vehicle) 
--this function should return the Matrix of the Barrel, it gets 3 different Rotations (x from Barrel, y from Rhino, z from Turret) 
local rx, ry_unused, rz_unused = getVehicleComponentRotation (vehicle, "misc_b" ) 
local rotX, ry, rotZ = getElementRotation (vehicle) 
local rx_unused, ry_unused, rz = getVehicleComponentRotation (vehicle, "misc_a" ) 
  
rz = rotZ - rz 
rx = rotX - rx 
--local rx, ry, rz = math.rad(rz), 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[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[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[4] = {} 
    matrix[4][1], matrix[4][2], matrix[4][3] = getElementPosition(vehicle)  
    return matrix 
end 
  
  
function getPositionFromElementOffset(element,offX,offY,offZ) 
--this function should calculate the offset, but it doesnt work correctly. 
    local m = getSpecialMatrix ( element ) --get the barrel matrix 
    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 --return the wanted offset 
end 
  

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