0
\$\begingroup\$

I am trying to write a script in C# to find the position of point D in 2D space in a specific scenario.

I know the (X,Y) position of point A, which never happens to be on the line that contains the line-segment BC. I also know that point D lies on the perpendicular line that passes through A and crosses BC at a point that we can call P. Also, I know the distance that D is from P. Se the image below:

enter image description here

Can anyone shed some line on which is the most optimal way of achieving that?

\$\endgroup\$
  • 1
    \$\begingroup\$ Does "never at the line BC" mean "never on the line segment BC" or "never on the line through both points B ad C"? What does "point D lays at the ..." mean? Your question is currently completely incomprehensible, so please clarify in correct English mathematical terms so as to be well defined. \$\endgroup\$ – Pieter Geerkens Oct 10 '15 at 5:31
  • \$\begingroup\$ Thanks for your feedback. I really meant " on the line" , not only on the line segment. I edited the question to clarify. Hope now it looks better in "English mathematical terms". Let me know. \$\endgroup\$ – Louis15 Oct 10 '15 at 15:02
  • \$\begingroup\$ Can you explain more on what is bc , is it a forward vector of some gameobject . I am asking this because the solution can become more simpler , without any mathematical calculations. \$\endgroup\$ – Hash Buoy Oct 22 '15 at 20:07
  • \$\begingroup\$ @HashBuoy thanks for the comment. Here, isactually neither a vector nor a GameObject. It is just a line segment in 2D space (although we could make it a vector by either B-C or C-B). What are your thoughts? \$\endgroup\$ – Louis15 Oct 24 '15 at 2:11
  • \$\begingroup\$ Then it might not help , anyways i had answered a similar problem . Don't know if this would help you , check this out - gamedev.stackexchange.com/a/108493/40457 \$\endgroup\$ – Hash Buoy Oct 25 '15 at 18:58
2
\$\begingroup\$

Finding point P is easy if you use the dot-product. In pseudo-code:

vec2  BC = C - B
vec2  BA = A - B
vec2  nBC = normalize( BC )
float dBP = dot( BA, nBC )
vec2  P = B + dBP * nBC

vec2  D = P + dist * normalize( A - P )
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

First, you need to find point P, it is the closest point on line BC. Knowing where P is, you can construct line AP. Going from that, finding point D on line AP is trivial (P + dirAP * dist).

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thank you very much for the link. Today's morning I already figured out the general process itself, but was struggling to implement it correctly. I will try the second answer from that link, since it seems like a quite efficient solution. \$\endgroup\$ – Louis15 Oct 10 '15 at 15:18
  • \$\begingroup\$ @Louis15 yep, it looks good. If you were interested in how it works there is an explanation on physics SE - it is actually doing projection AP onto BC. \$\endgroup\$ – wondra Oct 10 '15 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.