Jump to content

Multiply a Vector3 times a Quaternion


MackeyK24
 Share

Recommended Posts

Hey guys I got a situation (fallowing some training material) where I need to multiply and Vector3 * Quaternion... Example:

 

var direction:BABYLON.Vector3 = new BABYLON.Vector3(0,0,-distance);
var rotation:BABYLON.Quaternion = this.manager.euler(rotaionX, rotaionY, 0.0);

// Something like this pseudo code below
var result:BABYLON.Vector3 = this.cameraTarget.position + rotation * direction;

 

How do I do something like this in Babylon ???

 

Link to comment
Share on other sites

4 minutes ago, adam said:

I looked again at what you're doing and you are probably going to want to use Vector3.TransformNormal()

Yo @adam do you think you format this code to something useful in Babylon and I will make part of a MothTools or something:

 

Edit:

// Something like this pseudo code below

var result:BABYLON.Vector3 = position + (rotation * direction);

Link to comment
Share on other sites

   

var position = BABYLON.Vector3.Zero();
var direction = new BABYLON.Vector3(1, 0, 0);

var rotation = BABYLON.Quaternion.RotationYawPitchRoll(Math.PI*.5, 0, 0);
var matrix = BABYLON.Matrix.Identity();
BABYLON.Matrix.FromQuaternionToRef(rotation, matrix);


var result = position.add(BABYLON.Vector3.TransformNormal(direction, matrix)); 

console.log(result);

 

Link to comment
Share on other sites

16 minutes ago, adam said:

   


var position = BABYLON.Vector3.Zero();
var direction = new BABYLON.Vector3(1, 0, 0);

var rotation = BABYLON.Quaternion.RotationYawPitchRoll(Math.PI*.5, 0, 0);
var matrix = BABYLON.Matrix.Identity();
BABYLON.Matrix.FromQuaternionToRef(rotation, matrix);


var result = position.add(BABYLON.Vector3.TransformNormal(direction, matrix)); 

console.log(result);

 

 Yo @adam Thanks man... I will put this in some kind of Helper Tool :)

 

Link to comment
Share on other sites

Yo @adam  Just so I'm clear... Does this do the actual Multplication of the rotation (converted to a matrix) times Direction Vector..

So I should be able to do something like:

public multplyVectorByQuaternion(vec:Vector3, quat:Quaternion):Vector3 {
 var matrix = BABYLON.Matrix.Identity();
 BABYLON.Matrix.FromQuaternionToRef(quat, matrix);
 return BABYLON.Vector3.TransformNormal(vec, matrix);
}

And if that works... I make a xxxToRef version:

public multplyVectorByQuaternionToRef(vec:Vector3, quat:Quaternion, result:Vector3):void {
 var matrix = BABYLON.Matrix.Identity();
 BABYLON.Matrix.FromQuaternionToRef(quat, matrix);
 BABYLON.Vector3.TransformNormalToRef(vec, matrix, result);
}

Note: I will of a better name than: 'multplyVectorByQuaternion'

But should that work ???

Link to comment
Share on other sites

Here are two versions of my "rotateVector3ToRef" function.
basic version:

export function rotateVector3ToRef(pV: BABYLON.Vector3, pRot: BABYLON.Quaternion,
        pResult: BABYLON.Vector3): void
    {
        //pResult = pRot * pV * conjugate(pRot)
        let tQuat = pRot.multiply(new BABYLON.Quaternion(pV.x, pV.y, pV.z, 0));
        tQuat.multiplyInPlace(pRot.conjugate());

        pResult.x = tQuat.x;
        pResult.y = tQuat.y;
        pResult.z = tQuat.z;
    }

 

And a faster version based on this: http://blog.molecular-matters.com/2013/05/24/a-faster-quaternion-vector-multiplication/
This is the one i'm using in production.

    export function rotateVector3ToRef(pV: BABYLON.Vector3, pRot: BABYLON.Quaternion,
        pResult: BABYLON.Vector3): void
    {
        //t = 2 * cross(pRotation.xyz, pVector)
        //pResult = pVector + pRotation.w * t * cross(pRotation.xyz, t)
        let tx = 2 * (pRot.y * pV.z - pRot.z * pV.y);
        let ty = 2 * (pRot.z * pV.x - pRot.x * pV.z);
        let tz = 2 * (pRot.x * pV.y - pRot.y * pV.x);

        pResult.x = pV.x + pRot.w * tx + (pRot.y * tz - pRot.z * ty);
        pResult.y = pV.y + pRot.w * ty + (pRot.z * tx - pRot.x * tz);
        pResult.z = pV.z + pRot.w * tz + (pRot.x * ty - pRot.y * tx);
    }

 

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...