Jump to content

Finding XYZ in front of – my formula works, but...


relvarten

Recommended Posts

I wrote a function to create objects in front of:

function xyz_in_front_3d(x, y, z, rz, rx, distance)
    x = x - math.sin(math.rad(rz)) * (math.cos(math.rad(rx)) * distance)
    y = y + math.cos(math.rad(rz)) * (math.cos(math.rad(rx)) * distance)
    z = z + math.sin(math.rad(rx)) * distance
    return x, y, z
end

In short, the principle of the function:

An object is created in front of you, based on your Z rotation and X rotation, in addition, the Y rotation of the object is changed to the same as that of the player. The distance is determined by the last parameter of the function.

The command:

function CMDo(source, command, parameterDistance)
    local x, y, z, rx, ry, rz
    x, y, z = getElementPosition(getPedOccupiedVehicle(source))
    rx, ry, rz = getElementRotation(getPedOccupiedVehicle(source))
    x, y, z = xyz_in_front_3d(x, y, z, rz, rx, parameterDistance)
    SpawnedObject = createObject(1634, x, y, z)
    setElementRotation(SpawnedObject, rx, ry, rz)
end
addCommandHandler("o", CMDo, false, false)

Spoiler:

Spoiler

Usage of the command:

/o 10

The result:

ojNaw5h.png

 

mDBHTqg.png

FmS9hFy.png

The problems started when I wanted to change not only the range, but also X and Z. And so, I began to modernize the command to make this possible:

function CMDo(source, command, parameterDistance, parameterWidth) --New parameter: parameterWidth
    local x, y, z, rx, ry, rz
    x, y, z = getElementPosition(getPedOccupiedVehicle(source))
    rx, ry, rz = getElementRotation(getPedOccupiedVehicle(source))
    x, y, z = xyz_in_front_3d(x, y, z, rz, rx, parameterDistance)
    x, y, z = xyz_in_front_3d(x, y, z, rz - 90, -ry, parameterWidth) -- New line
    SpawnedObject = createObject(987, x, y, z)
    setElementRotation(SpawnedObject, rx, ry, rz, "ZYX") -- New parameter: "ZYX"
end
addCommandHandler("o", CMDo, false, false)

Why did I add "ZYX" for setElementRotation? – Worked incorrectly without it.

And now i can do magic:

Spoiler:

Spoiler

Usage of the command:

/o -15 10 0
/o -10 10 0
/o -5 10 0
/o -0 10 0
/o 5 10 0
/o 10 10 0
/o 15 10 0
/o -15 10 0

The result:

i9Bh36z.png

And in this moment I'm stuck :-( I can't figure out how to explain to the game how to do the same but for the height (it would seem that you just need to turn 90 degrees...).

But ok, let's try:

function CMDo(source, command, parameterDistance, parameterWidth, parameterHeight) --New parameter: parameterHeight
    local x, y, z, rx, ry, rz
    x, y, z = getElementPosition(getPedOccupiedVehicle(source))
    rx, ry, rz = getElementRotation(getPedOccupiedVehicle(source))
    x, y, z = xyz_in_front_3d(x, y, z, rz, rx, parameterDistance)
    x, y, z = xyz_in_front_3d(x, y, z, rz - 90, -ry, parameterWidth)
    x, y, z = xyz_in_front_3d(x, y, z, ry, rx + 90, parameterHeight) -- New line
    SpawnedObject = createObject(987, x, y, z)
    setElementRotation(SpawnedObject, rx, ry, rz, "ZYX")
end
addCommandHandler("o", CMDo, false, false)

Spoiler:

Spoiler

Usage of the command:

/o 0 10 -5
/o 0 10 0
/o 0 10 5

At best, the result is a Frankenstein like this:

* How it should be

* How it actually "works"

8u3rOxL.png

 

Edited by relvarten
Link to comment
  • Moderators
2 hours ago, relvarten said:

And in this moment I'm stuck 

I understand that you want to do the calculations your self.

But if you are using a matrix, you really do not have to think about all that complex and brain exploding stuff.

https://wiki.multitheftauto.com/wiki/Matrix

local x, y, z, rx, ry, rz
x, y, z = getElementPosition(getPedOccupiedVehicle(source))
rx, ry, rz = getElementRotation(getPedOccupiedVehicle(source))

local vehicleMatrix = Matrix ( Vector3(x, y, z), Vector3(rx, ry, rz) )

local offsetX, offsetY, offsetZ = 0, 0, 0
local newPosition = vehicleMatrix:transformPosition ( Vector3(offsetX, offsetY, offsetZ) )

SpawnedObject = createObject(987, newPosition.x, newPosition.y, newPosition.z)
setElementRotation(SpawnedObject, rx, ry, rz, "ZYX")

Note: You can do all of this with less lines.

 

  • Thanks 2
Link to comment
18 minutes ago, IIYAMA said:

I understand that you want to do the calculations your self.

1wS0Fxs.png

But...

I'M TIRED OF THESE CALCULATIONS. IT WAS FUN IN THE BEGINNING, BUT NOW I GIVE UP!

YOU HAVE NO IMAGINATION HOW GRATEFUL I AM TO YOU. YOUR METHOD WORKS.

I really hope that this topic will help other people as well, since for me personally this kind of object-movement is the basis for any server.

  • Like 1
  • Thanks 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...