Calculating intersecting angles from headings on a -180 to 180 degree plot

179 Views Asked by At

I’m trying to program an agent to avoid barriers that it hits. I have access to the heading of the agent as it meets the barrier, which are recorded in positive and negative degrees up to 180 (as shown here, with example headings of agent). I also have access to the coordinate points of the barriers, and thus can also calculate the ‘heading’ of the barrier as well.

My aim is to calculate the angles in which the barrier and agent meet, so that I know how much to change the heading of the agent so that it moves along the barrier in the direction of the largest supplementary angle, like shown here - the new heading is obtained by adding sometimes negative or positive numbers on to the original heading.

I’ve currently been using the inverse tan of the start and end coordinates of the barrier (to get its heading and get the theta angle), getting the theta angle of the agent meeting the barrier, and then calculating the angle that needs to be added to the heading for the agent to start travelling along the barrier, however, obviously since this is being implemented in a program, I can’t seem to come up with one universal maths formula which can account for all the combinations of headings of the barrier and agent meeting? And was, therefore, wondering if anyone had any advice on how to deal with these calculations when using headings that are on a positive/negative scale of 180 degrees?