How do I (Math problem) calculating a value?

  • Anyone here any good at math?

    I need to calculate the distance between an object and a point in a diagonal line between 2 other objects.

    A bit hard to explain but i will try.

    The point at the top (DTC) is an object. I need to calculate the Y distance between this object and a point straight below in the diagonal line.

    The diagonal line would be calculated based on 2 points. the lower left point and the upper right point. Anyone comprehend what I'm trying to do?

    The DTC object can move in both X and Y axis so the distance value can be variable.

    Any help appriciated.

  • I'm not good at maths but heres a round about method.

    Create a sprite 1 pixel in size at the upper right point, then run a loop the distance between the upper right point and the lower left point in that direction and move it 1 pixel each loop. Stop the loop when its y coordinate matches the DTC's. Now get the distance between the sprite and the DTC.

    Also i think this is called "raycasting". Maybe do a search for that term.\

  • Ethan thanks, but that's not really it i suppose. I'm googling a little bit and might be on to something. I need to calculate the X&Y position of a point in the slope, based on the object.... when i have that value, it's not that hard to calculate the distance between that point and the object.

  • You can find a equation by googling the distance between a line and a point.

    If a and b are the two points of the diagonal and p is the other point then the distance from p to the line would be:

    Abs(((P.x-a.x)*(b.y-a.y)-(p.y-a.y)*(b.x-a.x))/distance(a.x,a.y,b.x,b.y))

    Edit:

    Oops you wanted y distance, that would be this:

    Abs(Lerp(a.y, b.y, (P.x-a.x)/(b.x-a.x))-p.y)

  • Good example, not so useful in this case since i was looking for a formula, but quite impressive how u solved it....

    That's just brilliant. Exactly what i was looking for.

    Thanks both. I should have payed more attention in math class.

  • You can find a equation by googling the distance between a line and a point.

    If a and b are the two points of the diagonal and p is the other point then the distance from p to the line would be:

    Abs(((P.x-a.x)*(b.y-a.y)-(p.y-a.y)*(b.x-a.x))/distance(a.x,a.y,b.x,b.y))

    Edit:

    Oops you wanted y distance, that would be this:

    Abs(Lerp(a.y, b.y, (P.x-a.x)/(b.x-a.x))-p.y)

    Everything works perfect, but now i noticed that 'p' needs a negative distance value if below the line. How would i go about getting that? hmmmmm

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Take away the ABS().

  • Ahhh.. simple as that!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)