Rotating an offset vector to point at a position

This topic is 2632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

This is something I can't seem to figure out:

[attachment=5990:problem.png]

I have two 2D points P and Z, and two 2D vectors V and D. I need to find a rotation angle a such that Z lies on a line with the origin P+V[sup]R[/sup] and direction D[sup]R [/sup]where V[sup]R [/sup]is V rotated by a and the same for D.
Note that D doesn't have to be a vector, it can be an angle if that makes it easier.

I'd appreciate any help on this issue.

Share on other sites

This is something I can't seem to figure out:

[attachment=5990:problem.png]

I have two 2D points P and Z, and two 2D vectors V and D. I need to find a rotation angle a such that Z lies on a line with the origin P+V[sup]R[/sup] and direction D[sup]R [/sup]where V[sup]R [/sup]is V rotated by a and the same for D.
Note that D doesn't have to be a vector, it can be an angle if that makes it easier.

I'd appreciate any help on this issue.

Here's a crack at it, untested and could be completely wrong

theta = angle between V, D (given)
||P - Z|| / sin(theta) = ||V - P|| / sin(phi)
phi = arcsin(sin(theta) * ||V - P|| / ||P - Z||)
beta = arctan((Zy - Py)/(Zx - Px))
omega = 2 * pi - theta - phi - beta
Vr.x = ||V - P|| * cos(omega)
Vr.y = ||V - P|| * sin(omega)

Share on other sites
I tried out your solution pantaloons and it seems to work properly, but only when V.y is less than zero and when D is pointing directly to the right.

The restriction on D isn't really an issue, because V and D can always be rotated beforehand to make D point to the right, but the restriction on V is a bigger problem.

Still, thanks a lot!

Edit: Figured out how to make it work when V.y > 0. Kind of an unintuitive solution, but omega = beta - (PI - theta - phi), and Z.x should be mirrored relative to P.x when V.y > 0

Share on other sites
It should have been:
[color="#1C2837"]omega = pi - theta - phi - beta
[color="#1C2837"]If you didn't catch that. Although you are right I think quadrant issues get in the way with the right triangle approach to computing Vr. We can make it easier:

[color="#1C2837"]theta = angle between V, D (given)[color="#1C2837"]

||P - Z|| / sin(theta) = ||V|| / sin(phi)

phi = arcsin(sin(theta) * ||V|| / ||P - Z||)

[color="#1C2837"]omega = pi - theta - phi
[color="#1C2837"]

X = (Z - P)/||Z - P||

[color="#1C2837"]if(0 <= theta <= pi) [color="#1C2837"]Vr = ||V|| * R(omega) * X [color="#1C2837"]else Vr = ||V|| * R(-omega) * X
[color="#1C2837"]

[color="#1C2837"]

Where R(omega) is a standard 2x2 rotation matrix through angle omega. Notice that in fact, discounting the direction of vector D, there are two solutions that are equally rotated away from the vector Z - P, however you want the solution where D points towards Z which necessitates a negative rotation if -pi < theta < 0 (or turn(P, V, D) == right)).

Share on other sites
After looking over the solution again, I found that I didn't actually need beta or Vr. Once I have omega, I can compare it to the angle between V and Z-P to figure out how much rotation is needed.
It now works for all the cases I'll need.

Really appreciate it.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• Forum Statistics

• Total Topics
634112
• Total Posts
3015580
×