Jump to content
• Advertisement

# Rotating an offset vector to point at a position

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

If you intended to correct an error in the post then please contact us.

## 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 this post

##### Share on other sites
Advertisement

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 this post

##### 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 this post

##### 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 this post

##### 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.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5
• Advertisement

• 12
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
633346
• Total Posts
3011451
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!