Find the angle between two vectors, then move away...
Show older comments
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
Accepted Answer
More Answers (1)
John D'Errico
on 14 Jan 2015
Edited: John D'Errico
on 14 Jan 2015
0 votes
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
on 14 Jan 2015
John D'Errico
on 14 Jan 2015
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
on 14 Jan 2015
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!