playMaker

Author Topic: square root math / pinch gesture  (Read 4860 times)

Sjones

  • Full Member
  • ***
  • Posts: 203
square root math / pinch gesture
« on: August 29, 2012, 02:53:46 PM »
I am trying to implement a pinch gesture, so I got the position of finger 1 and 2 and was trying to get the distance float between then using the following calculation

however I cant find the square root math in the actions

is this the best way to go about it or am I doing it wrong?
« Last Edit: August 29, 2012, 03:54:15 PM by Sjones »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: square root math / pinch gesture
« Reply #1 on: August 30, 2012, 04:39:09 AM »
hi,

  Here we go:

http://hutonggames.com/playmakerforum/index.php?topic=2175.0

There should be more vector2 actions to cover the possible math around that. I'll see what I can do about that too.

bye,

 Jean

Sjones

  • Full Member
  • ***
  • Posts: 203
Re: square root math / pinch gesture
« Reply #2 on: August 30, 2012, 06:35:34 AM »
thank you very much, I couldnt sleep last night so I spent over an hour coming up with that XP, it was my first ever script and it was almost identical, mainly because I looked at your other math examples. it was a good experience, learning as I go :)

I was gona share it however as you got it there for everyone that is better :P

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: square root math / pinch gesture
« Reply #3 on: August 31, 2012, 03:44:25 AM »
Hi,

 Very good that you tried yourself. As an exercice, you could create an action that get the distance between two vector2, this way I am sure you it would be even more efficient in your fsm and states.

bye,

Jean

Sjones

  • Full Member
  • ***
  • Posts: 203
Re: square root math / pinch gesture
« Reply #4 on: September 02, 2012, 03:04:30 PM »
done what you suggested as I realised how easy it would be to modify the square root to have the full 2d distance calculation, this should help anyone who is looking to do any 2D distance mapping souch as click location or touch location (plus many more uses) I currently left it in the math section however here is the code.

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Math)]
[Tooltip("gives the distance between two points (2d location, e.g. screen)")]
public class 2DDistance : FsmStateAction
{
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmFloat floatVariableLocation1X;
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmFloat floatVariableLocation1Y;
[UIHint(UIHint.Variable)]
public FsmFloat floatVariableLocation2X;
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmFloat floatVariableLocation2Y;
public FsmFloat floatVariableDistance;
public bool everyFrame;

public override void Reset()
{
floatVariableLocation1X = null;
floatVariableLocation2X = null;
floatVariableLocation1Y = null;
floatVariableLocation2Y = null;
everyFrame = false;
}

public override void OnEnter()
{
DoFloatSqrt();

if (!everyFrame)
Finish();
}

public override void OnUpdate()
{
DoFloatSqrt();
}

void DoFloatSqrt()
{
floatVariableDistance.Value = Mathf.Sqrt(Mathf.Pow(floatVariableLocation2X.Value - floatVariableLocation1X.Value, 2) + Mathf.Pow(floatVariableLocation2Y.Value - floatVariableLocation1Y.Value, 2));
}
}
}

it uses a float for each X and Y for each location

Edit*
reason I never looked into 2 vectors is because screen location either works by X&Y seperatly or 3vector from what I understand
« Last Edit: September 02, 2012, 03:20:11 PM by Sjones »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: square root math / pinch gesture
« Reply #5 on: September 03, 2012, 04:37:20 AM »
Hi,

 cool, thanks for sharing :)

bye,

 Jean

smiffy

  • Junior Playmaker
  • **
  • Posts: 54
Re: square root math / pinch gesture
« Reply #6 on: September 03, 2012, 09:26:01 AM »
Hi SJones,

Just wondering how you got the locations of 2 fingers, I cant seem to get touch info for more than one finger at a time.

Regards,
Matt

smiffy

  • Junior Playmaker
  • **
  • Posts: 54
Re: square root math / pinch gesture
« Reply #7 on: September 03, 2012, 09:50:12 AM »
Scratch that just sorted it, had the finger index ints set to 1 & 2 rather that 0 & 1 :)

Thanks for the action, works great!

Cheers,
Matt

doppelmonster

  • Full Member
  • ***
  • Posts: 157
    • Grinder Games
Re: square root math / pinch gesture
« Reply #8 on: April 21, 2013, 10:17:57 AM »
This is an rather old topic, but im also scratching my head how i get two finger Ids from Touch event? Can someone enlighten me?

escpodgames

  • Hero Member
  • *****
  • Posts: 687
    • Assets
Re: square root math / pinch gesture
« Reply #9 on: April 21, 2013, 05:40:39 PM »
Have two touch event actions?!