playMaker

Author Topic: Ferr2d and Playmaker  (Read 1825 times)

treborjones

  • Playmaker Newbie
  • *
  • Posts: 24
Ferr2d and Playmaker
« on: May 20, 2017, 10:25:33 AM »
Hi there!

I'm using Ferr2d and playmaker and wondered if I could use the 'GetdistanceFromPath' functionality in ferr2d. Basically, I want to slow the player down the further away from the spline they are, so need to measure that distance. It might be a Ferr2d question, so have posted on that forum too.

Any help much appreciated!

Cheers,

Bob.

tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
Re: Ferr2d and Playmaker
« Reply #1 on: May 21, 2017, 10:25:19 PM »
Is GetdistanceFromPath a function (method)? Can you link to your forum post on Ferr2d? You may need a custom action, or maybe not. Its hard to know because I dont know much about Ferr2d api.

treborjones

  • Playmaker Newbie
  • *
  • Posts: 24
Re: Ferr2d and Playmaker
« Reply #2 on: May 22, 2017, 03:34:41 AM »
Here's the link to the ferr2d Unity forum

https://forum.unity3d.com/threads/ferr2d-terrain-tool.204436/page-24

There's not much extra info there, but hoping someone might pipe up soon.

'GetDistanceToPath' doesn't show as a Method to call by default in playmaker. it's just something in the Ferr 2D_path.CS that I noticed and was hoping I might be able to use/access that functionality somehow! My knowledge of using Playmaker in this way is pretty limited. :-)

This is the code in question.

   
Code: [Select]
/// <summary>
    /// Gets the smallest distance to the provided path
    /// </summary>
    /// <param name="aPoint">The point to check from.</param>
    /// <returns>Smallest distance! float.MaxValue if none found</returns>
public static float GetDistanceFromPath(List<Vector2> aPath, Vector2 aPoint, bool aClosed) {
if (aPath.Count <= 1) return float.MaxValue;

float dist  = float.MaxValue;
int   count = aClosed ? aPath.Count : aPath.Count-1;
for (int i = 0; i < count; i++)
{
int     next  = (i+1) % aPath.Count;
Vector2 pt    = GetClosetPointOnLine(aPath[i], aPath[next], aPoint, true);
float   tDist = (aPoint - pt).SqrMagnitude();
if (tDist < dist)
dist = tDist;
}
return Mathf.Sqrt(dist);
}

Cheers!

treborjones

  • Playmaker Newbie
  • *
  • Posts: 24
Re: Ferr2d and Playmaker
« Reply #3 on: May 22, 2017, 08:51:04 AM »
A codey chum resolved this for me.

First I learnt about Methods and Static Methods, and that Playmaker appeared to only show Methods as an option from the Call Method functionality.

My coder chum then resolved a couple more issues related to World space/Local space.

Here's the snippet just in case anyone else is ever trying to do the same thing.

Code: [Select]
/// <summary>
/// Gets the smallest distance to the provided path
/// </summary>
/// <param name="aPoint">The point to check from.</param>
/// <returns>Smallest distance! float.MaxValue if none found</returns>
public float GetDistanceFromPathDynamic(Vector3 point) {
if (pathVerts.Count <= 1) return float.MaxValue;

Vector3 localPoint = transform.InverseTransformPoint(point);
Vector2 aPoint = new Vector2( localPoint.x, localPoint.y );

float dist  = float.MaxValue;
int   count = closed ? pathVerts.Count : pathVerts.Count-1;
for (int i = 0; i < count; i++)
{
int     next  = (i+1) % pathVerts.Count;
Vector2 pt    = GetClosetPointOnLine(pathVerts[i], pathVerts[next], aPoint, true);
float   tDist = (aPoint - pt).SqrMagnitude();
if (tDist < dist)
dist = tDist;
}
return Mathf.Sqrt(dist);
}

Ta,

Bob.