Simplifying equations into the expressions editor

patrickbeardmorepatrickbeardmore Member Posts: 11
edited August 2012 in Working with GS (Mac)
I am trying to solve a simple maths problem, to find the closest point on a line to a point. Here's a diagram, where x4,y4 is unknown (http://i.imgur.com/CG17x.png)

The answer is readily available, for instance here: http://stackoverflow.com/questions/3120357/get-closest-point-to-a-line

I am using the one that starts "Find the slope of a1 ........"

I want an actor to accelerate towards the point x4,y4, but because GameSalad has a very limited expression editor I am having trouble inputting into the x and y boxes.
What techniques do people use to do more complicated maths and then using attributes apply them later.

Comments

  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    edited August 2012

    @patrickbeardmore

    Hi Patrick, interesting problem which I can hopefully help to clear the fog a little...

    So firstly, let's see if I'm understanding you correctly, you will have several points near that line and you want to find out which point is the closest to the line; so you need to measure the distance off all the points to the line, then put those into attributes to compare?

    The distance is no problem if you use magnitude with an offset but still leaves the problem of finding those intersection points which you'd need for that...

    As it is, with the line at an angle, I think it might be beyond the capabilities of GSC, as I think the solution would involve finding the angle between two lines (so if you knew the angle between x2,y2 to x1,y1 and x2,y2 to x3,y3 you could add 90º and so get the angle of x3,y3 so it's at right angles to the line... this still leaves the problem of where that line intersects before you can get the length of line....) Maybe that's the wrong approach, but anyhow, I still think there aren't enough functions in GSC to handle this problem as it is; but I'd stand to be corrected, as they say.

    So, as far as I can see, there is only one way to get those intersection coordinates (in this one case, x4,y4) – and that would be if the line was perfectly horizontal....

    Then they would all be easy to know, then you could use magnitude with the offsets to find each distance, then compare....

    ""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

  • patrickbeardmorepatrickbeardmore Member Posts: 11
    @gyroscope

    I'm very grateful for your comment and thank-you for taking an interest. If I could just clarify my question a little, I am less interested in the specifics of solving this problem (although I would be interested in knowing that too!) but rather how people are using GS to solve slightly trickier maths problems.

    This problem involves a simultaneous equations, but I can't spot an obvious way of solving these within GS at the moment.
  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    edited August 2012

    No worries, Patrick; and you're welcome! :-)

    As far as my knowledge of all the functions in GSC, there are some quite powerful expressions, but not nearly enough for trigonometrical problems; however, some relatively straightforward algebra/equations should be within the capabilities of GSC utilizing some of the built-in expressions and attributes. I hope that's of some help.

    ""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

  • patrickbeardmorepatrickbeardmore Member Posts: 11
    It's the idea of doing multi-step calculations I am interested in. Writing everything in one line is confusing and also limiting, but having the ability to carry out calculations with intermediate variables would enable a lot of functionality.

    Perhaps a behaviour called "math box" is necessary
  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    edited August 2012

    I think I'd need an example to maybe try to help in a practical sense, but as far as I can see from your explanation, some relatively straightforward multi-step calculations could all be done using attributes as well as the mathematical expressions of course, as well as possibly a few of the functions (and all of these put into a Group behaviour and titled Math Box if you wanted); the solution you might want to display in an actor perhaps, using Display Text.

    One thing I will say: there's nothing wrong with using GSC for apps other than games of course, but to try to push it's boundaries too far where utilities are concerned, you might come up to a brick wall or two, as GSC is geared mainly for games...

    ""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

  • CodeMonkeyCodeMonkey Head Chef, Member, PRO Posts: 1,803
    Here are some quick equations.
    m = tan(angle) = ∆y/∆x (change in y divided by change in x)
    With that you can basically derive these equations.

    Line one:
    m=(y1-y2)/(x1-x2)=tan(angle)=(y4-y1)/(x4-x1)
    y4=tan(angle)*(x4-x1)+y1=(y1-y2)/(x1-x2)*(x4-x1)+y1
    angle= atan((y1-y2)/(x1-x2))

    Line created from the unknown point(x4,y4) and the other point(x3,y3)
    tan(angle+90)=(y4-y3)/(x4-x3)
    y4 = tan(angle+90)*(x4-x3)+y3


    Combining the equations:
    (y1-y2)/(x1-x2)*(x4-x1)+y1 = tan(angle+90)*(x4-x3)+y3

    That should get you started.
  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    edited August 2012

    Excellent stuff, @CodeMonkey !

    not understanding most though:

    including both these lines:

    y4=tan(angle)*(x4-x1)+y1=(y1-y2)/(x1-x2)*(x4-x1)+y1
    and
    y4 = tan(angle+90)*(x4-x3)+y3 seem at odds with each other; and I'm not understanding why a coordinate point on its own (y4) has any bearing on a tangent of an angle in both cases....don't know what I'm missing there...

    But mainly in both, the coordinate y4 is derived from an equation which includes use of the x4 coordinate which isn't known; so I'm a bit confused there as well...

    ""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

  • patrickbeardmorepatrickbeardmore Member Posts: 11
    edited August 2012
    Very grateful for this @codemonkey.

    Am keen to turn this into formulae I can insert into GS (in particular I want something to pop into the x and y coordinates of the accelerate towards behaviour).

    Have tried rearranging for x4 (http://wolfr.am/RESyWI) but to no avail (answer is extremely long!). How do you recommend I go about using the equation?
  • patrickbeardmorepatrickbeardmore Member Posts: 11
    edited August 2012
    I am pleased to say I have cobbled together an answer to my question!

    Source: http://nic-gamedev.blogspot.fr/2011/11/using-vector-mathematics-and-bit-of_08.html

    Define the points as in the picture: http://i.imgur.com/CG17x.png

    1) Constrain attribute:
    distance_along = ((x3-x1)*(x2-x1)+(y3-y1)*(y2-y1))/(pow(x2-x1,2)+pow(y2-y1,2))
    2) Then:
    x4 = x1 + distance_along * (x2-x1)
    y4 = y1 + distance_along * (y2-y1)
  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    edited August 2012
    I am pleased to say I have cobbled together an answer to my question!

    Source: http://nic-gamedev.blogspot.fr/2011/11/using-vector-mathematics-and-bit-of_08.html

    Define the points as in the picture: http://i.imgur.com/CG17x.png

    1) Constrain attribute:
    distance_along = ((x3-x1)*(x2-x1)+(y3-y1)*(y2-y1))/(pow(x2-x1,2)+pow(y2-y1,2))
    2) Then:
    x4 = x1 + distance_along * (x2-x1)
    y4 = y1 + distance_along * (y2-y1)
    Hi Patrick, I don't understand how this is meant to work out to the solution you're after.

    1) don't understand why you're using a Constrain attribute on fixed numbers

    2) don't understand why you're using the function power

    3) doesn't make sense to multiply the distance between x1 and x3 with x1 and x2 let alone adding a horizontal distance to a vertical distance (y3-y1); then dividing by the addition of an x distance and a y distance added together, both powered by 2... ??? just really doesn't mean anything in this instance, as far as I can see, sorry to say...

    4) so your 2) doesn't add up & doesn't work out for what you're after....

    Example: let's call x1,y1 10,50 and x2,y2 40,30 and x3,y3 15,10

    Ignore the constrain attribute for the moment (P.S "const" in your link example means constant, not constrain):

    distance_along = (5*30) + (-40*-20)/(30*30) + (-20*-20) = 150 + 800/900 + 400 =950/1300 = 0.7307etc

    x4 = 10 + 0.7307etc * 20 = 214 approx.

    y4 similarly wrong

    All in all, nothing makes sense to me with your equations....


    Finally, your second source (link) shows a straight line, which as I suggested earlier would be straightforward in GSC to get the x4,y4 coordinates and so then the distance between x3,y3 and x4,y4, as you wanted:

    x4 = x3
    y4 = y1 or y2

    So in my example coordinates, x4 is 15, and y4 whatever is decided for y1 or y3, now both equal being a straight line)

    Then use magnitude with an offset using x3,y3 and x4,y4, then you have your solution being the distance between the point and where a vector from it would intersect with the line at 90º.

    ----------------------

    Edit: I've been thinking on, and suddenly realised: you don't even need the magnitude expression to find out the distance of your point to a horizontal line........

    When you've got y4 (being the same as y1 or y2) you just subtract y3 from it, and you've got the distance; simples!! :-)

    ""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

Sign In or Register to comment.