Richtung zu Punkt auf der Map ermitteln.

  • Hallo zusammen,


    ich habe zwei Punkte direkt in der Spielwelt. Nun möchte ich die nötige Rotation für Punkt1 eins ermitteln, damit er in Richtung Punkt2 blickt.


    Ich habe es bereits mit einfacher Trigonometrie versucht, allerdings ohne großen Erfolg. Die Richtung passt nicht und Punkt1 dreht sich wild im Kreis, sobald Punkt2 seine Position verändert. Es geht mit erstmal nur um die Y-Rotation.
    [expander]
    [lua]
    local gx,gy,gz = getWorldTranslation(grauerPunkt);
    local rx,ry,rz = getWorldTranslation(roterPunkt);


    local x = rx-gx;
    local z = rz-gz;


    local angle = math.atan2(z,x);
    angle = math.rad(angle);
    [/lua]
    [/expander]



    Ich stehe echt auf dem Schlauch, vielleicht hat jemand von euch eine Idee :)


    Danke!

  • Hi,


    Ich hab das beim Mix Feeder auch gehabt. Dort konnte ich es aber lösen, auch wenn das sicher nicht der beste Weg war.
    Dort hatte ich zusätzliche Helper eingebaut, die man so drehen muss wie die Hauptrichtung ist, sprich X, Y, Z. Die Y
    Rotation berechne ich dann über die Winkelfunktion.



    Habe das grad mal getestet mit 2 einfachen "stangen", geht soweit auch ganz gut. Aber nur solange die 2. Stange in der z position
    hinter der 1. liegt.
    Sprich solange der rote Punkt in Z vor dem grauen liegt.


    Aber vielleicht hilft es dir ja etwas weiter.


    Die Y Rotation muss ich aber auch je nach größe mit *(-1) umrechnen. Je nachdem wie Point1 in Y gedreht ist. Habe ich beim Mix Feeder auch schon gemacht. Kommt ja aber auch darauf an, was du genau vor hast, vielleicht bringt es dich aber weiter.


    Wenn du das ganze in einer TG verbaust, vielleicht ist es dann auch ein Weg wenn du dir mit worldToLocal die Position innerhalb der TG
    ausrechnen lässt.
    [lua]
    local x, y, z = getWorldTranslation(idPoint1);
    local x1, y1, z1 = getWorldTranslation(idPoint2);


    local wx, wy, wz = worldToLocal(idTestTG, x,y,z);
    local wx1, wy1, wz1 = worldToLocal(idTestTG, x1,y1,z1);
    [/lua]



    So habe ich es im GE grad mal getestet.
    [lua]
    local idPoint1 = 384116
    local idPoint2 = 384118


    local wx, wy, wz = getWorldTranslation(idPoint1);
    local wx1, wy1, wz1 = getWorldTranslation(idPoint2);


    local a, b, c = wx-wx1, wy-wy1, wz-wz1;


    ab = math.atan (a/c);


    rx, dhY, rz = getRotation(idPoint1);

    if dhY ~= nil and dhY < 0 then
    if ab > 0 then
    ab = ab*(-1);
    end;
    elseif dhY ~= nil and dhY > 0 then
    ab = math.abs(ab)
    end;


    setRotation(idPoint2, rx, ab, rz);
    [/lua]

  • Ich hatte in LS08 beim Häcksler mit der Autopipe die zum Anhänger zielt das selbe Problem.


    Hier ein Code Ausschnitt:


    Interessant ist der obere Teil, p1yr gibt die y-rotation des nodes an der zum self.pipeentladepoint2 node schauen soll.


    Weiter gehts dann mit der Begrenzung der y-rotation damit die Pipe nicht in die Kabine rauscht.


    Der untere teil ist für eine 2-teilige Auswurfklappe.


    [lua]if self.pipeAP then

    if table.getn(self.trailerID) > 0 and not self.waitingForDischarge then

    local i = 1;
    if self.trailerID[1].fillLevel >= self.trailerID[1].capacity and table.getn(self.trailerID) > 1 then
    i = 2;
    end;
    self.trailerIndex = i;

    local tx,ty,tz = getWorldTranslation(self.trailerID[i].rootNode);
    if self.trailerID[i].fillNode ~= nil then
    tx,ty,tz = getWorldTranslation(self.trailerID[i].fillNode);
    end;
    local xw,yw,zw = worldToLocal(self.rootNode, tx, ty, tz);
    local pex,pey,pez = getTranslation(self.pipeentladepoint2);
    setTranslation(self.pipeentladepoint2, xw, pey, zw);

    local pxr,pyr,pzr = getRotation(self.rotationParts.rotationPart1.index);
    local p1yr = math.atan2(-xw,-zw);
    local rotY = pyr;
    if (p1yr - self.currentRotY) > 0.001 and pyr < self.rotationParts.rotationPart1.minRot[2] then
    rotY = math.min(pyr + (self.rotationParts.rotationPart1.minRot[2]-self.rotationParts.rotationPart1.maxRot[2])/self.rotationParts.rotationPart1.rotTime * dt, p1yr)
    elseif (p1yr - self.currentRotY) < -0.001 and pyr > self.rotationParts.rotationPart1.maxRot[2] then
    rotY = math.max(pyr - (self.rotationParts.rotationPart1.minRot[2]-self.rotationParts.rotationPart1.maxRot[2])/self.rotationParts.rotationPart1.rotTime * dt, p1yr)
    end;
    self.currentRotY = rotY;
    setRotation(self.rotationParts.rotationPart1.index, pxr, rotY, pzr);

    local kxr3,kyr3,kzr3 = getRotation(self.rotationParts.rotationPart3.index);
    local kxr4,kyr4,kzr4 = getRotation(self.rotationParts.rotationPart4.index);
    --local terrainDeltaY = ty - getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, tx, 0, tz);
    local px,py,pz = getWorldTranslation(self.rotationParts.rotationPart3.index);
    local kz =Utils.vector2Length(tx-px, tz-pz);
    --local kz = math.sqrt(xw^2+zw^2);

    local kxr = math.atan2(ty-py,kz);
    local kxr3 = kxr / 2;
    --print(kxr);
    setRotation(self.rotationParts.rotationPart3.index, kxr3, kyr3, kzr3);
    setRotation(self.rotationParts.rotationPart4.index, kxr3, kyr4, kzr4);
    end;
    else[/lua]

  • Vielen Dank, das Problem ist gelöst :love:


    [lua]
    local wx, wy, wz = getWorldTranslation(g_currentMission.player.rootNode);
    local wx1, wy1, wz1 = getWorldTranslation(self.i3dNode);
    local a, b, c = wx-wx1, wy-wy1, wz-wz1;
    ab = math.atan2 (a, c);

    setRotation(self.i3dNode, 0, ab, 0);
    [/lua]


    So bin ich jetzt zur Lösung gelangt. Komisch nur, das math.atan() nur zuverlässig funktioniert, wenn der Targetpoint eine höhere z-Position hat als das Objekt selbst. math.atan2 funktioniert durchgehend zuverlässig.



    Danke für die schnelle Hilfe jetzt kanns weiter gehen :D

  • Das klappt echt richtig gut mit math.atan2(), dachte jetzt das ich das direkt am MixFeeder auch mal teste, so hätte ich noch mehr Möglichkeiten auch Splines zu unterstützen die bergauf/bergab gehen. Aber leider klappt das beim Mix Feeder und einer Spline doch nicht.


    Du drehst ja ein Objekt zum anderen hin, sprich deine self.i3dNode dreht sich immer zum Player hin. Ich muss hingegen 2 Spline Punkte ermitteln und errechnen in welchem Winkel mein Objekt stehen muss um beide Punkte zu verbinden. Klappte mit math.atan2() nur wenn ich die rotation ebenfalls dann immer mit *(-1) gesetzt habe.


    Werde das nochmal weiter testen, denn es wäre eine echte Erleichterung wenn es klappen würde.

  • :patsch:
    Das ist immer das Problem wenn man nicht weiter nachdenkt.


    Es funktioniert jetzt auch super bei mir. Mein Fehler war immer die x und z Roation. Ich hatte das jetzt mit math.atan2() getestet, aber nach wievor die x und z Rotation angenommen wie sie von meinen "helpern" kommen. Somit wird x und z halt auch gesetzt, das führt dann dazu das math.atan2() auch nicht korrekt funktioniert hat. Das habe ich nun einfach mal so geändert wie es Fiat80-90DT auch gepostet hat. Musste dann nur meine die a, b, c Berechnung umdrehen und schon steht mein Feeder genau so da wie er soll. Ohne zusätzlichen Einbau von irgendwelchen Helpern um die Richtung zu bestimmen.


    Und auch wenn Fiat das Thema eröffnet hat, so muss ich Danke sagen, denn ohne seinen Code zur Lösung, hätte ich es nicht umgeändert. :thumbup:

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!