Hallo ihr,
heute gibt es viel Text für ganz wenig Script. Ich habe keinen direkten Nutzen dafür aber vielleicht braucht ja mal jemand sowas. Der Codeschnipsel ist mit Sicherheit keine Meisterleistung und nur durch Zufall entstanden.
Es handelt sich um eine neue Funktion für MTA, basierend auf getPointFromDistanceRotation(). Diese Funktion ermittelt quasi eine vorher definierte Position in Abhängigkeit von einem bewegtem Element (Player, Vehicle, Ped ... usw. ...). Es lassen sich somit Sachen an ein bewegliches Objekt heften, welche von Haus aus nicht attachable sind (z. B. Camera) und zwar wohin man möchte.
Code Funktion:
function getAttachedPosition(x, y, z, rx, ry, rz, distance, angleAttached, height)
local nrx = math.rad(rx);
local nry = math.rad(ry);
local nrz = math.rad(angleAttached - rz);
local dx = math.sin(nrz) * distance;
local dy = math.cos(nrz) * distance;
local dz = math.sin(nrx) * distance;
local newX = x + dx;
local newY = y + dy;
local newZ = (z + height) - dz;
return newX, newY, newZ;
end
Alles anzeigen
Aufgerufen wird die Funktion folgendermaßen:
float, float, float getAttachedPosition(float x, float y,float z, float rx, float ry, float rz, float distance, float angle, float height)
Anbei hab ich noch zwei Beispiele, weil Bilder und Code einfach mehr sagen:
EXAMPLE 1:
local player = getLocalPlayer()
local testMarker = createMarker(0, 0, 0, "cylinder", 1, 255, 255, 0, 50)
-- example1
function example1()
local vehicle = getPedOccupiedVehicle(player)
if (vehicle) then
local x, y, z = getElementPosition(vehicle)
local rx, ry, rz = getElementRotation(vehicle)
local cx, cy, cz = getAttachedPosition(x, y, z, rx, ry, rz, 10, 180, 0) -- 10m entfernt, 180° bedeutet hinten, 0 Meter Höhe in Relation zum Fahrzeug
setElementPosition(testMarker, cx, cy, cz) -- bind new position to marker
end
end
addEventHandler("onClientRender", root, example1)
function getAttachedPosition(x, y, z, rx, ry, rz, distance, angleAttached, height)
local nrx = math.rad(rx);
local nry = math.rad(ry);
local nrz = math.rad(angleAttached - rz);
local dx = math.sin(nrz) * distance;
local dy = math.cos(nrz) * distance;
local dz = math.sin(nrx) * distance;
local newX = x + dx;
local newY = y + dy;
local newZ = (z + height) - dz;
return newX, newY, newZ;
end
Alles anzeigen
Als Beispiel-Resource: example1.zip
EXAMPLE 2:
local player = getLocalPlayer()
local testMarker1 = createMarker(0, 0, 0, "cylinder", 1, 0, 255, 0, 50)
local testMarker2 = createMarker(0, 0, 0, "cylinder", 1, 0, 255, 0, 50)
local testMarker3 = createMarker(0, 0, 0, "cylinder", 1, 0, 255, 0, 50)
local testMarker4 = createMarker(0, 0, 0, "cylinder", 1, 0, 255, 0, 50)
local rot1 = 0
local rot2 = 90
local rot3 = 180
local rot4 = 270
-- example2
function example2()
local vehicle = getPedOccupiedVehicle(player)
rot1 = rot1 + 2
rot2 = rot2 + 2
rot3 = rot3 + 2
rot4 = rot4 + 2
if (rot1 > 360) then rot1 = 0 end
if (rot2 > 360) then rot2 = 0 end
if (rot3 > 360) then rot3 = 0 end
if (rot4 > 360) then rot4 = 0 end
if (vehicle) then
local x, y, z = getElementPosition(vehicle)
local rx, ry, rz = getElementRotation(vehicle)
local cx1, cy1, cz1 = getAttachedPosition(x, y, z, rx, ry, rz, 5, rot1, 0)
local cx2, cy2, cz2 = getAttachedPosition(x, y, z, rx, ry, rz, 5, rot2, 0)
local cx3, cy3, cz3 = getAttachedPosition(x, y, z, rx, ry, rz, 5, rot3, 0)
local cx4, cy4, cz4 = getAttachedPosition(x, y, z, rx, ry, rz, 5, rot4, 0)
setElementPosition(testMarker1, cx1, cy1, cz1) -- bind new position to marker 1
setElementPosition(testMarker2, cx2, cy2, cz2) -- bind new position to marker 2
setElementPosition(testMarker3, cx3, cy3, cz3) -- bind new position to marker 3
setElementPosition(testMarker4, cx4, cy4, cz4) -- bind new position to marker 4
end
end
addEventHandler("onClientRender", root, example2)
---------------------------------------------------------------------------------------
-- Calculate and attached position ----------------------------------------------------
---------------------------------------------------------------------------------------
function getAttachedPosition(x, y, z, rx, ry, rz, distance, angleAttached, height)
local nrx = math.rad(rx);
local nry = math.rad(ry);
local nrz = math.rad(angleAttached - rz);
local dx = math.sin(nrz) * distance;
local dy = math.cos(nrz) * distance;
local dz = math.sin(nrx) * distance;
local newX = x + dx;
local newY = y + dy;
local newZ = (z + height) - dz;
return newX, newY, newZ;
end
Alles anzeigen
Als Beispiel-Resource: example2.zip
So, simple aber vielleicht für irgendwas zu gebrauchen. *^^*
Liebe Grüße,
Samake