How to implement obstacle avoidance in Netlogo using the concept of two intersecting lines ( turtle heading vs wall made of patches)

非 Y 不嫁゛ 提交于 2020-06-23 09:53:29

问题


How can we convert the heading, of a Netlogo turtle, into a line equation (y = mx + c ) so that it can be compared to another line equation (eg that of patches representing a wall) ?

I need to convert the heading of a turtle into a line equation. Then compare the heading line equation with the line equation of a wall (which will have either a fixed x or a fixed y – depending on whether the wall is vertical or horizontal) There is an interception lines example code in the library (that I don’t understand that well) which is using moving segments. I think what I am after is a bit simpler. Perhaps a better starting point would be the code below that I found in one of the forums.

to-report calculate-line [ x y angle] let m tan angle let A m let B -1 let C ( - m * x + y ) report (list A B C) end

What must be done in order to 'read' the x and y values of the current turtle's heading and input those into the equations? Similarly, how can a set of patches that have been coloured to represent a wall, can be converted into a line equation that will be used to check possible intersection (between the heading line equation and wall line equation) ? For example I have create a line/segment of grey patches using the below code to represent the wall and given it is straight, there must be a way to convert this into a line equation (which will have a fixed y value - so that I will only have to 'test' the x value against the line equation of the turtle's heading. (In the simulated environment there are 4 walls in total - 2 vertical and 2 horizontal)

ask patches with [abs pycor = 10] [set pcolor grey] ask patches with [abs pycor = 11] [set pcolor grey]


回答1:


The x and y in the arguments of calculate-line refer to the current position of the turtle, and angle the angle between the turtle's current heading and the x axis, measured counter-clockwise with zero "east". I have no idea why the B is there, it is simply set and returned as -1, so a simpler version would be

to-report calculate-line [x y angle]
  let m tan angle  ; the slope
  let c y - m * x   ; the constant
  report (list m c)
end

and it would be called by a turtle. However, NetLogo measures a turtle's heading clockwise from the vertical axis, with zero "north", so we need to adjust the heading before taking its angle. The easiest way of doing this is to add 90 degrees to the heading, rotating everything clockwise, and, since that would make a turtle traveling (say) NE travel SE, thus reversing the sign of the slope, we need to take the negative of the result. Finally, if we let the turtle itself determine the line, it can simply fill in its own location and heading.

We then end up with

to-report calculate-line
  ; We need to remember that NetLogo measures 
  ; heading from the vertical axis, while the
  ; tan function assumes an angle measured from
  ; the horizontal axis. Thus we rotate the
  ; heading 90 degrees before taking the tan.
  ; also we deal separately with headings of 
  ; 0 and 180, where the slope is "infinite",
  ; and headings of 90 and 270, where the slope
  ; is zero.
  let m 0
  (ifelse (heading mod 180 = 0) [set m 10e16] ; m is "infinite"
  (heading mod 90 = 0) [set m 0]
  [set m (- tan (heading + 90))])
  let c ycor - m * xcor
  report (list m c)
end

ask one-of turtles [show calculate-line]

This way, the turtle fills in its own coordinates and heading. Note that we treat headings along the axes as special cases, and make and "infinite" (vertical) slope a very large number.

If you have four walls, you will need to be careful to test that the line does not intersect one wall beyond the limits of one perpendicular to it. Also, if you are using the pxcor and pycor of the wall to define its position, remember that the intersections will occur in the middle of the wall, i.e., the middle of the patch.



来源:https://stackoverflow.com/questions/60646674/how-to-implement-obstacle-avoidance-in-netlogo-using-the-concept-of-two-intersec

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!