enter image description here

So, initially, I had points A and C. To get point B, I had to shoot a raycast down from point A and get the hit.point, which is point B now. Point D is somewhere between the raycast I shot from A and perpendicular to C. How do I find the point D.

  • 1
    \$\begingroup\$ I answered, but it seems that you don't even need a raycast. What purpose does it have? \$\endgroup\$ – trollingchar Aug 7 '19 at 14:13
  • \$\begingroup\$ Nothing important! I shouldn't have mentioned it! \$\endgroup\$ – MrRobot9 Aug 7 '19 at 14:55
  • \$\begingroup\$ You could use just any second point to define the line, not nessessary the result of raycast. \$\endgroup\$ – trollingchar Aug 7 '19 at 16:57

Ok, so your task is actually to project point onto line, defined by two points.


XY d = a + XY.Project (c - a, b - a);

XY is a 2-dimensional vector with these methods defined:

/// projection of vector v onto vector w
public static XY Project (XY v, XY w) => XY.Dot (v, w) / XY.Dot (w, w) * w;

/// dot product of vectors a and b
public static float Dot (XY a, XY b) => a.X * b.X + a.Y * b.Y;

Also you'll have to implement trivial +-*/ operators.


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.