From 409e2125c9d6ee72090c92dc94c54d0560a80c5d Mon Sep 17 00:00:00 2001 From: orwell96 Date: Wed, 22 Nov 2017 20:46:36 +0100 Subject: Make trackplacer align rails by any tracks, not just by tracks with the same nnpref This finally fixes the need to rotate atc rails and bumpers. Also prefers rotation that is closer to the player's look dir (placed bumpers will face the player) --- advtrains/helpers.lua | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) (limited to 'advtrains/helpers.lua') diff --git a/advtrains/helpers.lua b/advtrains/helpers.lua index d7b977e..f2e969f 100644 --- a/advtrains/helpers.lua +++ b/advtrains/helpers.lua @@ -151,26 +151,58 @@ function advtrains.yawToDirection(yaw, conn1, conn2) if not conn1 or not conn2 then error("given nil to yawToDirection: conn1="..(conn1 or "nil").." conn2="..(conn1 or "nil")) end - local yaw1=math.pi*(conn1/4) - local yaw2=math.pi*(conn2/4) - if advtrains.minAngleDiffRad(yaw, yaw1) if weird behavior + local yaw1=math.pi*(conn1/8) + local yaw2=math.pi*(conn2/8) + local adiff1 = advtrains.minAngleDiffRad(yaw, yaw1) + local adiff2 = advtrains.minAngleDiffRad(yaw, yaw2) + + if math.abs(adiff2)pi2 do + r1=r1-pi2 + end + while r1<0 do + r1=r1+pi2 + end + while r2>pi2 do + r2=r2-pi2 + end + while r1<0 do + r2=r2+pi2 + end local try1=r2-r1 - local try2=(r2+2*math.pi)-r1 - local try3=r2-(r1+2*math.pi) - if math.min(math.abs(try1), math.abs(try2), math.abs(try3))==math.abs(try1) then + local try2=r2+pi2-r1 + local try3=r2-pi2-r1 + + local minabs = math.min(math.abs(try1), math.abs(try2), math.abs(try3)) + if minabs==math.abs(try1) then return try1 end - if math.min(math.abs(try1), math.abs(try2), math.abs(try3))==math.abs(try2) then + if minabs==math.abs(try2) then return try2 end - if math.min(math.abs(try1), math.abs(try2), math.abs(try3))==math.abs(try3) then + if minabs==math.abs(try3) then return try3 end end -- cgit v1.2.3