### Robot in the room

*It does help if one knows trigonometric functions.*

I have encountered an interesting problem in connection with our game in the making. The name of the main character and the location has been changed due to privacy rights… 🙂

### The situation

“Mr. Robot” moves around in a rectangular “room”. He cannot stop, he can only move forward and turn on a certain arc. Mr. Robot must not hit the wall. After each unit of steps Mr. Robot calculates the direction in which he is going to progress.

### Rules of progress

The new direction depends on three parameters:

1. random (-1.0 – +1.0)

2. whether he was going left, right or straight on at the previous step (-1.0, 1.0 or 0.0)

3. forced turning angle (gradually changing between -1.0 – +1.0, then the same backwards)

The final turning angle is the sum of these, that is -3.0 <= angle <= +3.0.

If Mr. Robot used the parameters separately, he would move on these routes:

However, if he uses all of them, then he will move on a pleasantly winding route:

### The problem

The problem arises when he arrives near a wall. The rules then change as he cannot hit the wall. My first idea was to divert Mr. Robot from the wall when he is moving towards it, that is, I increased or decreased the turning angle so that he turned away from the wall as soon as possible. In order to achieve this, I used simple distance measurement: if he was too close to the wall, he turned away.

However, in the corners a competition occurred between the two walls, so Mr. Robot continuously tried to turn away from both walls, which resulted in hitting the wall. The solution was the use of trigonometric functions. They enable a much more accurate calculation of when it is necessary to intervene accurately – so he can go quite near the wall before he needs to be diverted. The competition was also solved.

### The solution

The analysis of the -20° directional angle is shown in the figure, Mr. Robot is moving north – north-east, and is coming near the north-eastern corner of the room. (Depending on the directional angle, only one section of the room needs to be examined.)

In case Mr. Robot gets nearer the wall then the diameter (*d*)of the turning circle, he examines whether he can progress by the above rules, or he should start to carry out the *emergency turning scenario*, which can happen in three cases:

1. (Blue and yellow arrows) If Mr. Robot gets near the corner, that is, his distance from the northern wall (*vPos*)) is shorter than *vFar ,*

**AND**his distance from the eastern wall (

*hPos*) ) is shorter than

*hFar*, then his direction of turning depends on whether Mr Robot is nearer the blue or yellow section.

In order to determine this, the following is needed:

*((hFar – hPos) / absSinAngle < (vFar – vPos) / absCosAngle)*

If the assumption is true, Mr. Robot was nearer the blue section, so he will turn right in the next step. If the assumption is not correct, then he was nearer the yellow section, so he will turn left.

2. (Green arrow) If he gets nearer the northern wall than *vNear*, then he will turn right.

3. (Red arrow) If he gets nearer the eastern wall than *hNear*, then he will turn left.

### The result

If neither assumption is realized, then Mr. Robot turns any way he wants, as he is not in the danger zone. 🙂

After following Mr. Robot as he was moving in this area, it can be seen that he used the emergency turning scenario skillfully:

## No Comments