playMaker

Author Topic: Vector3 operators  (Read 6423 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14035
  • Official Playmaker Support
    • View Profile
Vector3 operators
« on: May 26, 2011, 04:59:25 AM »
Hi,

Wanted to add two vectors together and store it in a third vector, but did not find any actions to do so ( like the float operator), so rolled one.

 You can do the following:
Dot product,
Cross product,
Distance,
Angle,
Project,
Reflect,
Add,
Subtract,
Multiply,
Divide,
Min,
Max

Suggestions and/or comments welcomed :)



Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Vector3)]
[Tooltip("Performs most possible operations on 2 Vector3: Dot product, Cross product,Distance,Angle,Project,Reflect, Add, Subtract, Multiply, Divide, Min, Max")]
public class Vector3Operator : FsmStateAction
{
public enum Vector3Operation
{
DotProduct,
CrossProduct,
Distance,
Angle,
Project,
Reflect,
Add,
Subtract,
Multiply,
Divide,
Min,
Max
}

[RequiredField]
public FsmVector3 vector1;
[RequiredField]
public FsmVector3 vector2;
public Vector3Operation operation = Vector3Operation.Add;

[UIHint(UIHint.Variable)]
public FsmVector3 storeVector3Result;

[UIHint(UIHint.Variable)]
public FsmFloat storeFloatResult;

public bool everyFrame;

public override void Reset()
{
vector1 = null;
vector2 = null;
operation = Vector3Operation.Add;
storeVector3Result = null;
storeFloatResult = null;
everyFrame = false;
}

public override void OnEnter()
{
DoVector3Operator();

if (!everyFrame)
Finish();
}

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

void DoVector3Operator()
{
Vector3 v1 = vector1.Value;
Vector3 v2 = vector2.Value;

switch (operation)
{
case Vector3Operation.DotProduct:
storeFloatResult.Value = Vector3.Dot(v1,v2);
break;

case Vector3Operation.CrossProduct:
storeVector3Result.Value = Vector3.Cross(v1,v2);
break;

case Vector3Operation.Distance:
storeFloatResult.Value = Vector3.Distance(v1,v2);
break;

case Vector3Operation.Angle:
storeFloatResult.Value = Vector3.Angle(v1,v2);
break;

case Vector3Operation.Project:
storeVector3Result.Value = Vector3.Project(v1,v2);
break;

case Vector3Operation.Reflect:
storeVector3Result.Value = Vector3.Reflect(v1,v2);
break;

case Vector3Operation.Add:
storeVector3Result.Value = v1 + v2;
break;

case Vector3Operation.Subtract:
storeVector3Result.Value = v1 - v2;
break;

case Vector3Operation.Multiply:
// I know... this is a far reach and not usefull in 99% of cases.
// I do use it when I use vector3 as arrays recipients holding something else than a position in space.
Vector3 multResult = Vector3.zero;
multResult.x = v1.x * v2.x;
multResult.y = v1.y * v2.y;
multResult.z = v1.z * v2.z;
storeVector3Result.Value = multResult;
break;

case Vector3Operation.Divide: // I know... this is a far reach and not usefull in 99% of cases.
// I do use it when I use vector3 as arrays recipients holding something else than a position in space.
Vector3 divResult = Vector3.zero;
divResult.x = v1.x / v2.x;
divResult.y = v1.y / v2.y;
divResult.z = v1.z / v2.z;
storeVector3Result.Value = divResult;
break;

case Vector3Operation.Min:
storeVector3Result.Value = Vector3.Min(v1, v2);
break;

case Vector3Operation.Max:
storeVector3Result.Value = Vector3.Max(v1, v2);
break;

default:
break;
}
}
}
}
« Last Edit: October 09, 2011, 09:57:49 PM by jeanfabre »

tobbeo

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 186
    • View Profile
Re: Vector3 operators
« Reply #1 on: May 26, 2011, 05:54:25 PM »
Very nice, thank you Jean!

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • View Profile
    • FluidPlay Studios
Re: Vector3 operators
« Reply #2 on: May 27, 2011, 08:23:12 AM »
Ah, the joy of non-state-branching actions ^_^

Thanks Jean, very very handy!
--
Breno "MaDDoX" Azevedo
@brenoazevedo

giyomu

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 108
    • View Profile
    • blog
Re: Vector3 operators
« Reply #3 on: June 07, 2011, 06:48:04 PM »
ah a bit late but thanks jean , very handy  :)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14035
  • Official Playmaker Support
    • View Profile
Re: Vector3 operators
« Reply #4 on: September 26, 2011, 12:19:33 AM »
Hi,

  I noticed ( thanks to markinjapan) that the vector 3 operator action has an issue. It requires a the vector result field, but in some cases is actually not required, so this needs to be lifted.

 I have updated the code of this post ( basically removed the Required flag on that vector and the       [UIHint(UIHint.Variable)] as well, because that is wrong too in this context).

 Alex, if you are ok with the change, you might be able to squeeze that for 1.2.1

Bye,

Jean

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3570
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: Vector3 operators
« Reply #5 on: September 26, 2011, 05:05:39 PM »
Thanks Jean! I'll get this into 1.2.1.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14035
  • Official Playmaker Support
    • View Profile
Re: Vector3 operators
« Reply #6 on: October 09, 2011, 09:59:05 PM »
Hi,

 one more fix, I don't know what went through my head during the second correction, but I forgot to add the [UIHint(UIHint.Variable)] for storeVector3Result and storeFloatResult

 bye,

 Jean

FritsLyn

  • Full Member
  • ***
  • Posts: 191
    • View Profile
Cross product
« Reply #7 on: August 06, 2013, 11:11:09 PM »
I have a question about Cross product - not sure where to post that question, so for Google and any one with the same question, here is that tread:

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