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

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 Kommentar

Alex
Alex am 14 Jan. 2015
Bearbeitet: Alex am 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..

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

arich82
arich82 am 14 Jan. 2015

0 Stimmen

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 Kommentar

Alex
Alex am 14 Jan. 2015
Bearbeitet: Alex am 14 Jan. 2015
arich82, thank you so much!! Exactly what I was after. Really appreciate you taking the time. Much obliged.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

John D'Errico
John D'Errico am 14 Jan. 2015
Bearbeitet: John D'Errico am 14 Jan. 2015

0 Stimmen

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 Kommentare

Alex
Alex am 14 Jan. 2015
For this code, I need the angle of rotation, as my inputs to the 'bot' are only angle and magnitude unfortunately! I am also need the bot to go in the direction opposite to the object rather than the opposite directional vector. I hope that makes sense. I have cleared up the code somewhat..
Still not clear, but better.
Given a vector [x,y], the angle at which it points is
atan2(y,x)
So if you have a direction vector, you trivially have the angle at which you would move.
As for the object, do you want to know a direction normal (orthogonal) to the surface of that object? If you do, then you have not given me that information, only the direction you are moving.
I'm not sure what you think you have with this:
y=[0,0]; %obstacle
But that appears to be simply a location, NOT a normal vector in any form, or anything that would yield that information.
Alex
Alex am 14 Jan. 2015
Answered below. Thanks for your help anyway!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Object Properties finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 14 Jan. 2015

Kommentiert:

am 14 Jan. 2015

Community Treasure Hunt

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

Start Hunting!

Translated by