playMaker

Author Topic: Vector3's and Quaternions  (Read 5209 times)

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2488
  • Yup.
    • View Profile
    • Cleverous
Vector3's and Quaternions
« on: September 18, 2013, 06:38:54 PM »
Whats the difference between a Vec3 and a Quat?

Tonight I was trying to build a physics motor for an AI and I wanted to get the current rotation, then get the target position and figure out the rotation needed to look at the target, then use that as a multiplier for torque input to turn the AI object toward the target. With lots of angular drag it would stop itself plenty close to the target and I could setup a threshold to prevent oscillation. No problem. I don't want to use LookAt or similar because of it overriding the physics input from the scene during collisions.

But I got confused. Very confused.

Are Vector3's directions, or are they positions? Can they be used for both? I do know that I can apply torque on a vector but is that designed as a convenience? I know I could populate a vector3 and use it just as simple data but it seems like it wouldn't work for anything outside the 0 - 1 range. I haven't tried it, but the idea confuses me. I recall once noticing it and the debug was really unpredictable until I set it into the 0 - 1 value range and it made sense.

This lead me to wonder if I should be using quaternions to accomplish this, since they're rotations, but they are really difficult to simply glance at and begin working with. Even with basic testing I wasn't able to figure out what they actually do.

Unity Docs say they're rotations, but how are they rotations if they do not define anything other than axis coordinates like a vector3 does? Why use quaternions instead of rotational data as floats?

So, do I even need quaternions for what I'm trying to do? I'm kind of flabbergasted that I can't actually just sit down and figure this out in an hour or two like I usually can. In addition to looking for a basic explanation, I'd still like an idea of how to go about doing what I mentioned at the top.

Thanks

Flying Robot

  • Sr. Member
  • ****
  • Posts: 293
  • Od ton yebo redro
    • View Profile
    • Flying Robot Studios
Re: Vector3's and Quaternions
« Reply #1 on: September 18, 2013, 10:23:24 PM »
Well, I got into a similar challenge while doing AI for Wheelie Town Heroes. In that game, the cars had to analyze what angle are they from their target (waypoint or enemy) and steer accordingly.

Let me take a look at it again, it's kinda fuzzy in my memory right now. I'll come back and tell you how I did that.

Oh, Quats are another way of expressing rotation value.

Flying Robot

  • Sr. Member
  • ****
  • Posts: 293
  • Od ton yebo redro
    • View Profile
    • Flying Robot Studios
Re: Vector3's and Quaternions
« Reply #2 on: September 19, 2013, 12:08:22 AM »
You need to take a look at this. This awesome action by Jean helped me a lot!

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

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2488
  • Yup.
    • View Profile
    • Cleverous
Re: Vector3's and Quaternions
« Reply #3 on: September 19, 2013, 07:59:05 AM »
I get weird results using that action, the result is dependent on the rotation of the target object, so I can have two static objects and rotate the target object and get a different angle result. The rotation of the target shouldn't have a bearing on the angle between the origin and target.

I tinkered with Get Angle To Target but it only calculates one axis. If this filled a Vector3 it may work for what I'm trying to do.

*Edit
Bah, Get Angle To Target gets the degrees from forward, but not in which direction so its always a positive number regardless of whether it is to the left or right, 45 degrees left is the same float as 45 degrees right.

So really, I must have a vector direction to use.
« Last Edit: September 19, 2013, 09:12:36 AM by Lane »

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2488
  • Yup.
    • View Profile
    • Cleverous
Re: Vector3's and Quaternions
« Reply #4 on: September 19, 2013, 11:47:46 AM »
Is there a way to reflect the values of a Quaternion into a Vector3?

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3634
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: Vector3's and Quaternions
« Reply #5 on: September 19, 2013, 02:19:54 PM »
I haven't used it, but this custom action should do it:
https://hutonggames.fogbugz.com/default.asp?W1094

And there are some other Quaternion actions one level up:
https://hutonggames.fogbugz.com/default.asp?W967

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14698
  • Official Playmaker Support
    • View Profile
Re: Vector3's and Quaternions
« Reply #6 on: September 19, 2013, 11:36:43 PM »
Hi,

Before you start using Quaternion, you really "must" understand what they are, cause indeed they are difficult to tackle at time.

 Have you searched for some vulgarisation web sites to explain quats? Typically, The unity doc gives example on each usage:

http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.html

bye,

 Jean

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2488
  • Yup.
    • View Profile
    • Cleverous
Re: Vector3's and Quaternions
« Reply #7 on: September 23, 2013, 04:30:06 AM »
Thanks guys.

I had looked at the Unity Docs before, still left me a little confused. Gonna put this on the backburner for a while and try to gradually start working into it.

Kinda frustrating getting stumped and not seeing progress. I can see when watching debug data that I can use Quat's do get the result I want, I just don't know the terminology in the actions and ways to get the math & results yet.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14698
  • Official Playmaker Support
    • View Profile
Re: Vector3's and Quaternions
« Reply #8 on: September 24, 2013, 03:18:16 AM »
Hi,

 I think you are overthinking this. If you have a lookat rotation system using torque, use this:

http://wiki.unity3d.com/index.php/TorqueLookRotation

if you study the code, it's like 3 lines... magic!!!

The code is commented out and clear ( imo), but if you have trouble implementing it in playmaker ( if you try instead of using the script as is), let me know. I think I did this on a previous project already.

bye,

 Jean

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2488
  • Yup.
    • View Profile
    • Cleverous
Re: Vector3's and Quaternions
« Reply #9 on: September 24, 2013, 07:19:56 AM »
Well that was pretty damn helpful! I couldn't figure out how to build that out of actions so I just made an action out of the script, see the attachment for the Action and Sample Scene.

I dunno how to translate the last bit of his code to PM actions, he does AddTorque with the vector3, the angle float and float multiplier for torque which confuses me.. I didn't even know you could do that and it seems to 'just work'. Makes perfect sense, but (to me) only in code.

Either way, really it was a great and fun learning experience making the action so thanks a bunch for the link! I might add a keep vertical option, or make a new action for keeping vertical with choice of direction and add an oscillation tolerance float with an if statement to return if the result is too small.

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.
// original script here: http://wiki.unity3d.com/index.php/TorqueLookRotation

using UnityEngine;
using System.Collections;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Physics)]
[Tooltip("Order a game object to look at a target by using Torque on its axi")]
public class TorqueLookRotation : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(Rigidbody))]
          [Tooltip("The object to effect.")]
public FsmOwnerDefault gameObject;

[RequiredField]
[Tooltip("The target it should be looking at")]
public FsmGameObject targetObject;

[RequiredField]
[Tooltip("The multiplier of torque to apply.")]
public float forceMultiplier;

[Tooltip("Repeat every frame.")]
public bool everyFrame = true;

public override void Reset()
{
gameObject = null;
targetObject = null;
forceMultiplier = 0.1f;
everyFrame = true;
}

public override void OnEnter()
{
DoLookAt();

if (!everyFrame)
{
Finish();
}
}

public override void OnFixedUpdate()
{
DoLookAt();
}

void DoLookAt() {

var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
LogWarning("Missing GameObject to manipulate!");
return;
}

if (go.rigidbody == null)
{
LogWarning("Missing rigid body: " + go.name);
return;
}

  var target = targetObject.Value;
if (target == null)
{
LogWarning("Missing target object to look at!");
return;
}

Vector3 targetDelta = target.transform.position - go.transform.position;
 
//get the angle between transform.forward and target delta
float angleDiff = Vector3.Angle(go.transform.forward, targetDelta);

// get its cross product, which is the axis of rotation to
// get from one vector to the other
Vector3 cross = Vector3.Cross(go.transform.forward, targetDelta);

// apply torque along that axis according to the magnitude of the angle.
go.rigidbody.AddTorque(cross * angleDiff * forceMultiplier);

}
}
}

*edit for code
« Last Edit: September 24, 2013, 07:51:36 AM by Lane »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14698
  • Official Playmaker Support
    • View Profile
Re: Vector3's and Quaternions
« Reply #10 on: September 24, 2013, 09:25:58 AM »
Hi,

 nice!

I'll see if I can do a playmaker version of this, it's not that hard tho. Please bump if nothing comes up in few days... :)


bye,

 Jean