MATLAB Answers

Find the angle between two vectors, then move away...

19 views (last 30 days)
Alex
Alex on 14 Jan 2015
Commented: Alex on 14 Jan 2015
Hi all,
I am trying to do the following - find the angle between a vector and a point, then turn the vector to face 180 degrees the opposite direction from the direction of the point. However, I am struggling to find the correct angle.
Here is my approach so far...
x=[-1,1]; %new bot coordinate
xold=[-2,2]; %old bot coordinate
y=[0,0]; %obstacle
a=x-xold;
b=atan2(a(1),a(2));
c=atan2(y(1)-x(1),y(2)-x(1));
d=(b-c)-pi;
Any advice would be very greatfully recieved! Many thanks as always.
  1 Comment
Alex
Alex on 14 Jan 2015
To add more detail - the 'bot' knows its (x,y) position and direction as it knows its old position and its current one. It also knows the position of the obstacle. I would like it to move in a direction 180 degrees away from the obstacle when a proximity loop is triggered. I am struggling to find the angle to turn through..

Sign in to comment.

Accepted Answer

arich82
arich82 on 14 Jan 2015
I think you might have two errors:
First, atan2 takes arguments atan2(y, x), i.e. the reverse of what you seem to have.
Second, in your equation for c, your second argument is 'y(2)-x(1)', where I think you meant 'y(2)-x(2)'.
See if the below produces the desired results:
x0 = rand(2, 1); % previous bot location
x = rand(2, 1); % current bot location
y = rand(2, 1); % obstacle location
initial bot heading (away from x0), relative to [1; 0] axis (i.e. your 'b')
theta0 = atan2(x(2) - x0(2), x(1) - x0(1));
angle to obstacle, relative to [1; 0] axis (i.e. your 'c', corrected)
alpha = atan2(y(2) - x(2), y(1) - x(1));
new bot heading (away from obstacle), relative to [1; 0] axis (delta_ would be your 'd'; theta would be the new heading you want the bot to take)
delta_theta = alpha - theta0 - pi;
theta = theta0 + delta_theta;
plot the results red 'arrow' give old heading blue 'arrow' gives new heading, away from obstacle
hf = figure('WindowStyle', 'docked');
ha = axes;
hold(ha, 'all');
plot(x0(1), x0(2), '.r');
plot(x(1), x(2), '.g');
plot(y(1), y(2), '.b');
ht = text(x(1), x(2), '-->', 'rotation', theta0*180/pi, 'color', 'r'); % original heading
ht = text(x(1), x(2), '-->', 'rotation', theta*180/pi, 'color', 'b'); % new heading
axis([0, 1, 0, 1]);
legend('x0 (old bot position)', 'x (new bot position)', 'y (obstacle)', 'location', 'best')
%%%
  1 Comment
Alex
Alex on 14 Jan 2015
arich82, thank you so much!! Exactly what I was after. Really appreciate you taking the time. Much obliged.

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 14 Jan 2015
Edited: John D'Errico on 14 Jan 2015
Way too much work for a simple problem. There is NO need for trig here, so I'm not even going to try to figure out what you have in that undocumented mess of code. (Even if you were talking about reflection from a surface, that too can be done trig-less.)
If the vector X = [x,y] points in the direction you are currently moving, then the vector -X points in the opposite direction.
  3 Comments
Alex
Alex on 14 Jan 2015
Answered below. Thanks for your help anyway!

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by