Jayceon Posted July 5, 2017 Share Posted July 5, 2017 Hello dear reader! I'm searching a math function to get the perfect angle. I have one function, but it not whole. function getAngle(x1, y1, x2, y2) while true do while true do if math.atan2(x2, y2) - math.atan2(x1, y1) <= -math.pi then end end end while true do if math.atan2(x2, y2) - math.atan2(x1, y1) + 2 * math.pi > math.pi then end end return math.atan2(x2, y2) - math.atan2(x1, y1) + 2 * math.pi - 2 * math.pi end Thanks the replies. (The findRotation and other simple functions not good for me.) Link to comment
Gordon_G Posted July 6, 2017 Share Posted July 6, 2017 (edited) I don't understand, why findRotation is not good ? It works perfectly. Edited July 6, 2017 by Gordon_G Link to comment
Jayceon Posted July 6, 2017 Author Share Posted July 6, 2017 Yes.. works perfectly, but not for my code. (Don't get all directions (left/right - like highways exit) Link to comment
Moderators IIYAMA Posted July 6, 2017 Moderators Share Posted July 6, 2017 (edited) If you need the right and the left, you can add 90 or subtract 90. If you add/subtract 180, you will have the back of it. (it can also be lower than 0 or higher than 360, which is acceptable for using in mta functions) Edited July 6, 2017 by IIYAMA Link to comment
Jayceon Posted July 6, 2017 Author Share Posted July 6, 2017 We can not use findRotation, because we need left or right angles (it can be -10>n then it's a left turn, if it's 10<n then it's a right turn, if it's between -10 and 10 it is nothing) We have a table with precalculated values (the need field is what we need), but we get different numbers (got field) function getAngle(x1, y1, x2, y2) local angle = math.atan2(x1, y1) - math.atan2(x2, y2) if angle <= -math.pi then angle = angle + 2 * math.pi usedAlg = "hoz" else if angle + 2 * math.pi <= math.pi then angle = angle - 2 * math.pi usedAlg = "min" end end return -math.deg(angle) end pass fail || need: 13.532079523427 || got: -346.46792047657 fail || need: 368.80375654813 || got: 8.8037565481247 fail || need: 270 || got: -90 fail || need: -85.150542731867 || got: -85.150542731865 pass fail || need: 13.532079523427 || got: -346.46792047657 pass pass pass pass but it does not work Link to comment
Syntrax# Posted July 6, 2017 Share Posted July 6, 2017 38 minutes ago, Jayceon said: We can not use findRotation, because we need left or right angles (it can be -10>n then it's a left turn, if it's 10<n then it's a right turn, if it's between -10 and 10 it is nothing) We have a table with precalculated values (the need field is what we need), but we get different numbers (got field) function getAngle(x1, y1, x2, y2) local angle = math.atan2(x1, y1) - math.atan2(x2, y2) if angle <= -math.pi then angle = angle + 2 * math.pi usedAlg = "hoz" else if angle + 2 * math.pi <= math.pi then angle = angle - 2 * math.pi usedAlg = "min" end end return -math.deg(angle) end pass fail || need: 13.532079523427 || got: -346.46792047657 fail || need: 368.80375654813 || got: 8.8037565481247 fail || need: 270 || got: -90 fail || need: -85.150542731867 || got: -85.150542731865 pass fail || need: 13.532079523427 || got: -346.46792047657 pass pass pass pass but it does not work Lol basically what you currently got and what ya need is 360 - got. If you do 360 - the value you get with findrotation you get what you need Link to comment
Jayceon Posted July 6, 2017 Author Share Posted July 6, 2017 We have some pass marks, so i can not 360-value every single time Link to comment
Moderators IIYAMA Posted July 6, 2017 Moderators Share Posted July 6, 2017 (edited) I am still having a hard time to figure out what you need. But you want to know the differences between the rotations right?(positive and negative) If you calculate from both sides the rotation with findRotation, you can use this code to find the offset: local rotZ1, rotZ2 = 90, -90 local rotationOffsetZ = (rotZ1 - rotZ2 + 360) % 360; if (rotationOffsetZ > 180) then rotationOffsetZ = 360 - rotationOffsetZ else rotationOffsetZ = -rotationOffsetZ end iprint(rotationOffsetZ)-- -180 Edited July 6, 2017 by IIYAMA Link to comment
Jayceon Posted July 6, 2017 Author Share Posted July 6, 2017 wtf is rotationOffset ? i have the function, with X Y, X2, Y2 points on the map. I need the angle between them, but it can be negative or positive depending on left or right. n>10 -> right n<-10 -> left so it should work clockwise and counter-clockwise Link to comment
Syntrax# Posted July 6, 2017 Share Posted July 6, 2017 (edited) Just do this if rotation > 9 then "Right" if rotation < -9 then "Left" This should check if it is let or right Edited July 6, 2017 by Syntrax# Link to comment
Jayceon Posted July 6, 2017 Author Share Posted July 6, 2017 Example: local angle = math.deg(getAngle(currentNode.x - previousNode.x, currentNode.y - previousNode.y, nextNode.x - currentNode.x, nextNode.y - currentNode.y)) if angle > 10 then print("right") elseif angle < -10 then print("left") end Link to comment
Moderators IIYAMA Posted July 6, 2017 Moderators Share Posted July 6, 2017 12 minutes ago, Jayceon said: wtf is rotationOffset ? i have the function, with X Y, X2, Y2 points on the map. I need the angle between them, but it can be negative or positive depending on left or right. n>10 -> right n<-10 -> left so it should work clockwise and counter-clockwise That's what it does... If you say the angle between them, it can only be positive.... (in my opinion) But if you talk about offset, it can also be negative. And it freaking works for me. Just test it... Link to comment
Jayceon Posted July 6, 2017 Author Share Posted July 6, 2017 Please show me an example code, i shown you mine, just use it and please do an example getAngle function) Link to comment
Syntrax# Posted July 6, 2017 Share Posted July 6, 2017 17 minutes ago, IIYAMA said: That's what it does... If you say the angle between them, it can only be positive.... (in my opinion) But if you talk about offset, it can also be negative. And it freaking works for me. Just test it... He is true, you just have to check if it is positive or negative, and if the value is bigger than 10 Link to comment
Jayceon Posted July 6, 2017 Author Share Posted July 6, 2017 I just need an example please Link to comment
Moderators IIYAMA Posted July 6, 2017 Moderators Share Posted July 6, 2017 (edited) It is still not clear to us what your end result will be. This is what you have: X, Y, X2, Y2 Not a 3e position?(else there is nothing to compare against) Where do you get your second angle from? Do you want to add or subtract 10, to get the ones you want to get the new rotation from? Or do you need the pitch rotation instead? If you still can't explain what you need, then I recommend to you visualize it. Edited July 6, 2017 by IIYAMA Link to comment
Jayceon Posted July 6, 2017 Author Share Posted July 6, 2017 I have 4 positions. One X-Y pair for the start, one XY pair for the end of the line. I want to get the angle between the starting point and the endpoint of these. But. I need it to be negative if left and positive if right. Link to comment
Moderators IIYAMA Posted July 6, 2017 Moderators Share Posted July 6, 2017 So 2 positions. (not 4) Technically it can't be left or right unless you compare it with an start-point. Is 0 your start-point? And what is left and right to you? Link to comment
Jayceon Posted July 6, 2017 Author Share Posted July 6, 2017 It is a GPS Navig system, using nodes. Now. There is a start and an endpoint of these. At every endpoint a new node starts. This is everything i know, i want to know if the endpoint is to the left or the right from the start point. If you don't understand I can visualize it. for i, node in ipairs(gpsRoute) do local nextNode = gpsRoute[i + 1] local previousNode = gpsRoute[i - 1] if i > 1 and i < #gpsRoute then for k, v in pairs(node.neighbours) do if previousNode and nextNode and k ~= previousNode.id and k ~= nextNode.id then local nodeAngle = getAngle(node.x - previousNode.x, node.y - previousNode.y, nextNode.x - node.x, nextNode.y - node.y) if math.deg(nodeAngle) > 10 then print("right") break end if math.deg(nodeAngle) < -10 then print("left") end break end end end end Link to comment
Moderators IIYAMA Posted July 6, 2017 Moderators Share Posted July 6, 2017 function findRotation( x1, y1, x2, y2 ) local t = -math.deg( math.atan2( x2 - x1, y2 - y1 ) ) return t < 0 and t + 360 or t end function getRightOrLeft(oldX, oldY, startX, startY, endX, endY) local rotZ1, rotZ2 = findRotation( oldX, oldY, startX, startY ), findRotation(startX, startY, endX, endY) local rotationOffsetZ = (rotZ1 - rotZ2 + 360) % 360; if (rotationOffsetZ > 180) then rotationOffsetZ = 360 - rotationOffsetZ else rotationOffsetZ = -rotationOffsetZ end return rotationOffsetZ > 10 and "right" or rotationOffsetZ < -10 "left" or "nothing" end So you have 3 positions... seriously... pffffff your really making it hard to help you. -- try to use this in your code iprint(getRightOrLeft(previousNode.x, previousNode.y, node.x, node.y, nextNode.x, nextNode.y)) -- 1 Link to comment
Jayceon Posted July 6, 2017 Author Share Posted July 6, 2017 With a little transformation, but work. Thanks 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now