pAndrei
pAndrei

Reputation: 383

Determine coordinates for an object at a specific distance and angle from a point

In a 2d space, I have an object of coordinates x1 and y1 and it is facing a specific direction , we'll call it "viewer". At start, the angle that measures the object rotation is 0, so the object starts always facing the same way. The angle is measured by a variable called yrot.

let D be the distance from the object determined by sqrt ((x1-x2)^2 + (y1-y2)^2), consider this distance known.

Now, knowing the viewer coordinates, the D distance and the yrot angle I want to determine the coordinates x2, y2 of the object that is at distance D from the viewer object's face.

To clarify this I will add a simple matrix to explain what I want:

Z 0 0

0 0 0

0 0 V

V is the viewer, V is facing towards Z. I am only interested if there is an object in front of V(at the specific distance). In other words, suppose Z is at distance D(known) from V, I am only interested if Z is an object, nothing else.

I believe the needed coordinates are x2 = x1 (+/-) d* sin yrot; y2 = y1 (+/-) d*cos yrot; I am not sure if this is the correct formula and it doesn't seem to be working. I am also unsure abut the signum of the second operand.

If anything is unclear, please leave comments and I will do my best to answer as fast as possible.

Thank you!

Later edit: || <- where is viewer oriented, yrot = 0; [] <- viewer

        = [] <- viewer yrot = 90 degrees. 

This should clarify what yrot is. Also, the object can rotate as much as I want to ( > 2 PI) and it can rotate both clock-wise and counter clock wise.

Charles Bretana's answer seems almost correct, I'm not sure if it covers overrotating(rotating > 360 degrees) and rotating in different directions.

Upvotes: 1

Views: 5723

Answers (1)

Charles Bretana
Charles Bretana

Reputation: 146541

Given a point A (ax, ay), the coordinates of the point B (bx, by) which is a distance d from A in the direction represented by angle t (where t: - pi < t < + pi and is measured counter clockwise from the positive x direction), would be:

 bx = ax + d*cos(t)
 by = ay + d*sin(t)

I'm not exactly sure if this approach matches your question, as It is not clear from your question what angle yrot represents. But if you determine yrot as simply the angle between the viewer's line of sight to the point B with the positive X-Axis, the above should work.

To cover overrotating, just take the yRot and subtract 2*Pi until the result is between - Pi and +Pi

Upvotes: 7

Related Questions