# Quaternion.LookRotation Implementation

## Recommended Posts

Hi Guys... For the toolkit, I am creating a layer of UNITY-LIKE Game Mechanic API. Kinda like how unreal has a Chart Called "UnrealEngine for Unity Developers"

where they would OUTLINE how the same thing you do in Unity you would do in Unreal.

For Example: In Unity there is a TON of game mechanic code that in Unity:

Quaternion.Euler(x, y, z) but for use Babylon that would be more of a BABYLON.Quaternion.RotationYawPitchRoll(Y, X, Z).

Note the XYZ ---> YXZ

So I have a Util Function Called: BABYLON.Utilities.Euler(X, Y, Z) that simply wrap BABYLON.Qauternion.RotationYawPitchRoll(Y, X, Z)

I am trying to create a matching call for Unity

Quaternion.LookRotation

public static Quaternion LookRotation(Vector3 forward, Vector3 upwards = Vector3.up);

Parameters

 forward The direction to look in. upwards The vector that defines in which direction up is.

Description

Creates a rotation with the specified forward and upwards directions.

Returns the computed quaternion. If used to orient a Transform, the Z axis will be aligned with forward/ and the Y axis with upwards if these vectors are orthogonal. Logs an error if the forward direction is zero.

using UnityEngine;

using System.Collections;

public class ExampleClass : MonoBehaviour {

public Transform target;

void Update() {

Vector3 relativePos = target.position - transform.position;

Quaternion rotation = Quaternion.LookRotation(relativePos);

transform.rotation = rotation;

}

}

My Question is, how to make this Wrapper function for Babylon...

As Always ANY Help is appreciated

##### Share on other sites

Hey Mackey, since Babylon already provides a method to convert euler angles to quaternions (RotationYawPitchRoll like you mentioned), you could try to convert the forward and upwards parameters of LookRotation to a euler angle and pass those values to the existing method. I think the forward vector can be converted to yaw + pitch and the roll can be determined with the help of the up vector. See https://stackoverflow.com/questions/2782647/how-to-get-yaw-pitch-and-roll-from-a-3d-vector and http://www.jldoty.com/code/DirectX/YPRfromUF/YPRfromUF.html for guidance. Hope this helps.

##### Share on other sites

7 hours ago, trevordev said:

Hey Mackey, since Babylon already provides a method to convert euler angles to quaternions (RotationYawPitchRoll like you mentioned), you could try to convert the forward and upwards parameters of LookRotation to a euler angle and pass those values to the existing method. I think the forward vector can be converted to yaw + pitch and the roll can be determined with the help of the up vector. See https://stackoverflow.com/questions/2782647/how-to-get-yaw-pitch-and-roll-from-a-3d-vector and http://www.jldoty.com/code/DirectX/YPRfromUF/YPRfromUF.html for guidance. Hope this helps.

I dont suppose you could write out real quick a little babylon version of Quat.LookRotation... PLEASE

##### Share on other sites

It appears there is already a lookAt method that the cameras were using. I wrapped them to give the same signature you are asking for. Give this a shot.

##### Share on other sites

10 hours ago, trevordev said:

It appears there is already a lookAt method that the cameras were using. I wrapped them to give the same signature you are asking for. Give this a shot.

Thanks Bro... I am gonna try these Util Function in my toolkit Managed API

/** Returns a new Quaternion set from the passed vector position. */
public static LookRotation(position:BABYLON.Vector3):BABYLON.Quaternion {
var result:BABYLON.Quaternion = BABYLON.Quaternion.Zero();
BABYLON.Utilities.LookRotationToRef(position, result);
return result;
}
/** Returns a new Quaternion set from the passed vector position. */
public static LookRotationToRef(position:BABYLON.Vector3, result:BABYLON.Quaternion):void {
BABYLON.Utilities.TempMatrix.reset();
BABYLON.Matrix.LookAtLHToRef(BABYLON.Utilities.ZeroVector, position, BABYLON.Utilities.UpVector, BABYLON.Utilities.TempMatrix)
BABYLON.Utilities.TempMatrix.invert()
BABYLON.Quaternion.FromRotationMatrixToRef(BABYLON.Utilities.TempMatrix, result);
}

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.