playMaker

Author Topic: Constrain  (Read 7823 times)

giyomu

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 108
    • blog
Constrain
« on: June 07, 2011, 09:44:16 PM »
hi all , I am recently use more playmaker on a project i am on ( almost exclusively)

and wanted to share a little action..XD

i use that often with animated stuff , etc..may not super optimized or perfect for all fit ...
simple enough 0 no constrain 1 full constrain and you can eventually put whatever number you want in the both vector.

here we go >

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Transform)]
[Tooltip("Constrain position and rotation to any other object, 0 for no constrain, 1 for full constrain")]
public class Constrain : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;
[RequiredField]
[UIHint(UIHint.FsmGameObject)]
public FsmGameObject target;
public FsmBool doRotation;
public FsmVector3 rotationConstrain;
public FsmBool doPosition;
public FsmVector3 positionConstrain;
public FsmVector3 offsetPosition;
public bool everyFrame;

public override void Reset ()
{
gameObject = null;
target = null;
doRotation = null;
doPosition = null;
rotationConstrain = null;
positionConstrain = null;
offsetPosition = null;
everyFrame = false;
}

public override void OnEnter ()
{
DoConstrain();

if (!everyFrame)
Finish();
}

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

void DoConstrain()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
return;

var tr = target.Value.transform;
if(tr == null)
return;

// perform constraining
var wantedPosition = tr.TransformPoint(offsetPosition.Value);

// calculate position
if(doPosition.Value == true)
{
wantedPosition = new Vector3( wantedPosition.x * positionConstrain.Value.x,
                               wantedPosition.y * positionConstrain.Value.y,
                               wantedPosition.z * positionConstrain.Value.z);

go.transform.position = wantedPosition;
}


// calculate rotation
go.transform.eulerAngles = new Vector3( tr.eulerAngles.x * rotationConstrain.Value.x,
                                      tr.eulerAngles.y * rotationConstrain.Value.y,
                                      tr.eulerAngles.z * rotationConstrain.Value.z);
}
}
}

« Last Edit: June 07, 2011, 10:09:04 PM by giyomu »

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • FluidPlay Studios
Re: Constrain
« Reply #1 on: June 11, 2011, 03:29:40 PM »
Very useful, thanks. We'll eventually need an IK action, anyone up for this (somewhat daunting) task? :)
--
Breno "MaDDoX" Azevedo
@brenoazevedo

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Constrain
« Reply #2 on: June 13, 2011, 02:02:00 AM »
How much limbs and degrees of freedom for your IK action?

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • FluidPlay Studios
Re: Constrain
« Reply #3 on: October 21, 2011, 09:21:27 PM »
How much limbs and degrees of freedom for your IK action?
2D IK with a pole vector is the best option imo. I've seen some guys getting huge progresses on that, I didn't have the chance to check it closer though. If you can do it it'd be awesome Jean ;)
--
Breno "MaDDoX" Azevedo
@brenoazevedo

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Constrain
« Reply #4 on: October 24, 2011, 03:17:48 AM »
HI Maddox,

 I am unfortunatly not clever enough to build a unlimited chain Ik... :) I can do limb ones of any constraint tho and some specific more complex IK, but generic all purposes ones, are bloody hard. and there is actually one available for free in the asset store. I requires serious optimizations but it works. THere is fully IK solution from blender available ( open source), but I fail to find my way around this to port it :)

 I am just very surprised that no one came up with a proper Ik on the asset store nor that Unity seems to have this in their target... very sad, I want Ik too :)

http://u3d.as/content/dogzer/inverse-kinematics/2fP

 Bye,

 Jean

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • FluidPlay Studios
Re: Constrain
« Reply #5 on: October 26, 2011, 02:23:59 AM »
In fact this is quite sexy already, even if limited to two parts. Thanks for the link - and congratz on it too, since Dogzer mentions you gave him a nice helping hand.

BTW, I've had lots of pain with Unity's Locomotion package in the past, have you ever tried - and managed to make it work - with a custom character?
--
Breno "MaDDoX" Azevedo
@brenoazevedo

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Constrain
« Reply #6 on: October 26, 2011, 06:27:27 AM »
Hi,

 Yes locomotion is a pain unless you are ready to dive in it fully. I made several attempts but failed to commit into making it work properly. I want to animate spiders and stuff like that, I should have start easy I guess. I know someone did succeed making multi legs stuff with locomotion. Mind you it was when I was running a trial version, and really knew nothing at all about Unity. I should make a new attempt:)

So yes... locomotion is full of gems in terms of code and useful stuff, but lacks of proper commitment to maintain it and make it evolve. Note that locomotion has its own limb ik solver ( that I failed to extract too :) at the time).

What kind of character do you want to animate?


 Bye,

 Jean

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • FluidPlay Studios
Re: Constrain
« Reply #7 on: November 17, 2011, 01:34:22 AM »
I've tried with a cartoon character and just one walk animation and it completely failed - during the step's leg stretch the pelvis comes all the way down to the ground, no idea how to limit it to some minimum height. In another character with more conventional animations (and a good number of them) it did work okay, but only after starting with the character laying down :) No idea what's the problem with his code, it's too deep-math and tricky to wrap my mind around it. Anyways, the final animation result is kinda "jelly", if you get what I mean, and it's probably not performant enough for mobile game development, so I didn't spend more time on it (then the lot I had already wasted). If anyone asks me I'll simply recommend to stay away from it with a 10-foot pole. Or longer.

PS.: I could send you the cartoon character scene if you're curious, the result is fricking ugly.
« Last Edit: November 19, 2011, 08:47:14 PM by MaDDoX »
--
Breno "MaDDoX" Azevedo
@brenoazevedo

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Constrain
« Reply #8 on: November 18, 2011, 06:35:34 AM »
Hi,
 

 I am unavailable for the next few days, attending a Trade show, but send it to me beginning of december (if you send it now, i'll forget...) and i'll have a llok. Tho the problem could well be the way the animation was done, and baked in your 3d software,  maybe.

Bye,

Jean